forked from reactphp/async
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAwaitTest.php
More file actions
164 lines (134 loc) · 4.94 KB
/
AwaitTest.php
File metadata and controls
164 lines (134 loc) · 4.94 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
<?php
namespace React\Tests\Async;
use React;
use React\EventLoop\Loop;
use React\Promise\Promise;
class AwaitTest extends TestCase
{
public function testAwaitThrowsExceptionWhenPromiseIsRejectedWithException()
{
$promise = new Promise(function () {
throw new \Exception('test');
});
$this->setExpectedException('Exception', 'test');
React\Async\await($promise);
}
public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWithFalse()
{
if (!interface_exists('React\Promise\CancellablePromiseInterface')) {
$this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3');
}
$promise = new Promise(function ($_, $reject) {
$reject(false);
});
$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type bool');
React\Async\await($promise);
}
public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWithNull()
{
if (!interface_exists('React\Promise\CancellablePromiseInterface')) {
$this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3');
}
$promise = new Promise(function ($_, $reject) {
$reject(null);
});
$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type NULL');
React\Async\await($promise);
}
/**
* @requires PHP 7
*/
public function testAwaitThrowsErrorWhenPromiseIsRejectedWithError()
{
$promise = new Promise(function ($_, $reject) {
throw new \Error('Test', 42);
});
$this->setExpectedException('Error', 'Test', 42);
React\Async\await($promise);
}
public function testAwaitReturnsValueWhenPromiseIsFullfilled()
{
$promise = new Promise(function ($resolve) {
$resolve(42);
});
$this->assertEquals(42, React\Async\await($promise));
}
public function testAwaitReturnsValueWhenPromiseIsFulfilledEvenWhenOtherTimerStopsLoop()
{
$promise = new Promise(function ($resolve) {
Loop::addTimer(0.02, function () use ($resolve) {
$resolve(2);
});
});
Loop::addTimer(0.01, function () {
Loop::stop();
});
$this->assertEquals(2, React\Async\await($promise));
}
public function testAwaitShouldNotCreateAnyGarbageReferencesForResolvedPromise()
{
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 = new Promise(function ($resolve) {
$resolve(42);
});
React\Async\await($promise);
unset($promise);
$this->assertEquals(0, gc_collect_cycles());
}
public function testAwaitShouldNotCreateAnyGarbageReferencesForRejectedPromise()
{
if (class_exists('React\Promise\When')) {
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}
gc_collect_cycles();
$promise = new Promise(function () {
throw new \RuntimeException();
});
try {
React\Async\await($promise);
} catch (\Exception $e) {
// no-op
}
unset($promise, $e);
$this->assertEquals(0, gc_collect_cycles());
}
public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWithNullValue()
{
if (!interface_exists('React\Promise\CancellablePromiseInterface')) {
$this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3');
}
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 = new Promise(function ($_, $reject) {
$reject(null);
});
try {
React\Async\await($promise);
} catch (\Exception $e) {
// no-op
}
unset($promise, $e);
$this->assertEquals(0, gc_collect_cycles());
}
public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
{
if (method_exists($this, 'expectException')) {
// PHPUnit 5+
$this->expectException($exception);
if ($exceptionMessage !== '') {
$this->expectExceptionMessage($exceptionMessage);
}
if ($exceptionCode !== null) {
$this->expectExceptionCode($exceptionCode);
}
} else {
// legacy PHPUnit 4
parent::setExpectedException($exception, $exceptionMessage, $exceptionCode);
}
}
}