nextcloud/tests/lib/Snowflake/ISequenceBase.php
Benjamin Gaussorgues 83dd9d313a
feat(snowflakeids): add File Sequence Generator
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
2025-11-03 11:45:13 +01:00

45 lines
1.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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) . 'isnt 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);
}
}