@@ -775,6 +775,126 @@ describe('Mock Timers Test Suite', () => {
775
775
} ) ;
776
776
} ) ;
777
777
778
+ describe ( 'scheduler Suite' , ( ) => {
779
+ describe ( 'scheduler.wait' , ( ) => {
780
+ it ( 'should advance in time and trigger timers when calling the .tick function' , ( t ) => {
781
+ t . mock . timers . enable ( { apis : [ 'scheduler.wait' ] } ) ;
782
+
783
+ const now = Date . now ( ) ;
784
+ const durationAtMost = 100 ;
785
+
786
+ const p = nodeTimersPromises . scheduler . wait ( 4000 ) ;
787
+ t . mock . timers . tick ( 4000 ) ;
788
+
789
+ return p . then ( common . mustCall ( ( result ) => {
790
+ assert . strictEqual ( result , undefined ) ;
791
+ assert . ok (
792
+ Date . now ( ) - now < durationAtMost ,
793
+ `time should be advanced less than the ${ durationAtMost } ms`
794
+ ) ;
795
+ } ) ) ;
796
+ } ) ;
797
+
798
+ it ( 'should advance in time and trigger timers when calling the .tick function multiple times' , async ( t ) => {
799
+ t . mock . timers . enable ( { apis : [ 'scheduler.wait' ] } ) ;
800
+
801
+ const fn = t . mock . fn ( ) ;
802
+
803
+ nodeTimersPromises . scheduler . wait ( 9999 ) . then ( fn ) ;
804
+
805
+ t . mock . timers . tick ( 8999 ) ;
806
+ assert . strictEqual ( fn . mock . callCount ( ) , 0 ) ;
807
+ t . mock . timers . tick ( 500 ) ;
808
+
809
+ await nodeTimersPromises . setImmediate ( ) ;
810
+
811
+ assert . strictEqual ( fn . mock . callCount ( ) , 0 ) ;
812
+ t . mock . timers . tick ( 500 ) ;
813
+
814
+ await nodeTimersPromises . setImmediate ( ) ;
815
+ assert . strictEqual ( fn . mock . callCount ( ) , 1 ) ;
816
+ } ) ;
817
+
818
+ it ( 'should work with the same params as the original timers/promises/scheduler.wait' , async ( t ) => {
819
+ t . mock . timers . enable ( { apis : [ 'scheduler.wait' ] } ) ;
820
+ const controller = new AbortController ( ) ;
821
+ const p = nodeTimersPromises . scheduler . wait ( 2000 , {
822
+ ref : true ,
823
+ signal : controller . signal ,
824
+ } ) ;
825
+
826
+ t . mock . timers . tick ( 1000 ) ;
827
+ t . mock . timers . tick ( 500 ) ;
828
+ t . mock . timers . tick ( 500 ) ;
829
+ t . mock . timers . tick ( 500 ) ;
830
+
831
+ const result = await p ;
832
+ assert . strictEqual ( result , undefined ) ;
833
+ } ) ;
834
+
835
+ it ( 'should abort operation if timers/promises/scheduler.wait received an aborted signal' , async ( t ) => {
836
+ t . mock . timers . enable ( { apis : [ 'scheduler.wait' ] } ) ;
837
+ const controller = new AbortController ( ) ;
838
+ const p = nodeTimersPromises . scheduler . wait ( 2000 , {
839
+ ref : true ,
840
+ signal : controller . signal ,
841
+ } ) ;
842
+
843
+ t . mock . timers . tick ( 1000 ) ;
844
+ controller . abort ( ) ;
845
+ t . mock . timers . tick ( 500 ) ;
846
+ t . mock . timers . tick ( 500 ) ;
847
+ t . mock . timers . tick ( 500 ) ;
848
+
849
+ await assert . rejects ( ( ) => p , {
850
+ name : 'AbortError' ,
851
+ } ) ;
852
+ } ) ;
853
+ it ( 'should abort operation even if the .tick was not called' , async ( t ) => {
854
+ t . mock . timers . enable ( { apis : [ 'scheduler.wait' ] } ) ;
855
+ const controller = new AbortController ( ) ;
856
+ const p = nodeTimersPromises . scheduler . wait ( 2000 , {
857
+ ref : true ,
858
+ signal : controller . signal ,
859
+ } ) ;
860
+
861
+ controller . abort ( ) ;
862
+
863
+ await assert . rejects ( ( ) => p , {
864
+ name : 'AbortError' ,
865
+ } ) ;
866
+ } ) ;
867
+
868
+ it ( 'should abort operation when .abort is called before calling setInterval' , async ( t ) => {
869
+ t . mock . timers . enable ( { apis : [ 'scheduler.wait' ] } ) ;
870
+ const controller = new AbortController ( ) ;
871
+ controller . abort ( ) ;
872
+ const p = nodeTimersPromises . scheduler . wait ( 2000 , {
873
+ ref : true ,
874
+ signal : controller . signal ,
875
+ } ) ;
876
+
877
+ await assert . rejects ( ( ) => p , {
878
+ name : 'AbortError' ,
879
+ } ) ;
880
+ } ) ;
881
+
882
+ it ( 'should reject given an an invalid signal instance' , async ( t ) => {
883
+ t . mock . timers . enable ( { apis : [ 'scheduler.wait' ] } ) ;
884
+ const p = nodeTimersPromises . scheduler . wait ( 2000 , {
885
+ ref : true ,
886
+ signal : { } ,
887
+ } ) ;
888
+
889
+ await assert . rejects ( ( ) => p , {
890
+ name : 'TypeError' ,
891
+ code : 'ERR_INVALID_ARG_TYPE' ,
892
+ } ) ;
893
+ } ) ;
894
+
895
+ } ) ;
896
+ } ) ;
897
+
778
898
describe ( 'Date Suite' , ( ) => {
779
899
it ( 'should return the initial UNIX epoch if not specified' , ( t ) => {
780
900
t . mock . timers . enable ( { apis : [ 'Date' ] } ) ;
0 commit comments