1111use function React \Promise \all ;
1212use function React \Promise \reject ;
1313use function React \Promise \resolve ;
14- use function React \Promise \Timer \sleep ;
1514
1615class AsyncTest extends TestCase
1716{
@@ -187,57 +186,70 @@ public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsA
187186 $ this ->assertLessThan (0.12 , $ time );
188187 }
189188
190- public function testCancel ()
189+ public function testCancelAsyncWillReturnRejectedPromiseWhenCancellingPendingPromiseRejects ()
191190 {
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+ })();
195196
196- $ promise = async (static function (): int {
197- echo 'a ' ;
198- await (sleep (2 ));
199- echo 'b ' ;
197+ $ promise ->cancel ();
200198
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+ }
202212 })();
203213
204214 $ promise ->cancel ();
205- await ($ promise );
215+
216+ $ promise ->then ($ this ->expectCallableOnceWith (42 ));
206217 }
207218
208- public function testCancelTryCatch ()
219+ public function testCancelAsycWillReturnPendigPromiseWhenCancellingFirstPromiseRejectsInsideCatchThatAwaitsSecondPromise ()
209220 {
210- self ::expectOutputString ('ab ' );
211-
212- $ promise = async (static function (): int {
213- echo 'a ' ;
221+ $ promise = async (function () {
214222 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+ }));
218230 }
219- echo 'b ' ;
220-
221- return time ();
222231 })();
223232
224233 $ promise ->cancel ();
225- await ($ promise );
234+
235+ $ promise ->then ($ this ->expectCallableNever (), $ this ->expectCallableNever ());
226236 }
227237
228- public function testNestedCancel ()
238+ public function testCancelAsyncWillCancelNestedAwait ()
229239 {
230240 self ::expectOutputString ('abc ' );
231- $ this ->expectException (\Exception ::class);
232- $ this ->expectExceptionMessage ('Timer cancelled ' );
241+ $ this ->expectException (\RuntimeException ::class);
242+ $ this ->expectExceptionMessage ('Operation cancelled ' );
233243
234244 $ promise = async (static function (): int {
235245 echo 'a ' ;
236246 await (async (static function (): void {
237247 echo 'b ' ;
238248 await (async (static function (): void {
239249 echo 'c ' ;
240- await (sleep (2 ));
250+ await (new Promise (function () { }, function () {
251+ throw new \RuntimeException ('Operation cancelled ' );
252+ }));
241253 echo 'd ' ;
242254 })());
243255 echo 'e ' ;
@@ -250,44 +262,4 @@ public function testNestedCancel()
250262 $ promise ->cancel ();
251263 await ($ promise );
252264 }
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- }
293265}
0 commit comments