11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciprocess.env.NODE_TEST_KNOWN_GLOBALS = 0;
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst assert = require('node:assert');
61cb0ef41Sopenharmony_ciconst { it, mock, describe } = require('node:test');
71cb0ef41Sopenharmony_ciconst nodeTimers = require('node:timers');
81cb0ef41Sopenharmony_ciconst nodeTimersPromises = require('node:timers/promises');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cidescribe('Mock Timers Test Suite', () => {
111cb0ef41Sopenharmony_ci  describe('MockTimers API', () => {
121cb0ef41Sopenharmony_ci    it('should throw an error if trying to enable a timer that is not supported', (t) => {
131cb0ef41Sopenharmony_ci      assert.throws(() => {
141cb0ef41Sopenharmony_ci        t.mock.timers.enable(['DOES_NOT_EXIST']);
151cb0ef41Sopenharmony_ci      }, {
161cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_ARG_VALUE',
171cb0ef41Sopenharmony_ci      });
181cb0ef41Sopenharmony_ci    });
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci    it('should throw an error if trying to enable a timer twice', (t) => {
211cb0ef41Sopenharmony_ci      t.mock.timers.enable();
221cb0ef41Sopenharmony_ci      assert.throws(() => {
231cb0ef41Sopenharmony_ci        t.mock.timers.enable();
241cb0ef41Sopenharmony_ci      }, {
251cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_STATE',
261cb0ef41Sopenharmony_ci      });
271cb0ef41Sopenharmony_ci    });
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    it('should not throw if calling reset without enabling timers', (t) => {
301cb0ef41Sopenharmony_ci      t.mock.timers.reset();
311cb0ef41Sopenharmony_ci    });
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    it('should throw an error if calling tick without enabling timers', (t) => {
341cb0ef41Sopenharmony_ci      assert.throws(() => {
351cb0ef41Sopenharmony_ci        t.mock.timers.tick();
361cb0ef41Sopenharmony_ci      }, {
371cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_STATE',
381cb0ef41Sopenharmony_ci      });
391cb0ef41Sopenharmony_ci    });
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    it('should throw an error if calling tick with a negative number', (t) => {
421cb0ef41Sopenharmony_ci      t.mock.timers.enable();
431cb0ef41Sopenharmony_ci      assert.throws(() => {
441cb0ef41Sopenharmony_ci        t.mock.timers.tick(-1);
451cb0ef41Sopenharmony_ci      }, {
461cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_ARG_VALUE',
471cb0ef41Sopenharmony_ci      });
481cb0ef41Sopenharmony_ci    });
491cb0ef41Sopenharmony_ci    it('should check that propertyDescriptor gets back after resetting timers', (t) => {
501cb0ef41Sopenharmony_ci      const getDescriptor = (ctx, fn) => Object.getOwnPropertyDescriptor(ctx, fn);
511cb0ef41Sopenharmony_ci      const getCurrentTimersDescriptors = () => {
521cb0ef41Sopenharmony_ci        const timers = [
531cb0ef41Sopenharmony_ci          'setTimeout',
541cb0ef41Sopenharmony_ci          'clearTimeout',
551cb0ef41Sopenharmony_ci          'setInterval',
561cb0ef41Sopenharmony_ci          'clearInterval',
571cb0ef41Sopenharmony_ci          'setImmediate',
581cb0ef41Sopenharmony_ci          'clearImmediate',
591cb0ef41Sopenharmony_ci        ];
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci        const globalTimersDescriptors = timers.map((fn) => getDescriptor(global, fn));
621cb0ef41Sopenharmony_ci        const nodeTimersDescriptors = timers.map((fn) => getDescriptor(nodeTimers, fn));
631cb0ef41Sopenharmony_ci        const nodeTimersPromisesDescriptors = timers
641cb0ef41Sopenharmony_ci          .filter((fn) => !fn.includes('clear'))
651cb0ef41Sopenharmony_ci          .map((fn) => getDescriptor(nodeTimersPromises, fn));
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci        return {
681cb0ef41Sopenharmony_ci          global: globalTimersDescriptors,
691cb0ef41Sopenharmony_ci          nodeTimers: nodeTimersDescriptors,
701cb0ef41Sopenharmony_ci          nodeTimersPromises: nodeTimersPromisesDescriptors,
711cb0ef41Sopenharmony_ci        };
721cb0ef41Sopenharmony_ci      };
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci      const originalDescriptors = getCurrentTimersDescriptors();
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci      t.mock.timers.enable();
771cb0ef41Sopenharmony_ci      const during = getCurrentTimersDescriptors();
781cb0ef41Sopenharmony_ci      t.mock.timers.reset();
791cb0ef41Sopenharmony_ci      const after = getCurrentTimersDescriptors();
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci      for (const env in originalDescriptors) {
821cb0ef41Sopenharmony_ci        for (const prop in originalDescriptors[env]) {
831cb0ef41Sopenharmony_ci          const originalDescriptor = originalDescriptors[env][prop];
841cb0ef41Sopenharmony_ci          const afterDescriptor = after[env][prop];
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci          assert.deepStrictEqual(
871cb0ef41Sopenharmony_ci            originalDescriptor,
881cb0ef41Sopenharmony_ci            afterDescriptor,
891cb0ef41Sopenharmony_ci          );
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci          assert.notDeepStrictEqual(
921cb0ef41Sopenharmony_ci            originalDescriptor,
931cb0ef41Sopenharmony_ci            during[env][prop],
941cb0ef41Sopenharmony_ci          );
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci          assert.notDeepStrictEqual(
971cb0ef41Sopenharmony_ci            during[env][prop],
981cb0ef41Sopenharmony_ci            after[env][prop],
991cb0ef41Sopenharmony_ci          );
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci        }
1021cb0ef41Sopenharmony_ci      }
1031cb0ef41Sopenharmony_ci    });
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci    it('should reset all timers when calling .reset function', (t) => {
1061cb0ef41Sopenharmony_ci      t.mock.timers.enable();
1071cb0ef41Sopenharmony_ci      const fn = t.mock.fn();
1081cb0ef41Sopenharmony_ci      global.setTimeout(fn, 1000);
1091cb0ef41Sopenharmony_ci      t.mock.timers.reset();
1101cb0ef41Sopenharmony_ci      assert.throws(() => {
1111cb0ef41Sopenharmony_ci        t.mock.timers.tick(1000);
1121cb0ef41Sopenharmony_ci      }, {
1131cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_STATE',
1141cb0ef41Sopenharmony_ci      });
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci      assert.strictEqual(fn.mock.callCount(), 0);
1171cb0ef41Sopenharmony_ci    });
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci    it('should reset all timers when calling Symbol.dispose', (t) => {
1201cb0ef41Sopenharmony_ci      t.mock.timers.enable();
1211cb0ef41Sopenharmony_ci      const fn = t.mock.fn();
1221cb0ef41Sopenharmony_ci      global.setTimeout(fn, 1000);
1231cb0ef41Sopenharmony_ci      // TODO(benjamingr) refactor to `using`
1241cb0ef41Sopenharmony_ci      t.mock.timers[Symbol.dispose]();
1251cb0ef41Sopenharmony_ci      assert.throws(() => {
1261cb0ef41Sopenharmony_ci        t.mock.timers.tick(1000);
1271cb0ef41Sopenharmony_ci      }, {
1281cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_STATE',
1291cb0ef41Sopenharmony_ci      });
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci      assert.strictEqual(fn.mock.callCount(), 0);
1321cb0ef41Sopenharmony_ci    });
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci    it('should execute in order if timeout is the same', (t) => {
1351cb0ef41Sopenharmony_ci      t.mock.timers.enable();
1361cb0ef41Sopenharmony_ci      const order = [];
1371cb0ef41Sopenharmony_ci      const fn1 = t.mock.fn(() => order.push('f1'));
1381cb0ef41Sopenharmony_ci      const fn2 = t.mock.fn(() => order.push('f2'));
1391cb0ef41Sopenharmony_ci      global.setTimeout(fn1, 1000);
1401cb0ef41Sopenharmony_ci      global.setTimeout(fn2, 1000);
1411cb0ef41Sopenharmony_ci      t.mock.timers.tick(1000);
1421cb0ef41Sopenharmony_ci      assert.strictEqual(fn1.mock.callCount(), 1);
1431cb0ef41Sopenharmony_ci      assert.strictEqual(fn2.mock.callCount(), 1);
1441cb0ef41Sopenharmony_ci      assert.deepStrictEqual(order, ['f1', 'f2']);
1451cb0ef41Sopenharmony_ci    });
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci    describe('runAll Suite', () => {
1481cb0ef41Sopenharmony_ci      it('should throw an error if calling runAll without enabling timers', (t) => {
1491cb0ef41Sopenharmony_ci        assert.throws(() => {
1501cb0ef41Sopenharmony_ci          t.mock.timers.runAll();
1511cb0ef41Sopenharmony_ci        }, {
1521cb0ef41Sopenharmony_ci          code: 'ERR_INVALID_STATE',
1531cb0ef41Sopenharmony_ci        });
1541cb0ef41Sopenharmony_ci      });
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci      it('should trigger all timers when calling .runAll function', async (t) => {
1571cb0ef41Sopenharmony_ci        const timeoutFn = t.mock.fn();
1581cb0ef41Sopenharmony_ci        const intervalFn = t.mock.fn();
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ci        t.mock.timers.enable();
1611cb0ef41Sopenharmony_ci        global.setTimeout(timeoutFn, 1111);
1621cb0ef41Sopenharmony_ci        const id = global.setInterval(intervalFn, 9999);
1631cb0ef41Sopenharmony_ci        t.mock.timers.runAll();
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci        global.clearInterval(id);
1661cb0ef41Sopenharmony_ci        assert.strictEqual(timeoutFn.mock.callCount(), 1);
1671cb0ef41Sopenharmony_ci        assert.strictEqual(intervalFn.mock.callCount(), 1);
1681cb0ef41Sopenharmony_ci      });
1691cb0ef41Sopenharmony_ci    });
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci  });
1721cb0ef41Sopenharmony_ci
1731cb0ef41Sopenharmony_ci  describe('globals/timers', () => {
1741cb0ef41Sopenharmony_ci    describe('setTimeout Suite', () => {
1751cb0ef41Sopenharmony_ci      it('should advance in time and trigger timers when calling the .tick function', (t) => {
1761cb0ef41Sopenharmony_ci        mock.timers.enable(['setTimeout']);
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci        const fn = mock.fn();
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci        global.setTimeout(fn, 4000);
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci        mock.timers.tick(4000);
1831cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 1);
1841cb0ef41Sopenharmony_ci        mock.timers.reset();
1851cb0ef41Sopenharmony_ci      });
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_ci      it('should advance in time and trigger timers when calling the .tick function multiple times', (t) => {
1881cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setTimeout']);
1891cb0ef41Sopenharmony_ci        const fn = t.mock.fn();
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ci        global.setTimeout(fn, 2000);
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ci        t.mock.timers.tick(1000);
1941cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 0);
1951cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
1961cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 0);
1971cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
1981cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 1);
1991cb0ef41Sopenharmony_ci      });
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci      it('should work with the same params as the original setTimeout', (t) => {
2021cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setTimeout']);
2031cb0ef41Sopenharmony_ci        const fn = t.mock.fn();
2041cb0ef41Sopenharmony_ci        const args = ['a', 'b', 'c'];
2051cb0ef41Sopenharmony_ci        global.setTimeout(fn, 2000, ...args);
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci        t.mock.timers.tick(1000);
2081cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
2091cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 1);
2121cb0ef41Sopenharmony_ci        assert.deepStrictEqual(fn.mock.calls[0].arguments, args);
2131cb0ef41Sopenharmony_ci      });
2141cb0ef41Sopenharmony_ci
2151cb0ef41Sopenharmony_ci      it('should keep setTimeout working if timers are disabled', (t, done) => {
2161cb0ef41Sopenharmony_ci        const now = Date.now();
2171cb0ef41Sopenharmony_ci        const timeout = 2;
2181cb0ef41Sopenharmony_ci        const expected = () => now - timeout;
2191cb0ef41Sopenharmony_ci        global.setTimeout(common.mustCall(() => {
2201cb0ef41Sopenharmony_ci          assert.strictEqual(now - timeout, expected());
2211cb0ef41Sopenharmony_ci          done();
2221cb0ef41Sopenharmony_ci        }), timeout);
2231cb0ef41Sopenharmony_ci      });
2241cb0ef41Sopenharmony_ci
2251cb0ef41Sopenharmony_ci    });
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ci    describe('clearTimeout Suite', () => {
2281cb0ef41Sopenharmony_ci      it('should not advance in time if clearTimeout was invoked', (t) => {
2291cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setTimeout']);
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ci        const fn = mock.fn();
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_ci        const id = global.setTimeout(fn, 4000);
2341cb0ef41Sopenharmony_ci        global.clearTimeout(id);
2351cb0ef41Sopenharmony_ci        t.mock.timers.tick(4000);
2361cb0ef41Sopenharmony_ci
2371cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 0);
2381cb0ef41Sopenharmony_ci      });
2391cb0ef41Sopenharmony_ci    });
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ci    describe('setInterval Suite', () => {
2421cb0ef41Sopenharmony_ci      it('should tick three times using fake setInterval', (t) => {
2431cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setInterval']);
2441cb0ef41Sopenharmony_ci        const fn = t.mock.fn();
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ci        const id = global.setInterval(fn, 200);
2471cb0ef41Sopenharmony_ci
2481cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
2491cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
2501cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
2511cb0ef41Sopenharmony_ci
2521cb0ef41Sopenharmony_ci        global.clearInterval(id);
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 3);
2551cb0ef41Sopenharmony_ci      });
2561cb0ef41Sopenharmony_ci
2571cb0ef41Sopenharmony_ci      it('should work with the same params as the original setInterval', (t) => {
2581cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setInterval']);
2591cb0ef41Sopenharmony_ci        const fn = t.mock.fn();
2601cb0ef41Sopenharmony_ci        const args = ['a', 'b', 'c'];
2611cb0ef41Sopenharmony_ci        const id = global.setInterval(fn, 200, ...args);
2621cb0ef41Sopenharmony_ci
2631cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
2641cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
2651cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
2661cb0ef41Sopenharmony_ci
2671cb0ef41Sopenharmony_ci        global.clearInterval(id);
2681cb0ef41Sopenharmony_ci
2691cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 3);
2701cb0ef41Sopenharmony_ci        assert.deepStrictEqual(fn.mock.calls[0].arguments, args);
2711cb0ef41Sopenharmony_ci        assert.deepStrictEqual(fn.mock.calls[1].arguments, args);
2721cb0ef41Sopenharmony_ci        assert.deepStrictEqual(fn.mock.calls[2].arguments, args);
2731cb0ef41Sopenharmony_ci
2741cb0ef41Sopenharmony_ci      });
2751cb0ef41Sopenharmony_ci    });
2761cb0ef41Sopenharmony_ci
2771cb0ef41Sopenharmony_ci    describe('clearInterval Suite', () => {
2781cb0ef41Sopenharmony_ci      it('should not advance in time if clearInterval was invoked', (t) => {
2791cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setInterval']);
2801cb0ef41Sopenharmony_ci
2811cb0ef41Sopenharmony_ci        const fn = mock.fn();
2821cb0ef41Sopenharmony_ci        const id = global.setInterval(fn, 200);
2831cb0ef41Sopenharmony_ci        global.clearInterval(id);
2841cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
2851cb0ef41Sopenharmony_ci
2861cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 0);
2871cb0ef41Sopenharmony_ci      });
2881cb0ef41Sopenharmony_ci    });
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_ci    describe('setImmediate Suite', () => {
2911cb0ef41Sopenharmony_ci      it('should keep setImmediate working if timers are disabled', (t, done) => {
2921cb0ef41Sopenharmony_ci        const now = Date.now();
2931cb0ef41Sopenharmony_ci        const timeout = 2;
2941cb0ef41Sopenharmony_ci        const expected = () => now - timeout;
2951cb0ef41Sopenharmony_ci        global.setImmediate(common.mustCall(() => {
2961cb0ef41Sopenharmony_ci          assert.strictEqual(now - timeout, expected());
2971cb0ef41Sopenharmony_ci          done();
2981cb0ef41Sopenharmony_ci        }));
2991cb0ef41Sopenharmony_ci      });
3001cb0ef41Sopenharmony_ci
3011cb0ef41Sopenharmony_ci      it('should work with the same params as the original setImmediate', (t) => {
3021cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate']);
3031cb0ef41Sopenharmony_ci        const fn = t.mock.fn();
3041cb0ef41Sopenharmony_ci        const args = ['a', 'b', 'c'];
3051cb0ef41Sopenharmony_ci        global.setImmediate(fn, ...args);
3061cb0ef41Sopenharmony_ci        t.mock.timers.tick(9999);
3071cb0ef41Sopenharmony_ci
3081cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 1);
3091cb0ef41Sopenharmony_ci        assert.deepStrictEqual(fn.mock.calls[0].arguments, args);
3101cb0ef41Sopenharmony_ci      });
3111cb0ef41Sopenharmony_ci
3121cb0ef41Sopenharmony_ci      it('should not advance in time if clearImmediate was invoked', (t) => {
3131cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate']);
3141cb0ef41Sopenharmony_ci
3151cb0ef41Sopenharmony_ci        const id = global.setImmediate(common.mustNotCall());
3161cb0ef41Sopenharmony_ci        global.clearImmediate(id);
3171cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
3181cb0ef41Sopenharmony_ci      });
3191cb0ef41Sopenharmony_ci
3201cb0ef41Sopenharmony_ci      it('should advance in time and trigger timers when calling the .tick function', (t) => {
3211cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate']);
3221cb0ef41Sopenharmony_ci        global.setImmediate(common.mustCall(1));
3231cb0ef41Sopenharmony_ci        t.mock.timers.tick(0);
3241cb0ef41Sopenharmony_ci      });
3251cb0ef41Sopenharmony_ci
3261cb0ef41Sopenharmony_ci      it('should execute in order if setImmediate is called multiple times', (t) => {
3271cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate']);
3281cb0ef41Sopenharmony_ci        const order = [];
3291cb0ef41Sopenharmony_ci        const fn1 = t.mock.fn(common.mustCall(() => order.push('f1'), 1));
3301cb0ef41Sopenharmony_ci        const fn2 = t.mock.fn(common.mustCall(() => order.push('f2'), 1));
3311cb0ef41Sopenharmony_ci
3321cb0ef41Sopenharmony_ci        global.setImmediate(fn1);
3331cb0ef41Sopenharmony_ci        global.setImmediate(fn2);
3341cb0ef41Sopenharmony_ci
3351cb0ef41Sopenharmony_ci        t.mock.timers.tick(0);
3361cb0ef41Sopenharmony_ci
3371cb0ef41Sopenharmony_ci        assert.deepStrictEqual(order, ['f1', 'f2']);
3381cb0ef41Sopenharmony_ci      });
3391cb0ef41Sopenharmony_ci
3401cb0ef41Sopenharmony_ci      it('should execute setImmediate first if setTimeout was also called', (t) => {
3411cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate', 'setTimeout']);
3421cb0ef41Sopenharmony_ci        const order = [];
3431cb0ef41Sopenharmony_ci        const fn1 = t.mock.fn(common.mustCall(() => order.push('f1'), 1));
3441cb0ef41Sopenharmony_ci        const fn2 = t.mock.fn(common.mustCall(() => order.push('f2'), 1));
3451cb0ef41Sopenharmony_ci
3461cb0ef41Sopenharmony_ci        global.setTimeout(fn2, 0);
3471cb0ef41Sopenharmony_ci        global.setImmediate(fn1);
3481cb0ef41Sopenharmony_ci
3491cb0ef41Sopenharmony_ci        t.mock.timers.tick(100);
3501cb0ef41Sopenharmony_ci
3511cb0ef41Sopenharmony_ci        assert.deepStrictEqual(order, ['f1', 'f2']);
3521cb0ef41Sopenharmony_ci      });
3531cb0ef41Sopenharmony_ci    });
3541cb0ef41Sopenharmony_ci  });
3551cb0ef41Sopenharmony_ci
3561cb0ef41Sopenharmony_ci  describe('timers Suite', () => {
3571cb0ef41Sopenharmony_ci    describe('setTimeout Suite', () => {
3581cb0ef41Sopenharmony_ci      it('should advance in time and trigger timers when calling the .tick function multiple times', (t) => {
3591cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setTimeout']);
3601cb0ef41Sopenharmony_ci        const fn = t.mock.fn();
3611cb0ef41Sopenharmony_ci        const { setTimeout } = nodeTimers;
3621cb0ef41Sopenharmony_ci        setTimeout(fn, 2000);
3631cb0ef41Sopenharmony_ci
3641cb0ef41Sopenharmony_ci        t.mock.timers.tick(1000);
3651cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
3661cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
3671cb0ef41Sopenharmony_ci
3681cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 1);
3691cb0ef41Sopenharmony_ci      });
3701cb0ef41Sopenharmony_ci
3711cb0ef41Sopenharmony_ci      it('should work with the same params as the original timers.setTimeout', (t) => {
3721cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setTimeout']);
3731cb0ef41Sopenharmony_ci        const fn = t.mock.fn();
3741cb0ef41Sopenharmony_ci        const { setTimeout } = nodeTimers;
3751cb0ef41Sopenharmony_ci        const args = ['a', 'b', 'c'];
3761cb0ef41Sopenharmony_ci        setTimeout(fn, 2000, ...args);
3771cb0ef41Sopenharmony_ci
3781cb0ef41Sopenharmony_ci        t.mock.timers.tick(1000);
3791cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
3801cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
3811cb0ef41Sopenharmony_ci
3821cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 1);
3831cb0ef41Sopenharmony_ci        assert.deepStrictEqual(fn.mock.calls[0].arguments, args);
3841cb0ef41Sopenharmony_ci      });
3851cb0ef41Sopenharmony_ci    });
3861cb0ef41Sopenharmony_ci
3871cb0ef41Sopenharmony_ci    describe('clearTimeout Suite', () => {
3881cb0ef41Sopenharmony_ci      it('should not advance in time if clearTimeout was invoked', (t) => {
3891cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setTimeout']);
3901cb0ef41Sopenharmony_ci
3911cb0ef41Sopenharmony_ci        const fn = mock.fn();
3921cb0ef41Sopenharmony_ci        const { setTimeout, clearTimeout } = nodeTimers;
3931cb0ef41Sopenharmony_ci        const id = setTimeout(fn, 2000);
3941cb0ef41Sopenharmony_ci        clearTimeout(id);
3951cb0ef41Sopenharmony_ci        t.mock.timers.tick(2000);
3961cb0ef41Sopenharmony_ci
3971cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 0);
3981cb0ef41Sopenharmony_ci      });
3991cb0ef41Sopenharmony_ci    });
4001cb0ef41Sopenharmony_ci
4011cb0ef41Sopenharmony_ci    describe('setInterval Suite', () => {
4021cb0ef41Sopenharmony_ci      it('should tick three times using fake setInterval', (t) => {
4031cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setInterval']);
4041cb0ef41Sopenharmony_ci        const fn = t.mock.fn();
4051cb0ef41Sopenharmony_ci
4061cb0ef41Sopenharmony_ci        const id = nodeTimers.setInterval(fn, 200);
4071cb0ef41Sopenharmony_ci
4081cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
4091cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
4101cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
4111cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
4121cb0ef41Sopenharmony_ci
4131cb0ef41Sopenharmony_ci        nodeTimers.clearInterval(id);
4141cb0ef41Sopenharmony_ci
4151cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 4);
4161cb0ef41Sopenharmony_ci      });
4171cb0ef41Sopenharmony_ci
4181cb0ef41Sopenharmony_ci      it('should work with the same params as the original timers.setInterval', (t) => {
4191cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setInterval']);
4201cb0ef41Sopenharmony_ci        const fn = t.mock.fn();
4211cb0ef41Sopenharmony_ci        const args = ['a', 'b', 'c'];
4221cb0ef41Sopenharmony_ci        const id = nodeTimers.setInterval(fn, 200, ...args);
4231cb0ef41Sopenharmony_ci
4241cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
4251cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
4261cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
4271cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
4281cb0ef41Sopenharmony_ci
4291cb0ef41Sopenharmony_ci        nodeTimers.clearInterval(id);
4301cb0ef41Sopenharmony_ci
4311cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 4);
4321cb0ef41Sopenharmony_ci        assert.deepStrictEqual(fn.mock.calls[0].arguments, args);
4331cb0ef41Sopenharmony_ci        assert.deepStrictEqual(fn.mock.calls[1].arguments, args);
4341cb0ef41Sopenharmony_ci        assert.deepStrictEqual(fn.mock.calls[2].arguments, args);
4351cb0ef41Sopenharmony_ci        assert.deepStrictEqual(fn.mock.calls[3].arguments, args);
4361cb0ef41Sopenharmony_ci
4371cb0ef41Sopenharmony_ci      });
4381cb0ef41Sopenharmony_ci    });
4391cb0ef41Sopenharmony_ci
4401cb0ef41Sopenharmony_ci    describe('clearInterval Suite', () => {
4411cb0ef41Sopenharmony_ci      it('should not advance in time if clearInterval was invoked', (t) => {
4421cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setInterval']);
4431cb0ef41Sopenharmony_ci
4441cb0ef41Sopenharmony_ci        const fn = mock.fn();
4451cb0ef41Sopenharmony_ci        const { setInterval, clearInterval } = nodeTimers;
4461cb0ef41Sopenharmony_ci        const id = setInterval(fn, 200);
4471cb0ef41Sopenharmony_ci        clearInterval(id);
4481cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
4491cb0ef41Sopenharmony_ci
4501cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 0);
4511cb0ef41Sopenharmony_ci      });
4521cb0ef41Sopenharmony_ci    });
4531cb0ef41Sopenharmony_ci
4541cb0ef41Sopenharmony_ci    describe('setImmediate Suite', () => {
4551cb0ef41Sopenharmony_ci      it('should keep setImmediate working if timers are disabled', (t, done) => {
4561cb0ef41Sopenharmony_ci        const now = Date.now();
4571cb0ef41Sopenharmony_ci        const timeout = 2;
4581cb0ef41Sopenharmony_ci        const expected = () => now - timeout;
4591cb0ef41Sopenharmony_ci        nodeTimers.setImmediate(common.mustCall(() => {
4601cb0ef41Sopenharmony_ci          assert.strictEqual(now - timeout, expected());
4611cb0ef41Sopenharmony_ci          done();
4621cb0ef41Sopenharmony_ci        }, 1));
4631cb0ef41Sopenharmony_ci      });
4641cb0ef41Sopenharmony_ci
4651cb0ef41Sopenharmony_ci      it('should work with the same params as the original setImmediate', (t) => {
4661cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate']);
4671cb0ef41Sopenharmony_ci        const fn = t.mock.fn();
4681cb0ef41Sopenharmony_ci        const args = ['a', 'b', 'c'];
4691cb0ef41Sopenharmony_ci        nodeTimers.setImmediate(fn, ...args);
4701cb0ef41Sopenharmony_ci        t.mock.timers.tick(9999);
4711cb0ef41Sopenharmony_ci
4721cb0ef41Sopenharmony_ci        assert.strictEqual(fn.mock.callCount(), 1);
4731cb0ef41Sopenharmony_ci        assert.deepStrictEqual(fn.mock.calls[0].arguments, args);
4741cb0ef41Sopenharmony_ci      });
4751cb0ef41Sopenharmony_ci
4761cb0ef41Sopenharmony_ci      it('should not advance in time if clearImmediate was invoked', (t) => {
4771cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate']);
4781cb0ef41Sopenharmony_ci
4791cb0ef41Sopenharmony_ci        const id = nodeTimers.setImmediate(common.mustNotCall());
4801cb0ef41Sopenharmony_ci        nodeTimers.clearImmediate(id);
4811cb0ef41Sopenharmony_ci        t.mock.timers.tick(200);
4821cb0ef41Sopenharmony_ci      });
4831cb0ef41Sopenharmony_ci
4841cb0ef41Sopenharmony_ci      it('should advance in time and trigger timers when calling the .tick function', (t) => {
4851cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate']);
4861cb0ef41Sopenharmony_ci        nodeTimers.setImmediate(common.mustCall(1));
4871cb0ef41Sopenharmony_ci        t.mock.timers.tick(0);
4881cb0ef41Sopenharmony_ci      });
4891cb0ef41Sopenharmony_ci
4901cb0ef41Sopenharmony_ci      it('should execute in order if setImmediate is called multiple times', (t) => {
4911cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate']);
4921cb0ef41Sopenharmony_ci        const order = [];
4931cb0ef41Sopenharmony_ci        const fn1 = t.mock.fn(common.mustCall(() => order.push('f1'), 1));
4941cb0ef41Sopenharmony_ci        const fn2 = t.mock.fn(common.mustCall(() => order.push('f2'), 1));
4951cb0ef41Sopenharmony_ci
4961cb0ef41Sopenharmony_ci        nodeTimers.setImmediate(fn1);
4971cb0ef41Sopenharmony_ci        nodeTimers.setImmediate(fn2);
4981cb0ef41Sopenharmony_ci
4991cb0ef41Sopenharmony_ci        t.mock.timers.tick(0);
5001cb0ef41Sopenharmony_ci
5011cb0ef41Sopenharmony_ci        assert.deepStrictEqual(order, ['f1', 'f2']);
5021cb0ef41Sopenharmony_ci      });
5031cb0ef41Sopenharmony_ci
5041cb0ef41Sopenharmony_ci      it('should execute setImmediate first if setTimeout was also called', (t) => {
5051cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate', 'setTimeout']);
5061cb0ef41Sopenharmony_ci        const order = [];
5071cb0ef41Sopenharmony_ci        const fn1 = t.mock.fn(common.mustCall(() => order.push('f1'), 1));
5081cb0ef41Sopenharmony_ci        const fn2 = t.mock.fn(common.mustCall(() => order.push('f2'), 1));
5091cb0ef41Sopenharmony_ci
5101cb0ef41Sopenharmony_ci        nodeTimers.setTimeout(fn2, 0);
5111cb0ef41Sopenharmony_ci        nodeTimers.setImmediate(fn1);
5121cb0ef41Sopenharmony_ci
5131cb0ef41Sopenharmony_ci        t.mock.timers.tick(100);
5141cb0ef41Sopenharmony_ci
5151cb0ef41Sopenharmony_ci        assert.deepStrictEqual(order, ['f1', 'f2']);
5161cb0ef41Sopenharmony_ci      });
5171cb0ef41Sopenharmony_ci    });
5181cb0ef41Sopenharmony_ci  });
5191cb0ef41Sopenharmony_ci
5201cb0ef41Sopenharmony_ci  describe('timers/promises', () => {
5211cb0ef41Sopenharmony_ci    describe('setTimeout Suite', () => {
5221cb0ef41Sopenharmony_ci      it('should advance in time and trigger timers when calling the .tick function multiple times', async (t) => {
5231cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setTimeout']);
5241cb0ef41Sopenharmony_ci
5251cb0ef41Sopenharmony_ci        const p = nodeTimersPromises.setTimeout(2000);
5261cb0ef41Sopenharmony_ci
5271cb0ef41Sopenharmony_ci        t.mock.timers.tick(1000);
5281cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
5291cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
5301cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
5311cb0ef41Sopenharmony_ci
5321cb0ef41Sopenharmony_ci        p.then(common.mustCall((result) => {
5331cb0ef41Sopenharmony_ci          assert.strictEqual(result, undefined);
5341cb0ef41Sopenharmony_ci        }));
5351cb0ef41Sopenharmony_ci      });
5361cb0ef41Sopenharmony_ci
5371cb0ef41Sopenharmony_ci      it('should work with the same params as the original timers/promises/setTimeout', async (t) => {
5381cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setTimeout']);
5391cb0ef41Sopenharmony_ci        const expectedResult = 'result';
5401cb0ef41Sopenharmony_ci        const controller = new AbortController();
5411cb0ef41Sopenharmony_ci        const p = nodeTimersPromises.setTimeout(2000, expectedResult, {
5421cb0ef41Sopenharmony_ci          ref: true,
5431cb0ef41Sopenharmony_ci          signal: controller.signal
5441cb0ef41Sopenharmony_ci        });
5451cb0ef41Sopenharmony_ci
5461cb0ef41Sopenharmony_ci        t.mock.timers.tick(1000);
5471cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
5481cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
5491cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
5501cb0ef41Sopenharmony_ci
5511cb0ef41Sopenharmony_ci        const result = await p;
5521cb0ef41Sopenharmony_ci        assert.strictEqual(result, expectedResult);
5531cb0ef41Sopenharmony_ci      });
5541cb0ef41Sopenharmony_ci
5551cb0ef41Sopenharmony_ci      it('should abort operation if timers/promises/setTimeout received an aborted signal', async (t) => {
5561cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setTimeout']);
5571cb0ef41Sopenharmony_ci        const expectedResult = 'result';
5581cb0ef41Sopenharmony_ci        const controller = new AbortController();
5591cb0ef41Sopenharmony_ci        const p = nodeTimersPromises.setTimeout(2000, expectedResult, {
5601cb0ef41Sopenharmony_ci          ref: true,
5611cb0ef41Sopenharmony_ci          signal: controller.signal
5621cb0ef41Sopenharmony_ci        });
5631cb0ef41Sopenharmony_ci
5641cb0ef41Sopenharmony_ci        t.mock.timers.tick(1000);
5651cb0ef41Sopenharmony_ci        controller.abort();
5661cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
5671cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
5681cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
5691cb0ef41Sopenharmony_ci        await assert.rejects(() => p, {
5701cb0ef41Sopenharmony_ci          name: 'AbortError',
5711cb0ef41Sopenharmony_ci        });
5721cb0ef41Sopenharmony_ci
5731cb0ef41Sopenharmony_ci      });
5741cb0ef41Sopenharmony_ci      it('should abort operation even if the .tick wasn\'t called', async (t) => {
5751cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setTimeout']);
5761cb0ef41Sopenharmony_ci        const expectedResult = 'result';
5771cb0ef41Sopenharmony_ci        const controller = new AbortController();
5781cb0ef41Sopenharmony_ci        const p = nodeTimersPromises.setTimeout(2000, expectedResult, {
5791cb0ef41Sopenharmony_ci          ref: true,
5801cb0ef41Sopenharmony_ci          signal: controller.signal
5811cb0ef41Sopenharmony_ci        });
5821cb0ef41Sopenharmony_ci
5831cb0ef41Sopenharmony_ci        controller.abort();
5841cb0ef41Sopenharmony_ci
5851cb0ef41Sopenharmony_ci        await assert.rejects(() => p, {
5861cb0ef41Sopenharmony_ci          name: 'AbortError',
5871cb0ef41Sopenharmony_ci        });
5881cb0ef41Sopenharmony_ci
5891cb0ef41Sopenharmony_ci      });
5901cb0ef41Sopenharmony_ci
5911cb0ef41Sopenharmony_ci      it('should abort operation when .abort is called before calling setTimeout', async (t) => {
5921cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setTimeout']);
5931cb0ef41Sopenharmony_ci        const expectedResult = 'result';
5941cb0ef41Sopenharmony_ci        const controller = new AbortController();
5951cb0ef41Sopenharmony_ci        controller.abort();
5961cb0ef41Sopenharmony_ci        const p = nodeTimersPromises.setTimeout(2000, expectedResult, {
5971cb0ef41Sopenharmony_ci          ref: true,
5981cb0ef41Sopenharmony_ci          signal: controller.signal
5991cb0ef41Sopenharmony_ci        });
6001cb0ef41Sopenharmony_ci
6011cb0ef41Sopenharmony_ci        await assert.rejects(() => p, {
6021cb0ef41Sopenharmony_ci          name: 'AbortError',
6031cb0ef41Sopenharmony_ci        });
6041cb0ef41Sopenharmony_ci
6051cb0ef41Sopenharmony_ci      });
6061cb0ef41Sopenharmony_ci
6071cb0ef41Sopenharmony_ci      it('should reject given an an invalid signal instance', async (t) => {
6081cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setTimeout']);
6091cb0ef41Sopenharmony_ci        const expectedResult = 'result';
6101cb0ef41Sopenharmony_ci        const p = nodeTimersPromises.setTimeout(2000, expectedResult, {
6111cb0ef41Sopenharmony_ci          ref: true,
6121cb0ef41Sopenharmony_ci          signal: {}
6131cb0ef41Sopenharmony_ci        });
6141cb0ef41Sopenharmony_ci
6151cb0ef41Sopenharmony_ci        await assert.rejects(() => p, {
6161cb0ef41Sopenharmony_ci          name: 'TypeError',
6171cb0ef41Sopenharmony_ci          code: 'ERR_INVALID_ARG_TYPE'
6181cb0ef41Sopenharmony_ci        });
6191cb0ef41Sopenharmony_ci
6201cb0ef41Sopenharmony_ci      });
6211cb0ef41Sopenharmony_ci    });
6221cb0ef41Sopenharmony_ci
6231cb0ef41Sopenharmony_ci    describe('setInterval Suite', () => {
6241cb0ef41Sopenharmony_ci      it('should tick three times using fake setInterval', async (t) => {
6251cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setInterval']);
6261cb0ef41Sopenharmony_ci
6271cb0ef41Sopenharmony_ci        const interval = 100;
6281cb0ef41Sopenharmony_ci        const intervalIterator = nodeTimersPromises.setInterval(interval, Date.now());
6291cb0ef41Sopenharmony_ci
6301cb0ef41Sopenharmony_ci        const first = intervalIterator.next();
6311cb0ef41Sopenharmony_ci        const second = intervalIterator.next();
6321cb0ef41Sopenharmony_ci        const third = intervalIterator.next();
6331cb0ef41Sopenharmony_ci
6341cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
6351cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
6361cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
6371cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
6381cb0ef41Sopenharmony_ci
6391cb0ef41Sopenharmony_ci        const results = await Promise.all([
6401cb0ef41Sopenharmony_ci          first,
6411cb0ef41Sopenharmony_ci          second,
6421cb0ef41Sopenharmony_ci          third,
6431cb0ef41Sopenharmony_ci        ]);
6441cb0ef41Sopenharmony_ci
6451cb0ef41Sopenharmony_ci        const finished = await intervalIterator.return();
6461cb0ef41Sopenharmony_ci        assert.deepStrictEqual(finished, { done: true, value: undefined });
6471cb0ef41Sopenharmony_ci
6481cb0ef41Sopenharmony_ci        results.forEach((result) => {
6491cb0ef41Sopenharmony_ci          assert.strictEqual(typeof result.value, 'number');
6501cb0ef41Sopenharmony_ci          assert.strictEqual(result.done, false);
6511cb0ef41Sopenharmony_ci        });
6521cb0ef41Sopenharmony_ci
6531cb0ef41Sopenharmony_ci      });
6541cb0ef41Sopenharmony_ci      it('should tick five times testing a real use case', async (t) => {
6551cb0ef41Sopenharmony_ci
6561cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setInterval']);
6571cb0ef41Sopenharmony_ci
6581cb0ef41Sopenharmony_ci        const expectedIterations = 5;
6591cb0ef41Sopenharmony_ci        const interval = 1000;
6601cb0ef41Sopenharmony_ci        const startedAt = Date.now();
6611cb0ef41Sopenharmony_ci        async function run() {
6621cb0ef41Sopenharmony_ci          const times = [];
6631cb0ef41Sopenharmony_ci          for await (const time of nodeTimersPromises.setInterval(interval, startedAt)) {
6641cb0ef41Sopenharmony_ci            times.push(time);
6651cb0ef41Sopenharmony_ci            if (times.length === expectedIterations) break;
6661cb0ef41Sopenharmony_ci
6671cb0ef41Sopenharmony_ci          }
6681cb0ef41Sopenharmony_ci          return times;
6691cb0ef41Sopenharmony_ci        }
6701cb0ef41Sopenharmony_ci
6711cb0ef41Sopenharmony_ci        const r = run();
6721cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
6731cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
6741cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
6751cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
6761cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
6771cb0ef41Sopenharmony_ci
6781cb0ef41Sopenharmony_ci        const timeResults = await r;
6791cb0ef41Sopenharmony_ci        assert.strictEqual(timeResults.length, expectedIterations);
6801cb0ef41Sopenharmony_ci        for (let it = 1; it < expectedIterations; it++) {
6811cb0ef41Sopenharmony_ci          assert.strictEqual(timeResults[it - 1], startedAt + (interval * it));
6821cb0ef41Sopenharmony_ci        }
6831cb0ef41Sopenharmony_ci      });
6841cb0ef41Sopenharmony_ci
6851cb0ef41Sopenharmony_ci      it('should abort operation given an abort controller signal', async (t) => {
6861cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setInterval']);
6871cb0ef41Sopenharmony_ci
6881cb0ef41Sopenharmony_ci        const interval = 100;
6891cb0ef41Sopenharmony_ci        const abortController = new AbortController();
6901cb0ef41Sopenharmony_ci        const intervalIterator = nodeTimersPromises.setInterval(interval, Date.now(), {
6911cb0ef41Sopenharmony_ci          signal: abortController.signal
6921cb0ef41Sopenharmony_ci        });
6931cb0ef41Sopenharmony_ci
6941cb0ef41Sopenharmony_ci        const first = intervalIterator.next();
6951cb0ef41Sopenharmony_ci        const second = intervalIterator.next();
6961cb0ef41Sopenharmony_ci
6971cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
6981cb0ef41Sopenharmony_ci        abortController.abort();
6991cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
7001cb0ef41Sopenharmony_ci
7011cb0ef41Sopenharmony_ci        const firstResult = await first;
7021cb0ef41Sopenharmony_ci        // Interval * 2 because value can be a little bit greater than interval
7031cb0ef41Sopenharmony_ci        assert.ok(firstResult.value < Date.now() + interval * 2);
7041cb0ef41Sopenharmony_ci        assert.strictEqual(firstResult.done, false);
7051cb0ef41Sopenharmony_ci
7061cb0ef41Sopenharmony_ci        await assert.rejects(() => second, {
7071cb0ef41Sopenharmony_ci          name: 'AbortError',
7081cb0ef41Sopenharmony_ci        });
7091cb0ef41Sopenharmony_ci
7101cb0ef41Sopenharmony_ci      });
7111cb0ef41Sopenharmony_ci
7121cb0ef41Sopenharmony_ci      it('should abort operation when .abort is called before calling setInterval', async (t) => {
7131cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setInterval']);
7141cb0ef41Sopenharmony_ci
7151cb0ef41Sopenharmony_ci        const interval = 100;
7161cb0ef41Sopenharmony_ci        const abortController = new AbortController();
7171cb0ef41Sopenharmony_ci        abortController.abort();
7181cb0ef41Sopenharmony_ci        const intervalIterator = nodeTimersPromises.setInterval(interval, Date.now(), {
7191cb0ef41Sopenharmony_ci          signal: abortController.signal
7201cb0ef41Sopenharmony_ci        });
7211cb0ef41Sopenharmony_ci
7221cb0ef41Sopenharmony_ci        const first = intervalIterator.next();
7231cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
7241cb0ef41Sopenharmony_ci
7251cb0ef41Sopenharmony_ci        await assert.rejects(() => first, {
7261cb0ef41Sopenharmony_ci          name: 'AbortError',
7271cb0ef41Sopenharmony_ci        });
7281cb0ef41Sopenharmony_ci      });
7291cb0ef41Sopenharmony_ci
7301cb0ef41Sopenharmony_ci      it('should abort operation given an abort controller signal on a real use case', async (t) => {
7311cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setInterval']);
7321cb0ef41Sopenharmony_ci        const controller = new AbortController();
7331cb0ef41Sopenharmony_ci        const signal = controller.signal;
7341cb0ef41Sopenharmony_ci        const interval = 200;
7351cb0ef41Sopenharmony_ci        const expectedIterations = 2;
7361cb0ef41Sopenharmony_ci        const startedAt = Date.now();
7371cb0ef41Sopenharmony_ci        const timeResults = [];
7381cb0ef41Sopenharmony_ci        async function run() {
7391cb0ef41Sopenharmony_ci          const it = nodeTimersPromises.setInterval(interval, startedAt, { signal });
7401cb0ef41Sopenharmony_ci          for await (const time of it) {
7411cb0ef41Sopenharmony_ci            timeResults.push(time);
7421cb0ef41Sopenharmony_ci            if (timeResults.length === 5) break;
7431cb0ef41Sopenharmony_ci          }
7441cb0ef41Sopenharmony_ci        }
7451cb0ef41Sopenharmony_ci
7461cb0ef41Sopenharmony_ci        const r = run();
7471cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
7481cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
7491cb0ef41Sopenharmony_ci        controller.abort();
7501cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
7511cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
7521cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
7531cb0ef41Sopenharmony_ci        t.mock.timers.tick(interval);
7541cb0ef41Sopenharmony_ci
7551cb0ef41Sopenharmony_ci        await assert.rejects(() => r, {
7561cb0ef41Sopenharmony_ci          name: 'AbortError',
7571cb0ef41Sopenharmony_ci        });
7581cb0ef41Sopenharmony_ci        assert.strictEqual(timeResults.length, expectedIterations);
7591cb0ef41Sopenharmony_ci
7601cb0ef41Sopenharmony_ci        for (let it = 1; it < expectedIterations; it++) {
7611cb0ef41Sopenharmony_ci          assert.strictEqual(timeResults[it - 1], startedAt + (interval * it));
7621cb0ef41Sopenharmony_ci        }
7631cb0ef41Sopenharmony_ci
7641cb0ef41Sopenharmony_ci      });
7651cb0ef41Sopenharmony_ci
7661cb0ef41Sopenharmony_ci    });
7671cb0ef41Sopenharmony_ci
7681cb0ef41Sopenharmony_ci    describe('setImmediate Suite', () => {
7691cb0ef41Sopenharmony_ci      it('should advance in time and trigger timers when calling the .tick function multiple times', (t, done) => {
7701cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate']);
7711cb0ef41Sopenharmony_ci        const p = nodeTimersPromises.setImmediate();
7721cb0ef41Sopenharmony_ci
7731cb0ef41Sopenharmony_ci        t.mock.timers.tick(5555);
7741cb0ef41Sopenharmony_ci
7751cb0ef41Sopenharmony_ci        p.then(common.mustCall((result) => {
7761cb0ef41Sopenharmony_ci          assert.strictEqual(result, undefined);
7771cb0ef41Sopenharmony_ci          done();
7781cb0ef41Sopenharmony_ci        }, 1));
7791cb0ef41Sopenharmony_ci      });
7801cb0ef41Sopenharmony_ci
7811cb0ef41Sopenharmony_ci      it('should work with the same params as the original timers/promises/setImmediate', async (t) => {
7821cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate']);
7831cb0ef41Sopenharmony_ci        const expectedResult = 'result';
7841cb0ef41Sopenharmony_ci        const controller = new AbortController();
7851cb0ef41Sopenharmony_ci        const p = nodeTimersPromises.setImmediate(expectedResult, {
7861cb0ef41Sopenharmony_ci          ref: true,
7871cb0ef41Sopenharmony_ci          signal: controller.signal
7881cb0ef41Sopenharmony_ci        });
7891cb0ef41Sopenharmony_ci
7901cb0ef41Sopenharmony_ci        t.mock.timers.tick(500);
7911cb0ef41Sopenharmony_ci
7921cb0ef41Sopenharmony_ci        const result = await p;
7931cb0ef41Sopenharmony_ci        assert.strictEqual(result, expectedResult);
7941cb0ef41Sopenharmony_ci      });
7951cb0ef41Sopenharmony_ci
7961cb0ef41Sopenharmony_ci      it('should abort operation if timers/promises/setImmediate received an aborted signal', async (t) => {
7971cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate']);
7981cb0ef41Sopenharmony_ci        const expectedResult = 'result';
7991cb0ef41Sopenharmony_ci        const controller = new AbortController();
8001cb0ef41Sopenharmony_ci        const p = nodeTimersPromises.setImmediate(expectedResult, {
8011cb0ef41Sopenharmony_ci          ref: true,
8021cb0ef41Sopenharmony_ci          signal: controller.signal
8031cb0ef41Sopenharmony_ci        });
8041cb0ef41Sopenharmony_ci
8051cb0ef41Sopenharmony_ci        controller.abort();
8061cb0ef41Sopenharmony_ci        t.mock.timers.tick(0);
8071cb0ef41Sopenharmony_ci
8081cb0ef41Sopenharmony_ci        await assert.rejects(() => p, {
8091cb0ef41Sopenharmony_ci          name: 'AbortError',
8101cb0ef41Sopenharmony_ci        });
8111cb0ef41Sopenharmony_ci
8121cb0ef41Sopenharmony_ci      });
8131cb0ef41Sopenharmony_ci      it('should abort operation even if the .tick wasn\'t called', async (t) => {
8141cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate']);
8151cb0ef41Sopenharmony_ci        const expectedResult = 'result';
8161cb0ef41Sopenharmony_ci        const controller = new AbortController();
8171cb0ef41Sopenharmony_ci        const p = nodeTimersPromises.setImmediate(expectedResult, {
8181cb0ef41Sopenharmony_ci          ref: true,
8191cb0ef41Sopenharmony_ci          signal: controller.signal
8201cb0ef41Sopenharmony_ci        });
8211cb0ef41Sopenharmony_ci
8221cb0ef41Sopenharmony_ci        controller.abort();
8231cb0ef41Sopenharmony_ci
8241cb0ef41Sopenharmony_ci        await assert.rejects(() => p, {
8251cb0ef41Sopenharmony_ci          name: 'AbortError',
8261cb0ef41Sopenharmony_ci        });
8271cb0ef41Sopenharmony_ci      });
8281cb0ef41Sopenharmony_ci
8291cb0ef41Sopenharmony_ci      it('should abort operation when .abort is called before calling setImmediate', async (t) => {
8301cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate']);
8311cb0ef41Sopenharmony_ci        const expectedResult = 'result';
8321cb0ef41Sopenharmony_ci        const controller = new AbortController();
8331cb0ef41Sopenharmony_ci        controller.abort();
8341cb0ef41Sopenharmony_ci        const p = nodeTimersPromises.setImmediate(expectedResult, {
8351cb0ef41Sopenharmony_ci          ref: true,
8361cb0ef41Sopenharmony_ci          signal: controller.signal
8371cb0ef41Sopenharmony_ci        });
8381cb0ef41Sopenharmony_ci
8391cb0ef41Sopenharmony_ci        await assert.rejects(() => p, {
8401cb0ef41Sopenharmony_ci          name: 'AbortError',
8411cb0ef41Sopenharmony_ci        });
8421cb0ef41Sopenharmony_ci
8431cb0ef41Sopenharmony_ci      });
8441cb0ef41Sopenharmony_ci
8451cb0ef41Sopenharmony_ci      it('should reject given an an invalid signal instance', async (t) => {
8461cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate']);
8471cb0ef41Sopenharmony_ci        const expectedResult = 'result';
8481cb0ef41Sopenharmony_ci        const p = nodeTimersPromises.setImmediate(expectedResult, {
8491cb0ef41Sopenharmony_ci          ref: true,
8501cb0ef41Sopenharmony_ci          signal: {}
8511cb0ef41Sopenharmony_ci        });
8521cb0ef41Sopenharmony_ci
8531cb0ef41Sopenharmony_ci        await assert.rejects(() => p, {
8541cb0ef41Sopenharmony_ci          name: 'TypeError',
8551cb0ef41Sopenharmony_ci          code: 'ERR_INVALID_ARG_TYPE'
8561cb0ef41Sopenharmony_ci        });
8571cb0ef41Sopenharmony_ci
8581cb0ef41Sopenharmony_ci      });
8591cb0ef41Sopenharmony_ci
8601cb0ef41Sopenharmony_ci      it('should execute in order if setImmediate is called multiple times', async (t) => {
8611cb0ef41Sopenharmony_ci        t.mock.timers.enable(['setImmediate']);
8621cb0ef41Sopenharmony_ci
8631cb0ef41Sopenharmony_ci        const p1 = nodeTimersPromises.setImmediate('fn1');
8641cb0ef41Sopenharmony_ci        const p2 = nodeTimersPromises.setImmediate('fn2');
8651cb0ef41Sopenharmony_ci
8661cb0ef41Sopenharmony_ci        t.mock.timers.tick(0);
8671cb0ef41Sopenharmony_ci
8681cb0ef41Sopenharmony_ci        const results = await Promise.race([p1, p2]);
8691cb0ef41Sopenharmony_ci
8701cb0ef41Sopenharmony_ci        assert.strictEqual(results, 'fn1');
8711cb0ef41Sopenharmony_ci      });
8721cb0ef41Sopenharmony_ci    });
8731cb0ef41Sopenharmony_ci  });
8741cb0ef41Sopenharmony_ci});
875