forked from clue/reactphp-block
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionAwaitAllTest.php
More file actions
133 lines (109 loc) · 3.76 KB
/
Copy pathFunctionAwaitAllTest.php
File metadata and controls
133 lines (109 loc) · 3.76 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
<?php
namespace Clue\Tests\React\Block;
use React\Promise;
use React\Promise\Timer\TimeoutException;
use Clue\React\Block;
class FunctionAwaitAllTest extends TestCase
{
public function testAwaitAllEmpty()
{
$this->assertEquals(array(), Block\awaitAll(array(), $this->loop));
}
public function testAwaitAllAllResolved()
{
$all = array(
'first' => $this->createPromiseResolved(1),
'second' => $this->createPromiseResolved(2)
);
$this->assertEquals(array('first' => 1, 'second' => 2), Block\awaitAll($all, $this->loop));
}
public function testAwaitAllReturnsArrayWithFulfilledValueFromSinglePromiseWithoutGivingLoop()
{
$promise = Promise\resolve(42);
$this->assertEquals(array(42), Block\awaitAll(array($promise)));
}
public function testAwaitAllRejected()
{
$all = array(
$this->createPromiseResolved(1),
$this->createPromiseRejected(new \Exception('test'))
);
$this->setExpectedException('Exception', 'test');
Block\awaitAll($all, $this->loop);
}
public function testAwaitAllRejectedWithFalseWillWrapInUnexpectedValueException()
{
$all = array(
$this->createPromiseResolved(1),
Promise\reject(false)
);
$this->setExpectedException('UnexpectedValueException');
Block\awaitAll($all, $this->loop);
}
public function testAwaitAllOnlyRejected()
{
$all = array(
$this->createPromiseRejected(new \Exception('first')),
$this->createPromiseRejected(new \Exception('second'))
);
$this->setExpectedException('Exception', 'first');
Block\awaitAll($all, $this->loop);
}
public function testAwaitAllInterrupted()
{
$promise = $this->createPromiseResolved(2, 0.02);
$this->createTimerInterrupt(0.01);
$this->assertEquals(array(2), Block\awaitAll(array($promise), $this->loop));
}
public function testAwaitAllWithRejectedWillCancelPending()
{
$cancelled = false;
$promise = new Promise\Promise(function () { }, function () use (&$cancelled) {
$cancelled = true;
});
$all = array(
Promise\reject(new \Exception('test')),
$promise
);
try {
Block\awaitAll($all, $this->loop);
$this->fail();
} catch (\Exception $expected) {
$this->assertEquals('test', $expected->getMessage());
$this->assertTrue($cancelled);
}
}
public function testAwaitAllPendingWillThrowAndCallCancellerOnTimeout()
{
$cancelled = false;
$promise = new Promise\Promise(function () { }, function () use (&$cancelled) {
$cancelled = true;
});
try {
Block\awaitAll(array($promise), $this->loop, 0.001);
} catch (TimeoutException $expected) {
$this->assertTrue($cancelled);
}
}
/**
* @requires PHP 7
*/
public function testAwaitAllPendingPromiseWithTimeoutAndCancellerShouldNotCreateAnyGarbageReferences()
{
if (class_exists('React\Promise\When')) {
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}
gc_collect_cycles();
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
$promise = new \React\Promise\Promise(function () { }, function () {
throw new \RuntimeException();
});
try {
Block\awaitAll(array($promise), $this->loop, 0.001);
} catch (\Exception $e) {
// no-op
}
unset($promise, $e);
$this->assertEquals(0, gc_collect_cycles());
}
}