Skip to content

Commit 6a35e7d

Browse files
authored
πŸ› fix bug (#39)
Fix the sequence is always 0 for RandomSequenceResolver
1 parent 5c4f88c commit 6a35e7d

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

β€Žsrc/RandomSequenceResolver.phpβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function sequence(int $currentTime)
4848
$this->sequence = mt_rand(0, $this->maxSequence);
4949
$this->lastTimeStamp = $currentTime;
5050

51-
return 0;
51+
return $this->sequence;
5252
}
5353

5454
/**
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the godruoyi/php-snowflake.
5+
*
6+
* (c) Godruoyi <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled.
9+
*/
10+
11+
namespace Tests;
12+
13+
use Godruoyi\Snowflake\Snowflake;
14+
15+
class BatchSnowflakeIDTest extends TestCase
16+
{
17+
public function testBatchUseSameInstance()
18+
{
19+
$ids = [];
20+
$count = 100000;
21+
$snowflake = new Snowflake();
22+
23+
for ($i = 0; $i < $count; $i++) {
24+
$id = $snowflake->id();
25+
$ids[$id] = 1;
26+
}
27+
28+
$this->assertCount($count, $ids);
29+
}
30+
31+
public function testBatchForDiffInstance()
32+
{
33+
$ids = [];
34+
$count = 100000; // 10w
35+
36+
for ($i = 0; $i < $count; $i++) {
37+
$ids[(new Snowflake())->id()] = 1;
38+
}
39+
40+
$this->assertNotCount($count, $ids);
41+
$this->assertGreaterThan(90000, count($ids));
42+
}
43+
}

β€Žtests/DiffWorkIdBatchTest.phpβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public function testDissWorkID()
2020

2121
$ids = [];
2222

23-
for ($i = 0; $i < 10000; ++$i) {
23+
for ($i = 0; $i < 10000; $i++) {
2424
$id = $snowflake->id();
2525

2626
$ids[$id] = 1;
2727
}
2828

2929
$snowflake = new Snowflake(1, 2);
3030

31-
for ($j = 0; $j < 10000; ++$j) {
31+
for ($j = 0; $j < 10000; $j++) {
3232
$id = $snowflake->id();
3333

3434
$ids[$id] = 1;

β€Žtests/SnowflakeTest.phpβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function testParseId()
139139

140140
$this->assertTrue(2 === $payloads['datacenter']);
141141
$this->assertTrue(3 === $payloads['workerid']);
142-
$this->assertTrue(0 === $payloads['sequence']);
142+
$this->assertLessThan(Snowflake::MAX_SEQUENCE_SIZE, $payloads['sequence']);
143143

144144
$payloads = $snowflake->parseId('0');
145145
$this->assertTrue('' == $payloads['timestamp'] || false == $payloads['timestamp']);

0 commit comments

Comments
Β (0)