Skip to content

Commit 9599eab

Browse files
committed
fixup! fixup! feat(snowflake): Use string as type for snowflake ids
1 parent 109b151 commit 9599eab

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tests/lib/DB/SnowflakeTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use OC\DB\Snowflake\NextcloudSequenceResolver;
1414
use OC\DB\Snowflake\SnowflakeGenerator;
15+
use OCP\Server;
1516
use PHPUnit\Framework\Attributes\TestWith;
1617
use Test\TestCase;
1718

@@ -69,9 +70,58 @@ public function testLayout(bool $isCLIExpected, bool $is32BitsSystem, int $timeD
6970
$this->assertEquals(42, $sequence);
7071
}
7172

73+
#[TestWith(data: [true, true])]
74+
#[TestWith(data: [true, false])]
75+
#[TestWith(data: [false, true])]
76+
#[TestWith(data: [false, false])]
77+
public function testRandomSequence(bool $random, bool $is32BitsSystem): void {
78+
if (!$is32BitsSystem && PHP_INT_SIZE < 8) {
79+
$this->markTestSkipped('Unable to run 64 bits code on 32 bits system.');
80+
}
81+
82+
$baseTimestamp = strtotime('2025-01-01');
83+
if ($random) {
84+
$resolver = $this->createMock(NextcloudSequenceResolver::class);
85+
$resolver->method('isAvailable')->willReturn(false);
86+
} else {
87+
$resolver = Server::get(NextcloudSequenceResolver::class);
88+
$this->assertTrue($resolver->isAvailable());
89+
}
90+
91+
$snowFlake = $this->getMockBuilder(SnowflakeGenerator::class)
92+
->setConstructorArgs([21, 22, $resolver, true])
93+
->onlyMethods(['getCurrentMillisecond', 'is32BitsSystem'])
94+
->getMock();
95+
96+
$timeDiff = 42;
97+
if (PHP_INT_SIZE < 8) {
98+
$snowFlake->method('getCurrentMillisecond')
99+
->willReturn(gmp_strval(gmp_mul(gmp_add($baseTimestamp, $timeDiff), 1000)));
100+
} else {
101+
$snowFlake->method('getCurrentMillisecond')
102+
->willReturn((string)(($baseTimestamp + $timeDiff) * 1000));
103+
}
104+
105+
$snowFlake->method('is32BitsSystem')
106+
->willReturn($is32BitsSystem);
107+
108+
$snowFlake->setStartTimeStamp($baseTimestamp);
109+
110+
$ids = [];
111+
for ($i = 0; $i < 20; $i++) {
112+
$ids[] = $snowFlake->nextId();
113+
}
114+
115+
$this->assertEquals($ids, array_unique($ids));
116+
}
117+
72118
#[TestWith(data: [true])]
73119
#[TestWith(data: [false])]
74120
public function testSetStartTimeStamp(bool $is32BitsSystem): void {
121+
if (!$is32BitsSystem && PHP_INT_SIZE < 8) {
122+
$this->markTestSkipped('Unable to run 64 bits code on 32 bits system.');
123+
}
124+
75125
$generator = $this->getMockBuilder(SnowflakeGenerator::class)
76126
->setConstructorArgs([21, 22, $this->createMock(NextcloudSequenceResolver::class), true])
77127
->onlyMethods(['is32BitsSystem'])

0 commit comments

Comments
 (0)