-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLockerTest.php
60 lines (51 loc) · 1.25 KB
/
LockerTest.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\Locker;
use Hyperf\Engine\Channel;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use function Hyperf\Coroutine\go;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class LockerTest extends TestCase
{
public function testLockAndUnlock()
{
$chan = new Channel(10);
go(function () use ($chan) {
Locker::lock('foo');
$chan->push(1);
usleep(10000);
$chan->push(2);
Locker::unlock('foo');
});
go(function () use ($chan) {
Locker::lock('foo');
$chan->push(3);
usleep(10000);
$chan->push(4);
});
go(function () use ($chan) {
Locker::lock('foo');
$chan->push(5);
$chan->push(6);
});
$ret = [];
while ($res = $chan->pop(1)) {
$ret[] = $res;
}
$this->assertSame([1, 2, 3, 5, 6, 4], $ret);
}
}