11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci'use strict';
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst { strictEqual, throws } = require('assert');
81cb0ef41Sopenharmony_ciconst { setUnrefTimeout } = require('internal/timers');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// Schedule the unrefed cases first so that the later case keeps the event loop
111cb0ef41Sopenharmony_ci// active.
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci// Every case in this test relies on implicit sorting within either Node's or
141cb0ef41Sopenharmony_ci// libuv's timers storage data structures.
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci// unref()'d timer
171cb0ef41Sopenharmony_ci{
181cb0ef41Sopenharmony_ci  let called = false;
191cb0ef41Sopenharmony_ci  const timer = setTimeout(common.mustCall(() => {
201cb0ef41Sopenharmony_ci    called = true;
211cb0ef41Sopenharmony_ci  }), 1);
221cb0ef41Sopenharmony_ci  timer.unref();
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  // This relies on implicit timers handle sorting within libuv.
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  setTimeout(common.mustCall(() => {
271cb0ef41Sopenharmony_ci    strictEqual(called, false, 'unref()\'d timer returned before check');
281cb0ef41Sopenharmony_ci  }), 1);
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  strictEqual(timer.refresh(), timer);
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci// Should throw with non-functions
341cb0ef41Sopenharmony_ci{
351cb0ef41Sopenharmony_ci  [null, true, false, 0, 1, NaN, '', 'foo', {}, Symbol()].forEach((cb) => {
361cb0ef41Sopenharmony_ci    throws(
371cb0ef41Sopenharmony_ci      () => setUnrefTimeout(cb),
381cb0ef41Sopenharmony_ci      {
391cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_ARG_TYPE',
401cb0ef41Sopenharmony_ci      }
411cb0ef41Sopenharmony_ci    );
421cb0ef41Sopenharmony_ci  });
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci// unref pooled timer
461cb0ef41Sopenharmony_ci{
471cb0ef41Sopenharmony_ci  let called = false;
481cb0ef41Sopenharmony_ci  const timer = setUnrefTimeout(common.mustCall(() => {
491cb0ef41Sopenharmony_ci    called = true;
501cb0ef41Sopenharmony_ci  }), 1);
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  setUnrefTimeout(common.mustCall(() => {
531cb0ef41Sopenharmony_ci    strictEqual(called, false, 'unref pooled timer returned before check');
541cb0ef41Sopenharmony_ci  }), 1);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  strictEqual(timer.refresh(), timer);
571cb0ef41Sopenharmony_ci}
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci// regular timer
601cb0ef41Sopenharmony_ci{
611cb0ef41Sopenharmony_ci  let called = false;
621cb0ef41Sopenharmony_ci  const timer = setTimeout(common.mustCall(() => {
631cb0ef41Sopenharmony_ci    called = true;
641cb0ef41Sopenharmony_ci  }), 1);
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  setTimeout(common.mustCall(() => {
671cb0ef41Sopenharmony_ci    strictEqual(called, false, 'pooled timer returned before check');
681cb0ef41Sopenharmony_ci  }), 1);
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  strictEqual(timer.refresh(), timer);
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci// regular timer
741cb0ef41Sopenharmony_ci{
751cb0ef41Sopenharmony_ci  let called = false;
761cb0ef41Sopenharmony_ci  const timer = setTimeout(common.mustCall(() => {
771cb0ef41Sopenharmony_ci    if (!called) {
781cb0ef41Sopenharmony_ci      called = true;
791cb0ef41Sopenharmony_ci      process.nextTick(common.mustCall(() => {
801cb0ef41Sopenharmony_ci        timer.refresh();
811cb0ef41Sopenharmony_ci        strictEqual(timer.hasRef(), true);
821cb0ef41Sopenharmony_ci      }));
831cb0ef41Sopenharmony_ci    }
841cb0ef41Sopenharmony_ci  }, 2), 1);
851cb0ef41Sopenharmony_ci}
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci// interval
881cb0ef41Sopenharmony_ci{
891cb0ef41Sopenharmony_ci  let called = 0;
901cb0ef41Sopenharmony_ci  const timer = setInterval(common.mustCall(() => {
911cb0ef41Sopenharmony_ci    called += 1;
921cb0ef41Sopenharmony_ci    if (called === 2) {
931cb0ef41Sopenharmony_ci      clearInterval(timer);
941cb0ef41Sopenharmony_ci    }
951cb0ef41Sopenharmony_ci  }, 2), 1);
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  setTimeout(common.mustCall(() => {
981cb0ef41Sopenharmony_ci    strictEqual(called, 0, 'pooled timer returned before check');
991cb0ef41Sopenharmony_ci  }), 1);
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  strictEqual(timer.refresh(), timer);
1021cb0ef41Sopenharmony_ci}
103