Skip to content

Commit 28a2b79

Browse files
author
Willem Stuursma
committed
Switch to spatie/async for testing concurrency
1 parent 9e09100 commit 28a2b79

File tree

3 files changed

+18
-25
lines changed

3 files changed

+18
-25
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@
5353
"ext-pdo_sqlite": "*",
5454
"ext-sysvsem": "*",
5555
"eloquent/liberator": "^2.0",
56+
"friendsofphp/php-cs-fixer": "^2.16",
5657
"johnkary/phpunit-speedtrap": "^3.0",
57-
"kriswallsmith/spork": "^0.3",
5858
"mikey179/vfsstream": "^1.6",
5959
"php-mock/php-mock-phpunit": "^2.1",
6060
"phpunit/phpunit": "^9.4",
6161
"predis/predis": "^1.1",
62-
"squizlabs/php_codesniffer": "^3.3",
63-
"symfony/event-dispatcher": "^2.8"
62+
"spatie/async": "^1.5",
63+
"squizlabs/php_codesniffer": "^3.3"
6464
},
6565
"suggest": {
6666
"ext-igbinary": "To use this library with PHP Redis igbinary serializer enabled.",

tests/mutex/LockMutexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function setUp(): void
3434
public function testLockFails()
3535
{
3636
$this->expectException(LockAcquireException::class);
37-
37+
3838
$this->mutex->expects($this->once())
3939
->method('lock')
4040
->willThrowException(new LockAcquireException());

tests/mutex/MutexConcurrencyTest.php

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use PHPUnit\Framework\TestCase;
77
use Predis\Client;
88
use Redis;
9-
use Spork\ProcessManager;
9+
use Spatie\Async\Pool;
1010

1111
/**
1212
* Concurrency Tests for Mutex.
@@ -69,16 +69,15 @@ private function getPDO(string $dsn, string $user): \PDO
6969
* @param int $concurrency The amount of forks.
7070
* @param callable $code The code for the fork.
7171
*/
72-
private function fork($concurrency, callable $code)
72+
private function fork(int $concurrency, callable $code)
7373
{
74-
$manager = new ProcessManager();
75-
$manager->setDebug(true);
74+
$pool = Pool::create();
7675

7776
for ($i = 0; $i < $concurrency; $i++) {
78-
$manager->fork($code);
77+
$pool[] = async($code);
7978
}
8079

81-
$manager->check();
80+
await($pool);
8281
}
8382

8483
/**
@@ -92,8 +91,8 @@ private function fork($concurrency, callable $code)
9291
*/
9392
public function testHighContention(callable $code, callable $mutexFactory)
9493
{
95-
$concurrency = 2;
96-
$iterations = 20000 / $concurrency;
94+
$concurrency = 10;
95+
$iterations = 1000 / $concurrency;
9796
$timeout = $concurrency * 20;
9897

9998
$this->fork($concurrency, function () use ($mutexFactory, $timeout, $iterations, $code): void {
@@ -112,31 +111,25 @@ public function testHighContention(callable $code, callable $mutexFactory)
112111

113112
/**
114113
* Returns test cases for testHighContention().
115-
*
116-
* @return array The test cases.
117114
*/
118-
public function provideTestHighContention()
115+
public function provideTestHighContention(): array
119116
{
120117
$cases = array_map(function (array $mutexFactory) {
121-
$file = tmpfile();
122-
$this->assertEquals(4, fwrite($file, pack('i', 0)), 'Expected 4 bytes to be written to temporary file.');
118+
$filename = tempnam(sys_get_temp_dir(), 'php-lock');
119+
120+
file_put_contents($filename, pack('i', 0));
123121

124122
return [
125-
function (int $increment) use ($file): int {
126-
rewind($file);
127-
flock($file, LOCK_EX);
128-
$data = fread($file, 4);
123+
function (int $increment) use ($filename): int {
124+
$data = file_get_contents($filename);
129125

130126
$this->assertEquals(4, strlen($data), 'Expected four bytes to be present in temporary file.');
131127

132128
$counter = unpack('i', $data)[1];
133129

134130
$counter += $increment;
135131

136-
rewind($file);
137-
fwrite($file, pack('i', $counter));
138-
139-
flock($file, LOCK_UN);
132+
file_put_contents($filename, pack('i', $counter));
140133

141134
return $counter;
142135
},

0 commit comments

Comments
 (0)