|
4 | 4 |
|
5 | 5 | use React;
|
6 | 6 | use React\EventLoop\Loop;
|
| 7 | +use React\Promise\Deferred; |
7 | 8 | use React\Promise\Promise;
|
8 | 9 |
|
9 | 10 | class AwaitTest extends TestCase
|
@@ -43,6 +44,30 @@ public function testAwaitThrowsExceptionWithoutRunningLoop(callable $await)
|
43 | 44 | }
|
44 | 45 | }
|
45 | 46 |
|
| 47 | + /** |
| 48 | + * @dataProvider provideAwaiters |
| 49 | + */ |
| 50 | + public function testAwaitThrowsExceptionImmediatelyWhenPromiseIsRejected(callable $await) |
| 51 | + { |
| 52 | + $deferred = new Deferred(); |
| 53 | + |
| 54 | + $ticks = 0; |
| 55 | + Loop::futureTick(function () use (&$ticks) { |
| 56 | + ++$ticks; |
| 57 | + Loop::futureTick(function () use (&$ticks) { |
| 58 | + ++$ticks; |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + Loop::futureTick(fn() => $deferred->reject(new \RuntimeException())); |
| 63 | + |
| 64 | + try { |
| 65 | + $await($deferred->promise()); |
| 66 | + } catch (\RuntimeException $e) { |
| 67 | + $this->assertEquals(1, $ticks); |
| 68 | + } |
| 69 | + } |
| 70 | + |
46 | 71 | /**
|
47 | 72 | * @dataProvider provideAwaiters
|
48 | 73 | */
|
@@ -130,6 +155,27 @@ public function testAwaitReturnsValueImmediatelyWithoutRunningLoop(callable $awa
|
130 | 155 | $this->assertTrue($now);
|
131 | 156 | }
|
132 | 157 |
|
| 158 | + /** |
| 159 | + * @dataProvider provideAwaiters |
| 160 | + */ |
| 161 | + public function testAwaitReturnsValueImmediatelyWhenPromiseIsFulfilled(callable $await) |
| 162 | + { |
| 163 | + $deferred = new Deferred(); |
| 164 | + |
| 165 | + $ticks = 0; |
| 166 | + Loop::futureTick(function () use (&$ticks) { |
| 167 | + ++$ticks; |
| 168 | + Loop::futureTick(function () use (&$ticks) { |
| 169 | + ++$ticks; |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + Loop::futureTick(fn() => $deferred->resolve(42)); |
| 174 | + |
| 175 | + $this->assertEquals(42, $await($deferred->promise())); |
| 176 | + $this->assertEquals(1, $ticks); |
| 177 | + } |
| 178 | + |
133 | 179 | /**
|
134 | 180 | * @dataProvider provideAwaiters
|
135 | 181 | */
|
|
0 commit comments