-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWaiterTest.php
122 lines (103 loc) · 2.99 KB
/
WaiterTest.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?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\Context\ApplicationContext;
use Hyperf\Coroutine\Coroutine;
use Hyperf\Coroutine\Exception\WaitTimeoutException;
use Hyperf\Coroutine\Waiter;
use Hyperf\Engine\Channel;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use RuntimeException;
use function Hyperf\Coroutine\wait;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class WaiterTest extends TestCase
{
protected function setUp(): void
{
$container = Mockery::mock(ContainerInterface::class);
ApplicationContext::setContainer($container);
$container->shouldReceive('get')->with(Waiter::class)->andReturn(new Waiter());
}
protected function tearDown(): void
{
Mockery::close();
}
public function testWait()
{
$id = uniqid();
$result = wait(function () use ($id) {
return $id;
});
$this->assertSame($id, $result);
$id = rand(0, 9999);
$result = wait(function () use ($id) {
return $id + 1;
});
$this->assertSame($id + 1, $result);
}
public function testWaitNone()
{
$callback = function () {
};
$result = wait($callback);
$this->assertSame($result, $callback());
$this->assertSame(null, $result);
$callback = function () {
return null;
};
$result = wait($callback);
$this->assertSame($result, $callback());
$this->assertSame(null, $result);
}
public function testWaitException()
{
$message = uniqid();
$callback = function () use ($message) {
throw new RuntimeException($message);
};
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage($message);
wait($callback);
}
public function testWaitReturnException()
{
$message = uniqid();
$callback = function () use ($message) {
return new RuntimeException($message);
};
$result = wait($callback);
$this->assertInstanceOf(RuntimeException::class, $result);
$this->assertSame($message, $result->getMessage());
}
public function testPushTimeout()
{
$channel = new Channel(1);
$this->assertSame(true, $channel->push(1, 1));
$this->assertSame(false, $channel->push(1, 1));
}
public function testTimeout()
{
$callback = function () {
Coroutine::sleep(0.5);
return true;
};
$this->expectException(WaitTimeoutException::class);
$this->expectExceptionMessage('Channel wait failed, reason: Timed out for 0.001 s');
wait($callback, 0.001);
}
}