11
11
use function React \Promise \all ;
12
12
use function React \Promise \reject ;
13
13
use function React \Promise \resolve ;
14
- use function React \Promise \Timer \sleep ;
15
14
16
15
class AsyncTest extends TestCase
17
16
{
@@ -187,57 +186,70 @@ public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsA
187
186
$ this ->assertLessThan (0.12 , $ time );
188
187
}
189
188
190
- public function testCancel ()
189
+ public function testCancelAsyncWillReturnRejectedPromiseWhenCancellingPendingPromiseRejects ()
191
190
{
192
- self ::expectOutputString ('a ' );
193
- $ this ->expectException (\Exception::class);
194
- $ this ->expectExceptionMessage ('Timer cancelled ' );
191
+ $ promise = async (function () {
192
+ await (new Promise (function () { }, function () {
193
+ throw new \RuntimeException ('Operation cancelled ' );
194
+ }));
195
+ })();
195
196
196
- $ promise = async (static function (): int {
197
- echo 'a ' ;
198
- await (sleep (2 ));
199
- echo 'b ' ;
197
+ $ promise ->cancel ();
200
198
201
- return time ();
199
+ $ promise ->then (null , $ this ->expectCallableOnceWith (new \RuntimeException ('Operation cancelled ' )));
200
+ }
201
+
202
+ public function testCancelAsyncWillReturnFulfilledPromiseWhenCancellingPendingPromiseRejectsInsideCatchThatReturnsValue ()
203
+ {
204
+ $ promise = async (function () {
205
+ try {
206
+ await (new Promise (function () { }, function () {
207
+ throw new \RuntimeException ('Operation cancelled ' );
208
+ }));
209
+ } catch (\RuntimeException $ e ) {
210
+ return 42 ;
211
+ }
202
212
})();
203
213
204
214
$ promise ->cancel ();
205
- await ($ promise );
215
+
216
+ $ promise ->then ($ this ->expectCallableOnceWith (42 ));
206
217
}
207
218
208
- public function testCancelTryCatch ()
219
+ public function testCancelAsycWillReturnPendigPromiseWhenCancellingFirstPromiseRejectsInsideCatchThatAwaitsSecondPromise ()
209
220
{
210
- self ::expectOutputString ('ab ' );
211
-
212
- $ promise = async (static function (): int {
213
- echo 'a ' ;
221
+ $ promise = async (function () {
214
222
try {
215
- await (sleep (2 ));
216
- } catch (\Throwable ) {
217
- // No-Op
223
+ await (new Promise (function () { }, function () {
224
+ throw new \RuntimeException ('First operation cancelled ' );
225
+ }));
226
+ } catch (\RuntimeException $ e ) {
227
+ await (new Promise (function () { }, function () {
228
+ throw new \RuntimeException ('Second operation never cancelled ' );
229
+ }));
218
230
}
219
- echo 'b ' ;
220
-
221
- return time ();
222
231
})();
223
232
224
233
$ promise ->cancel ();
225
- await ($ promise );
234
+
235
+ $ promise ->then ($ this ->expectCallableNever (), $ this ->expectCallableNever ());
226
236
}
227
237
228
- public function testNestedCancel ()
238
+ public function testCancelAsyncWillCancelNestedAwait ()
229
239
{
230
240
self ::expectOutputString ('abc ' );
231
- $ this ->expectException (\Exception ::class);
232
- $ this ->expectExceptionMessage ('Timer cancelled ' );
241
+ $ this ->expectException (\RuntimeException ::class);
242
+ $ this ->expectExceptionMessage ('Operation cancelled ' );
233
243
234
244
$ promise = async (static function (): int {
235
245
echo 'a ' ;
236
246
await (async (static function (): void {
237
247
echo 'b ' ;
238
248
await (async (static function (): void {
239
249
echo 'c ' ;
240
- await (sleep (2 ));
250
+ await (new Promise (function () { }, function () {
251
+ throw new \RuntimeException ('Operation cancelled ' );
252
+ }));
241
253
echo 'd ' ;
242
254
})());
243
255
echo 'e ' ;
@@ -250,44 +262,4 @@ public function testNestedCancel()
250
262
$ promise ->cancel ();
251
263
await ($ promise );
252
264
}
253
-
254
- public function testCancelFiberThatCatchesExceptions ()
255
- {
256
- self ::expectOutputString ('ab ' );
257
- $ this ->expectException (\Exception::class);
258
- $ this ->expectExceptionMessage ('Timer cancelled ' );
259
-
260
- $ promise = async (static function (): int {
261
- echo 'a ' ;
262
- try {
263
- await (sleep (2 ));
264
- } catch (\Throwable ) {
265
- // No-Op
266
- }
267
- echo 'b ' ;
268
- await (sleep (0.1 ));
269
- echo 'c ' ;
270
-
271
- return time ();
272
- })();
273
-
274
- $ promise ->cancel ();
275
- await ($ promise );
276
- }
277
-
278
- public function testNotAwaitedPromiseWillNotBeCanceled ()
279
- {
280
- self ::expectOutputString ('acb ' );
281
-
282
- async (static function (): int {
283
- echo 'a ' ;
284
- sleep (0.001 )->then (static function (): void {
285
- echo 'b ' ;
286
- });
287
- echo 'c ' ;
288
-
289
- return time ();
290
- })()->cancel ();
291
- Loop::run ();
292
- }
293
265
}
0 commit comments