-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFunctionTest.php
91 lines (81 loc) · 2.35 KB
/
FunctionTest.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?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\Coroutine;
use Hyperf\Engine\Channel;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use Swoole\Runtime;
use function Hyperf\Coroutine\defer;
use function Hyperf\Coroutine\go;
use function Hyperf\Coroutine\parallel;
use function Hyperf\Coroutine\run;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class FunctionTest extends TestCase
{
public function testReturnOfGo()
{
$uniqid = uniqid();
$id = go(function () use (&$uniqid) {
$uniqid = 'Hyperf';
});
$this->assertTrue(is_int($id));
$this->assertSame('Hyperf', $uniqid);
}
#[Group('NonCoroutine')]
public function testRun()
{
$asserts = [
SWOOLE_HOOK_ALL,
SWOOLE_HOOK_SLEEP,
SWOOLE_HOOK_CURL,
];
foreach ($asserts as $flags) {
run(function () use ($flags) {
$this->assertTrue(Coroutine::inCoroutine());
$this->assertSame($flags, Runtime::getHookFlags());
}, $flags);
}
}
public function testDefer()
{
$channel = new Channel(10);
parallel([function () use ($channel) {
defer(function () use ($channel) {
$channel->push(0);
});
defer(function () use ($channel) {
$channel->push(1);
defer(function () use ($channel) {
$channel->push(2);
});
defer(function () use ($channel) {
$channel->push(3);
});
});
defer(function () use ($channel) {
$channel->push(4);
});
$channel->push(5);
}]);
$this->assertSame(5, $channel->pop(0.001));
$this->assertSame(4, $channel->pop(0.001));
$this->assertSame(1, $channel->pop(0.001));
$this->assertSame(3, $channel->pop(0.001));
$this->assertSame(2, $channel->pop(0.001));
$this->assertSame(0, $channel->pop(0.001));
}
}