Skip to content

Commit

Permalink
chore: improve unit test (#66)
Browse files Browse the repository at this point in the history
* chore: update laravel-point and reformat

* chore: import test
  • Loading branch information
godruoyi authored Mar 12, 2024
1 parent bf77419 commit 9c87138
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/Sonyflake.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Sonyflake extends Snowflake
/**
* Build Sonyflake Instance.
*
* @param int $machineId machine ID 0 ~ 65535 (2^16)-1
* @param int $machineId machine ID 0 ~ 65535 (2^16)-1
*/
public function __construct(protected int $machineId = 0)
{
Expand Down
39 changes: 23 additions & 16 deletions tests/BatchSnowflakeIDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,19 @@ public function test_batch_for_same_instance_with_default_driver(): void
public function test_batch_for_diff_instance_with_default_driver(): void
{
$ids = [];
$count = 100000; // 10w
$count = 100_000; // 100k

for ($i = 0; $i < $count; $i++) {
$ids[(new Snowflake())->id()] = 1;
}

$this->assertNotCount($count, $ids);
// This pattern will result in generating duplicate IDs.
$this->assertGreaterThan(90000, count($ids));
}

/**
* @throws Throwable
*/
public function test_batch_for_diff_instance_with_redis_driver()
{
if (! extension_loaded('redis')
Expand All @@ -58,38 +61,45 @@ public function test_batch_for_diff_instance_with_redis_driver()
$this->markTestSkipped('The pcntl extension is not installed.');
}

$this->parallelRun(function () {
$results = $this->parallelRun(function () {
$redis = new \Redis();
$redis->connect(getenv('REDIS_HOST'), getenv('REDIS_PORT') | 0);

return new RedisSequenceResolver($redis);
}, 100, 1000);

// Should generate 100k unique IDs
$this->assertResults($results, 100, 1000);
}

/**
* @throws Throwable
*/
public function test_batch_for_diff_instance_with_file_driver()
{
$fileResolver = new FileLockResolver(__DIR__);

$this->parallelRun(function () use ($fileResolver) {
$results = $this->parallelRun(function () use ($fileResolver) {
return $fileResolver;
}, 100, 1000);

// Should generate 100k unique IDs
$this->assertResults($results, 100, 1000);

$fileResolver->cleanAllLocksFile();
}

/**
* Runs the given function in parallel using the specified number of processes.
*
* @param callable $resolver
* @param int $parallel The number of processes to run in parallel.
* @param int $count The number of times to run the function.
* @return void
* @param int $parallel The number of processes to run in parallel.
* @param int $count The number of times to run the function.
*
* @throws Throwable
*/
protected function parallelRun(callable $resolver, int $parallel, int $count): void
protected function parallelRun(callable $resolver, int $parallel, int $count): array
{
$results = Support\Parallel::run(function () use ($resolver, $count) {
return Support\Parallel::run(function () use ($resolver, $count) {
$snowflake = (new Snowflake(0, 0))
->setSequenceResolver($resolver())
->setStartTimeStamp(strtotime('2022-12-14') * 1000);
Expand All @@ -101,17 +111,14 @@ protected function parallelRun(callable $resolver, int $parallel, int $count): v

return $ids;
}, $parallel);

$this->assertResults($results, $parallel, $count);
}

/**
* Asserts the results of a parallel execution.
*
* @param array $results The array of results.
* @param int $parallel The number of parallel executions.
* @param int $count The expected count for each execution.
* @return void
* @param array $results The array of results.
* @param int $parallel The number of parallel executions.
* @param int $count The expected count for each execution.
*/
private function assertResults(array $results, int $parallel, int $count): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/DiffWorkIdBatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public function test_diss_work_id(): void
$ids[$id] = 1;
}

$this->assertTrue(20000 === count($ids));
$this->assertTrue(count($ids) === 20000);
}
}
8 changes: 4 additions & 4 deletions tests/RedisSequenceResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public function test_sequence(): void

$snowflake = new RedisSequenceResolver($redis);

$this->assertTrue(0 == $snowflake->sequence(1));
$this->assertTrue(1 == $snowflake->sequence(1));
$this->assertTrue(2 == $snowflake->sequence(1));
$this->assertTrue(3 == $snowflake->sequence(1));
$this->assertTrue($snowflake->sequence(1) == 0);
$this->assertTrue($snowflake->sequence(1) == 1);
$this->assertTrue($snowflake->sequence(1) == 2);
$this->assertTrue($snowflake->sequence(1) == 3);
}

public function test_set_cache_prefix(): void
Expand Down
34 changes: 17 additions & 17 deletions tests/SnowflakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ public function test_work_id_and_data_center_id(): void
$this->assertTrue(! empty($snowflake->id()));
$this->assertTrue(strlen($id = $snowflake->id()) <= 19);

$this->assertTrue(1 === $snowflake->parseId($id, true)['datacenter']);
$this->assertTrue(2 === $snowflake->parseId($id, true)['workerid']);
$this->assertTrue($snowflake->parseId($id, true)['datacenter'] === 1);
$this->assertTrue($snowflake->parseId($id, true)['workerid'] === 2);

$snowflake = new Snowflake(999, 20);
$id = $snowflake->id();

$this->assertTrue(999 !== $snowflake->parseId($id, true)['datacenter']);
$this->assertTrue(20 === $snowflake->parseId($id, true)['workerid']);
$this->assertTrue($snowflake->parseId($id, true)['datacenter'] !== 999);
$this->assertTrue($snowflake->parseId($id, true)['workerid'] === 20);
}

public function test_extends(): void
Expand All @@ -85,9 +85,9 @@ public function test_extends(): void

$id = $snowflake->id();

$this->assertTrue(999 !== $snowflake->parseId($id, true)['datacenter']);
$this->assertTrue(999 === $snowflake->parseId($id, true)['sequence']);
$this->assertTrue(20 === $snowflake->parseId($id, true)['workerid']);
$this->assertTrue($snowflake->parseId($id, true)['datacenter'] !== 999);
$this->assertTrue($snowflake->parseId($id, true)['sequence'] === 999);
$this->assertTrue($snowflake->parseId($id, true)['workerid'] === 20);
}

public function test_batch(): void
Expand Down Expand Up @@ -116,7 +116,7 @@ public function test_batch(): void
$datas[$id] = 1;
}

$this->assertTrue(10000 === count($datas));
$this->assertTrue(count($datas) === 10000);
}

public function test_parse_id(): void
Expand All @@ -130,21 +130,21 @@ public function test_parse_id(): void

$data = $snowflake->parseId('1537200202186752', true);

$this->assertTrue(0 === $data['workerid']);
$this->assertTrue(0 === $data['datacenter']);
$this->assertTrue(0 === $data['sequence']);
$this->assertTrue($data['workerid'] === 0);
$this->assertTrue($data['datacenter'] === 0);
$this->assertTrue($data['sequence'] === 0);
$this->assertTrue($data['timestamp'] > 0);

$snowflake = new Snowflake(2, 3);
$id = $snowflake->id();
$payloads = $snowflake->parseId($id, true);

$this->assertTrue(2 === $payloads['datacenter']);
$this->assertTrue(3 === $payloads['workerid']);
$this->assertTrue($payloads['datacenter'] === 2);
$this->assertTrue($payloads['workerid'] === 3);
$this->assertLessThan(Snowflake::MAX_SEQUENCE_SIZE, $payloads['sequence']);

$payloads = $snowflake->parseId('0');
$this->assertTrue('' == $payloads['timestamp'] || false == $payloads['timestamp']);
$this->assertTrue($payloads['timestamp'] == '' || $payloads['timestamp'] == false);
$this->assertSame($payloads['workerid'], '0');
$this->assertSame($payloads['datacenter'], '0');
$this->assertSame($payloads['sequence'], '0');
Expand All @@ -164,7 +164,7 @@ public function test_set_start_time_stamp(): void
$snowflake = new Snowflake(999, 20);

$snowflake->setStartTimeStamp(1);
$this->assertTrue(1 === $snowflake->getStartTimeStamp());
$this->assertTrue($snowflake->getStartTimeStamp() === 1);
}

public function test_set_start_time_stamp_max_value_is_over(): void
Expand Down Expand Up @@ -193,7 +193,7 @@ public function test_get_start_time_stamp(): void
$this->assertTrue($snowflake->getStartTimeStamp() === (strtotime($defaultTime) * 1000));

$snowflake->setStartTimeStamp(1);
$this->assertTrue(1 === $snowflake->getStartTimeStamp());
$this->assertTrue($snowflake->getStartTimeStamp() === 1);
}

public function testcall_resolver(): void
Expand All @@ -207,7 +207,7 @@ public function testcall_resolver(): void
$seq = $snowflake->getSequenceResolver();

$this->assertTrue($seq instanceof Closure);
$this->assertTrue(999 === $seq(0));
$this->assertTrue($seq(0) === 999);
}

public function test_get_sequence_resolver(): void
Expand Down
6 changes: 3 additions & 3 deletions tests/SonyflakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function test_parse_id(): void
$this->assertArrayHasKey('sequence', $dumps);
$this->assertArrayHasKey('machineid', $dumps);
$this->assertArrayHasKey('timestamp', $dumps);
$this->assertTrue(110 == $dumps['machineid']);
$this->assertTrue($dumps['machineid'] == 110);
}

public function test_id(): void
Expand All @@ -102,7 +102,7 @@ public function test_id(): void
// $this->assertArrayNotHasKey($id, $datas);
$datas[$id] = 1;
}
$this->assertTrue(10000 === count($datas));
$this->assertTrue(count($datas) === 10000);
}

/**
Expand Down Expand Up @@ -169,7 +169,7 @@ public function test_get_start_time_stamp(): void
$this->assertTrue($snowflake->getStartTimeStamp() === (strtotime($defaultTime) * 1000));

$snowflake->setStartTimeStamp(1);
$this->assertTrue(1 === $snowflake->getStartTimeStamp());
$this->assertTrue($snowflake->getStartTimeStamp() === 1);
}

public function testget_current_millisecond(): void
Expand Down
10 changes: 2 additions & 8 deletions tests/Support/Parallel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ final class Parallel
/**
* Run specified callback in parallel.
*
* @param callable $callback
* @param int $parallel
* @return array
*
* @throws RuntimeException|Throwable
*/
Expand All @@ -46,8 +43,8 @@ public static function run(callable $callback, int $parallel = 100): array
/**
* Creates child processes to execute a callback function in parallel.
*
* @param callable $callback The callback function to execute in each child process.
* @param int $parallel The number of child processes to create (default: 100).
* @param callable $callback The callback function to execute in each child process.
* @param int $parallel The number of child processes to create (default: 100).
* @return array An array of child process information, including the process ID and the pipe.
*
* @throws RuntimeException If a child process cannot be created.
Expand Down Expand Up @@ -87,9 +84,6 @@ private static function createChildProcess(callable $callback, int $parallel = 1

/**
* Create pipelines with specified number, will fire a exception if failed.
*
* @param int $parallel
* @return array
*/
private static function createPipelines(int $parallel = 100): array
{
Expand Down
14 changes: 7 additions & 7 deletions tests/SwooleSequenceResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public function test_basic(): void
{
$snowflake = new SwooleSequenceResolver();

$this->assertTrue(0 == $snowflake->sequence(0));
$this->assertTrue(1 == $snowflake->sequence(0));
$this->assertTrue(2 == $snowflake->sequence(0));
$this->assertTrue(3 == $snowflake->sequence(0));
$this->assertTrue($snowflake->sequence(0) == 0);
$this->assertTrue($snowflake->sequence(0) == 1);
$this->assertTrue($snowflake->sequence(0) == 2);
$this->assertTrue($snowflake->sequence(0) == 3);

$this->assertTrue(0 == $snowflake->sequence(1));
$this->assertTrue(1 == $snowflake->sequence(1));
$this->assertTrue(2 == $snowflake->sequence(1));
$this->assertTrue($snowflake->sequence(1) == 0);
$this->assertTrue($snowflake->sequence(1) == 1);
$this->assertTrue($snowflake->sequence(1) == 2);
}

public function test_reset_lock(): void
Expand Down
15 changes: 10 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;
use ReflectionException;

class TestCase extends BaseTestCase
{
/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
* @return mixed Method return.
*
* @throws ReflectionException
*/
public function invokeMethod(&$object, $methodName, array $parameters = [])
{
Expand All @@ -36,9 +39,11 @@ public function invokeMethod(&$object, $methodName, array $parameters = [])
/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $propertyName property name to call
* @param object &$object Instantiated object that we will run method on.
* @param string $propertyName property name to call
* @return mixed Method return.
*
* @throws ReflectionException
*/
public function invokeProperty(&$object, string $propertyName)
{
Expand Down
3 changes: 2 additions & 1 deletion tests/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function test_time(): void
$s2 = $s->getCurrentMillisecond();
}
$a++;
$this->assertTrue($s1 != $s2);

$this->assertEquals($s1, $s2);
}
}
}

0 comments on commit 9c87138

Please sign in to comment.