nextcloud-server/tests/lib/Snowflake/ISequenceBase.php

46 lines
1.1 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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