@@ -347,6 +347,62 @@ const runBasicTests = (testSuiteTitle, TESTPAGE_URL) => {
347
347
} ) ;
348
348
expect ( signalReason ) . toEqual ( 'My reason' ) ;
349
349
} ) ;
350
+
351
+ it ( 'throws if the signal is aborted' , async ( ) => {
352
+ await browser . url ( TESTPAGE_URL ) ;
353
+ const result = await browser . executeAsync ( async ( done ) => {
354
+ const controller = new AbortController ( ) ;
355
+ controller . abort ( new Error ( 'My reason' ) ) ;
356
+ try {
357
+ controller . signal . throwIfAborted ( ) ;
358
+ done ( 'FAIL' ) ;
359
+ } catch ( error ) {
360
+ done ( [ error . name , error . message ] + '' ) ;
361
+ }
362
+ } ) ;
363
+ expect ( result ) . toBe ( 'AbortError,My reason' ) ;
364
+ } ) ;
365
+
366
+ it ( 'makes a new signal with a timeout' , async ( ) => {
367
+ await browser . url ( TESTPAGE_URL ) ;
368
+ const result = await browser . executeAsync ( async ( done ) => {
369
+ const signal = new AbortSignal . timeout ( 100 ) ;
370
+ setTimeout ( ( ) => {
371
+ if ( signal . aborted ) done ( 'PASS' ) ;
372
+ else done ( 'FAIL' ) ;
373
+ } , 100 ) ;
374
+ } ) ;
375
+ expect ( result ) . toBe ( 'PASS' ) ;
376
+ } ) ;
377
+
378
+ it ( 'aborted the new signal immediately if one of source signals is aborted already' , async ( ) => {
379
+ await browser . url ( TESTPAGE_URL ) ;
380
+ const result = await browser . executeAsync ( async ( done ) => {
381
+ const controller1 = new AbortController ( ) ,
382
+ controller2 = new AbortController ( ) ;
383
+ controller1 . abort ( ) ;
384
+
385
+ const signal = AbortSignal . any ( [ controller1 . signal , controller2 . signal ] ) ;
386
+ if ( signal . aborted ) done ( 'PASS' ) ;
387
+ else done ( 'FAIL' ) ;
388
+ } ) ;
389
+ expect ( result ) . toBe ( 'PASS' ) ;
390
+ } ) ;
391
+
392
+ it ( 'aborted the new signal asynchronously if one of source signals is aborted later' , async ( ) => {
393
+ await browser . url ( TESTPAGE_URL ) ;
394
+ const result = await browser . executeAsync ( async ( done ) => {
395
+ const controller1 = new AbortController ( ) ,
396
+ controller2 = new AbortController ( ) ;
397
+ const signal = AbortSignal . any ( [ controller1 . signal , controller2 . signal ] ) ;
398
+ setTimeout ( ( ) => controller2 . abort ( 'Controller 2 is aborted' ) , 100 ) ;
399
+ setTimeout ( ( ) => {
400
+ if ( signal . aborted ) done ( signal . reason ) ;
401
+ else done ( 'FAIL' ) ;
402
+ } , 100 ) ;
403
+ } ) ;
404
+ expect ( result ) . toBe ( 'Controller 2 is aborted' ) ;
405
+ } ) ;
350
406
} ) ;
351
407
} ;
352
408
0 commit comments