-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMutexTest.php
60 lines (52 loc) · 1.32 KB
/
MutexTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Coroutine;
use Hyperf\Coroutine\Mutex;
use Hyperf\Coroutine\WaitGroup;
use Hyperf\Engine\Channel;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use function Hyperf\Coroutine\go;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class MutexTest extends TestCase
{
public function testMutexLock()
{
$chan = new Channel(5);
$func = function (string $value) use ($chan) {
if (Mutex::lock('test')) {
try {
usleep(1000);
$chan->push($value);
} finally {
Mutex::unlock('test');
}
}
};
$wg = new WaitGroup(5);
foreach (['h', 'e', 'l', 'l', 'o'] as $value) {
go(function () use ($func, $value, $wg) {
$func($value);
$wg->done();
});
}
$res = '';
$wg->wait(1);
for ($i = 0; $i < 5; ++$i) {
$res .= $chan->pop(1);
}
$this->assertSame('hello', $res);
}
}