This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d2bafe9
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |