forked from clue/reactphp-block
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionAwaitTest.php
More file actions
239 lines (190 loc) · 6.73 KB
/
FunctionAwaitTest.php
File metadata and controls
239 lines (190 loc) · 6.73 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
namespace Clue\Tests\React\Block;
use React\Promise;
use React\Promise\Timer\TimeoutException;
use Clue\React\Block;
class FunctionAwaitTest extends TestCase
{
public function testAwaitOneRejected()
{
$promise = $this->createPromiseRejected(new \Exception('test'));
$this->setExpectedException('Exception', 'test');
Block\await($promise, $this->loop);
}
public function testAwaitOneRejectedWithFalseWillWrapInUnexpectedValueException()
{
$promise = Promise\reject(false);
$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type bool');
Block\await($promise, $this->loop);
}
public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException()
{
$promise = Promise\reject(null);
$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type NULL');
Block\await($promise, $this->loop);
}
/**
* @requires PHP 7
*/
public function testAwaitOneRejectedWithPhp7Error()
{
$expected = new \Error('test');
$promise = $this->createPromiseRejected($expected);
// PHPUnit doesn't have something like setExpectedThrowable,
// this try catch block tests if the Error is actually thrown
// when calling Block\await
try {
Block\await($promise, $this->loop);
$this->fail('Expected \Error to be thrown, did not happen');
}
catch (\Error $error) {
self::assertSame($expected, $error);
}
}
public function testAwaitOneResolved()
{
$promise = $this->createPromiseResolved(2);
$this->assertEquals(2, Block\await($promise, $this->loop));
}
public function testAwaitOneInterrupted()
{
$promise = $this->createPromiseResolved(2, 0.02);
$this->createTimerInterrupt(0.01);
$this->assertEquals(2, Block\await($promise, $this->loop));
}
public function testAwaitOncePendingWillThrowOnTimeout()
{
$promise = new Promise\Promise(function () { });
$this->setExpectedException('React\Promise\Timer\TimeoutException');
Block\await($promise, $this->loop, 0.001);
}
public function testAwaitOncePendingWillThrowAndCallCancellerOnTimeout()
{
$cancelled = false;
$promise = new Promise\Promise(function () { }, function () use (&$cancelled) {
$cancelled = true;
});
try {
Block\await($promise, $this->loop, 0.001);
} catch (TimeoutException $expected) {
$this->assertTrue($cancelled);
}
}
public function testAwaitOnceWithTimeoutWillResolvemmediatelyAndCleanUpTimeout()
{
$promise = Promise\resolve(true);
$time = microtime(true);
Block\await($promise, $this->loop, 5.0);
$this->loop->run();
$time = microtime(true) - $time;
$this->assertLessThan(0.1, $time);
}
public function testAwaitOneResolvesShouldNotCreateAnyGarbageReferences()
{
if (class_exists('React\Promise\When') && PHP_VERSION_ID >= 50400) {
$this->markTestSkipped('Not supported on legacy Promise v1 API with PHP 5.4+');
}
gc_collect_cycles();
$promise = Promise\resolve(1);
Block\await($promise, $this->loop);
unset($promise);
$this->assertEquals(0, gc_collect_cycles());
}
public function testAwaitOneRejectedShouldNotCreateAnyGarbageReferences()
{
if (class_exists('React\Promise\When') && PHP_VERSION_ID >= 50400) {
$this->markTestSkipped('Not supported on legacy Promise v1 API with PHP 5.4+');
}
gc_collect_cycles();
$promise = Promise\reject(new \RuntimeException());
try {
Block\await($promise, $this->loop);
} catch (\Exception $e) {
// no-op
}
unset($promise, $e);
$this->assertEquals(0, gc_collect_cycles());
}
public function testAwaitOneRejectedWithTimeoutShouldNotCreateAnyGarbageReferences()
{
if (class_exists('React\Promise\When') && PHP_VERSION_ID >= 50400) {
$this->markTestSkipped('Not supported on legacy Promise v1 API with PHP 5.4+');
}
gc_collect_cycles();
$promise = Promise\reject(new \RuntimeException());
try {
Block\await($promise, $this->loop, 0.001);
} catch (\Exception $e) {
// no-op
}
unset($promise, $e);
$this->assertEquals(0, gc_collect_cycles());
}
public function testAwaitNullValueShouldNotCreateAnyGarbageReferences()
{
if (class_exists('React\Promise\When') && PHP_VERSION_ID >= 50400) {
$this->markTestSkipped('Not supported on legacy Promise v1 API with PHP 5.4+');
}
gc_collect_cycles();
$promise = Promise\reject(null);
try {
Block\await($promise, $this->loop);
} catch (\Exception $e) {
// no-op
}
unset($promise, $e);
$this->assertEquals(0, gc_collect_cycles());
}
/**
* @requires PHP 7
*/
public function testAwaitPendingPromiseWithTimeoutAndCancellerShouldNotCreateAnyGarbageReferences()
{
if (class_exists('React\Promise\When')) {
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}
gc_collect_cycles();
$promise = new \React\Promise\Promise(function () { }, function () {
throw new \RuntimeException();
});
try {
Block\await($promise, $this->loop, 0.001);
} catch (\Exception $e) {
// no-op
}
unset($promise, $e);
$this->assertEquals(0, gc_collect_cycles());
}
/**
* @requires PHP 7
*/
public function testAwaitPendingPromiseWithTimeoutAndWithoutCancellerShouldNotCreateAnyGarbageReferences()
{
gc_collect_cycles();
$promise = new \React\Promise\Promise(function () { });
try {
Block\await($promise, $this->loop, 0.001);
} catch (\Exception $e) {
// no-op
}
unset($promise, $e);
$this->assertEquals(0, gc_collect_cycles());
}
/**
* @requires PHP 7
*/
public function testAwaitPendingPromiseWithTimeoutAndNoOpCancellerShouldNotCreateAnyGarbageReferences()
{
gc_collect_cycles();
$promise = new \React\Promise\Promise(function () { }, function () {
// no-op
});
try {
Block\await($promise, $this->loop, 0.001);
} catch (\Exception $e) {
// no-op
}
unset($promise, $e);
$this->assertEquals(0, gc_collect_cycles());
}
}