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