Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
Add atomic counter test
Browse files Browse the repository at this point in the history
  • Loading branch information
k911 committed May 24, 2018
0 parents commit d2bafe9
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Server/AtomicCounterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace App\Tests\Bundle\SwooleBundle\Server;

use App\Bundle\SwooleBundle\Server\AtomicCounter;
use Generator;
use PHPUnit\Framework\TestCase;

class AtomicCounterTest extends TestCase
{
public function testCreateCounter(): void
{
$counter = AtomicCounter::fromZero();

$this->assertSame(0, $counter->get());
}

public function countProvider(): Generator
{
return $this->generateArrays(0, 999, 65563);
}

/**
* @dataProvider countProvider
*
* @param int $count
*/
public function testSingleThreadedCounting(int $count): void
{
$counter = AtomicCounter::fromZero();

$this->incrementCounter($counter, $count);

$this->assertSame($count, $counter->get());
}

private function generateArrays(...$values): Generator
{
foreach ($values as $value) {
yield [$value];
}
}

private function incrementCounter(AtomicCounter $counter, int $times): void
{
while ($times > 0) {
$counter->increment();
--$times;
}
}
}

0 comments on commit d2bafe9

Please sign in to comment.