@@ -479,6 +479,59 @@ describe('Mock Timers Test Suite', () => {
479
479
code : 'ERR_INVALID_ARG_TYPE' ,
480
480
} ) ;
481
481
} ) ;
482
+
483
+ // Test for https://github.com/nodejs/node/issues/50365
484
+ it ( 'should not affect other timers when aborting' , async ( t ) => {
485
+ const f1 = t . mock . fn ( ) ;
486
+ const f2 = t . mock . fn ( ) ;
487
+ t . mock . timers . enable ( { apis : [ 'setTimeout' ] } ) ;
488
+ const ac = new AbortController ( ) ;
489
+
490
+ // id 1 & pos 1 in priority queue
491
+ nodeTimersPromises . setTimeout ( 100 , undefined , { signal : ac . signal } ) . then ( f1 , f1 ) ;
492
+ // id 2 & pos 1 in priority queue (id 1 is moved to pos 2)
493
+ nodeTimersPromises . setTimeout ( 50 ) . then ( f2 , f2 ) ;
494
+
495
+ ac . abort ( ) ; // BUG: will remove timer at pos 1 not timer with id 1!
496
+
497
+ t . mock . timers . runAll ( ) ;
498
+ await nodeTimersPromises . setImmediate ( ) ; // let promises settle
499
+
500
+ // First setTimeout is aborted
501
+ assert . strictEqual ( f1 . mock . callCount ( ) , 1 ) ;
502
+ assert . strictEqual ( f1 . mock . calls [ 0 ] . arguments [ 0 ] . code , 'ABORT_ERR' ) ;
503
+
504
+ // Second setTimeout should resolve, but never settles, because it was eronously removed by ac.abort()
505
+ assert . strictEqual ( f2 . mock . callCount ( ) , 1 ) ;
506
+ } ) ;
507
+
508
+ // Test for https://github.com/nodejs/node/issues/50365
509
+ it ( 'should not affect other timers when aborted after triggering' , async ( t ) => {
510
+ const f1 = t . mock . fn ( ) ;
511
+ const f2 = t . mock . fn ( ) ;
512
+ t . mock . timers . enable ( { apis : [ 'setTimeout' ] } ) ;
513
+ const ac = new AbortController ( ) ;
514
+
515
+ // id 1 & pos 1 in priority queue
516
+ nodeTimersPromises . setTimeout ( 50 , true , { signal : ac . signal } ) . then ( f1 , f1 ) ;
517
+ // id 2 & pos 2 in priority queue
518
+ nodeTimersPromises . setTimeout ( 100 ) . then ( f2 , f2 ) ;
519
+
520
+ // First setTimeout resolves
521
+ t . mock . timers . tick ( 50 ) ;
522
+ await nodeTimersPromises . setImmediate ( ) ; // let promises settle
523
+ assert . strictEqual ( f1 . mock . callCount ( ) , 1 ) ;
524
+ assert . strictEqual ( f1 . mock . calls [ 0 ] . arguments . length , 1 ) ;
525
+ assert . strictEqual ( f1 . mock . calls [ 0 ] . arguments [ 0 ] , true ) ;
526
+
527
+ // Now timer with id 2 will be at pos 1 in priority queue
528
+ ac . abort ( ) ; // BUG: will remove timer at pos 1 not timer with id 1!
529
+
530
+ // Second setTimeout should resolve, but never settles, because it was eronously removed by ac.abort()
531
+ t . mock . timers . runAll ( ) ;
532
+ await nodeTimersPromises . setImmediate ( ) ; // let promises settle
533
+ assert . strictEqual ( f2 . mock . callCount ( ) , 1 ) ;
534
+ } ) ;
482
535
} ) ;
483
536
484
537
describe ( 'setInterval Suite' , ( ) => {
@@ -626,6 +679,38 @@ describe('Mock Timers Test Suite', () => {
626
679
} ) ;
627
680
assert . strictEqual ( numIterations , expectedIterations ) ;
628
681
} ) ;
682
+
683
+ // Test for https://github.com/nodejs/node/issues/50381
684
+ it ( 'should use the mocked interval' , ( t ) => {
685
+ t . mock . timers . enable ( { apis : [ 'setInterval' ] } ) ;
686
+ const fn = t . mock . fn ( ) ;
687
+ setInterval ( fn , 1000 ) ;
688
+ assert . strictEqual ( fn . mock . callCount ( ) , 0 ) ;
689
+ t . mock . timers . tick ( 1000 ) ;
690
+ assert . strictEqual ( fn . mock . callCount ( ) , 1 ) ;
691
+ t . mock . timers . tick ( 1 ) ;
692
+ t . mock . timers . tick ( 1 ) ;
693
+ t . mock . timers . tick ( 1 ) ;
694
+ assert . strictEqual ( fn . mock . callCount ( ) , 1 ) ;
695
+ } ) ;
696
+
697
+ // Test for https://github.com/nodejs/node/issues/50382
698
+ it ( 'should not prevent due timers to be processed' , async ( t ) => {
699
+ t . mock . timers . enable ( { apis : [ 'setInterval' , 'setTimeout' ] } ) ;
700
+ const f1 = t . mock . fn ( ) ;
701
+ const f2 = t . mock . fn ( ) ;
702
+
703
+ setInterval ( f1 , 1000 ) ;
704
+ setTimeout ( f2 , 1001 ) ;
705
+
706
+ assert . strictEqual ( f1 . mock . callCount ( ) , 0 ) ;
707
+ assert . strictEqual ( f2 . mock . callCount ( ) , 0 ) ;
708
+
709
+ t . mock . timers . tick ( 1001 ) ;
710
+
711
+ assert . strictEqual ( f1 . mock . callCount ( ) , 1 ) ;
712
+ assert . strictEqual ( f2 . mock . callCount ( ) , 1 ) ;
713
+ } ) ;
629
714
} ) ;
630
715
} ) ;
631
716
} ) ;
0 commit comments