|
12 | 12 |
|
13 | 13 | use OC\DB\Snowflake\NextcloudSequenceResolver; |
14 | 14 | use OC\DB\Snowflake\SnowflakeGenerator; |
| 15 | +use OCP\Server; |
15 | 16 | use PHPUnit\Framework\Attributes\TestWith; |
16 | 17 | use Test\TestCase; |
17 | 18 |
|
@@ -69,9 +70,58 @@ public function testLayout(bool $isCLIExpected, bool $is32BitsSystem, int $timeD |
69 | 70 | $this->assertEquals(42, $sequence); |
70 | 71 | } |
71 | 72 |
|
| 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 | + |
72 | 118 | #[TestWith(data: [true])] |
73 | 119 | #[TestWith(data: [false])] |
74 | 120 | 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 | + |
75 | 125 | $generator = $this->getMockBuilder(SnowflakeGenerator::class) |
76 | 126 | ->setConstructorArgs([21, 22, $this->createMock(NextcloudSequenceResolver::class), true]) |
77 | 127 | ->onlyMethods(['is32BitsSystem']) |
|
0 commit comments