You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
nextcloud-server/tests/lib/Snowflake/ISequenceBase.php

45 lines
1.1 KiB

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Snowflake;
use OC\Snowflake\ISequence;
use Test\TestCase;
/**
* @package Test
*/
abstract class ISequenceBase extends TestCase {
protected ISequence $sequence;
public function testGenerator(): void {
if (!$this->sequence->isAvailable()) {
$this->markTestSkipped('Sequence ID generator ' . get_class($this->sequence) . 'is’nt available. Skip');
}
$nb = 1000;
$ids = [];
$server = 42;
for ($i = 0; $i < $nb; ++$i) {
$time = explode('.', (string)microtime(true));
$seconds = (int)$time[0];
$milliseconds = str_pad(substr($time[1] ?? '0', 0, 3), 3, '0');
$id = $this->sequence->nextId($server, $seconds, (int)$milliseconds);
$ids[] = sprintf('%d_%s_%d', $seconds, $milliseconds, $id);
usleep(100);
}
// Is it unique?
$this->assertCount($nb, array_unique($ids));
// Is it sequential?
$sortedIds = $ids;
natsort($sortedIds);
$this->assertSame($sortedIds, $ids);
}
}