11cb0ef41Sopenharmony_ci// Flags: --no-warnings --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst timers = require('timers');
61cb0ef41Sopenharmony_ciconst { promisify } = require('util');
71cb0ef41Sopenharmony_ciconst child_process = require('child_process');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst { getEventListeners } = require('events');
101cb0ef41Sopenharmony_ciconst { NodeEventTarget } = require('internal/event_target');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst timerPromises = require('timers/promises');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst setPromiseTimeout = promisify(timers.setTimeout);
151cb0ef41Sopenharmony_ciconst exec = promisify(child_process.exec);
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciassert.strictEqual(setPromiseTimeout, timerPromises.setTimeout);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciprocess.on('multipleResolves', common.mustNotCall());
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci{
221cb0ef41Sopenharmony_ci  const promise = setPromiseTimeout(1);
231cb0ef41Sopenharmony_ci  promise.then(common.mustCall((value) => {
241cb0ef41Sopenharmony_ci    assert.strictEqual(value, undefined);
251cb0ef41Sopenharmony_ci  }));
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci{
291cb0ef41Sopenharmony_ci  const promise = setPromiseTimeout(1, 'foobar');
301cb0ef41Sopenharmony_ci  promise.then(common.mustCall((value) => {
311cb0ef41Sopenharmony_ci    assert.strictEqual(value, 'foobar');
321cb0ef41Sopenharmony_ci  }));
331cb0ef41Sopenharmony_ci}
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci{
361cb0ef41Sopenharmony_ci  const ac = new AbortController();
371cb0ef41Sopenharmony_ci  const signal = ac.signal;
381cb0ef41Sopenharmony_ci  assert.rejects(setPromiseTimeout(10, undefined, { signal }), /AbortError/)
391cb0ef41Sopenharmony_ci    .then(common.mustCall());
401cb0ef41Sopenharmony_ci  ac.abort();
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci{
441cb0ef41Sopenharmony_ci  const signal = AbortSignal.abort(); // Abort in advance
451cb0ef41Sopenharmony_ci  assert.rejects(setPromiseTimeout(10, undefined, { signal }), /AbortError/)
461cb0ef41Sopenharmony_ci    .then(common.mustCall());
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci{
501cb0ef41Sopenharmony_ci  // Check that aborting after resolve will not reject.
511cb0ef41Sopenharmony_ci  const ac = new AbortController();
521cb0ef41Sopenharmony_ci  const signal = ac.signal;
531cb0ef41Sopenharmony_ci  setPromiseTimeout(10, undefined, { signal })
541cb0ef41Sopenharmony_ci    .then(common.mustCall(() => { ac.abort(); }))
551cb0ef41Sopenharmony_ci    .then(common.mustCall());
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci{
591cb0ef41Sopenharmony_ci  // Check that timer adding signals does not leak handlers
601cb0ef41Sopenharmony_ci  const signal = new NodeEventTarget();
611cb0ef41Sopenharmony_ci  signal.aborted = false;
621cb0ef41Sopenharmony_ci  setPromiseTimeout(0, null, { signal }).finally(common.mustCall(() => {
631cb0ef41Sopenharmony_ci    assert.strictEqual(getEventListeners(signal, 'abort').length, 0);
641cb0ef41Sopenharmony_ci  }));
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci{
681cb0ef41Sopenharmony_ci  Promise.all(
691cb0ef41Sopenharmony_ci    [1, '', false, Infinity].map(
701cb0ef41Sopenharmony_ci      (i) => assert.rejects(setPromiseTimeout(10, null, i), {
711cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_ARG_TYPE'
721cb0ef41Sopenharmony_ci      })
731cb0ef41Sopenharmony_ci    )
741cb0ef41Sopenharmony_ci  ).then(common.mustCall());
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  Promise.all(
771cb0ef41Sopenharmony_ci    [1, '', false, Infinity, null, {}].map(
781cb0ef41Sopenharmony_ci      (signal) => assert.rejects(setPromiseTimeout(10, null, { signal }), {
791cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_ARG_TYPE'
801cb0ef41Sopenharmony_ci      })
811cb0ef41Sopenharmony_ci    )
821cb0ef41Sopenharmony_ci  ).then(common.mustCall());
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  Promise.all(
851cb0ef41Sopenharmony_ci    [1, '', Infinity, null, {}].map(
861cb0ef41Sopenharmony_ci      (ref) => assert.rejects(setPromiseTimeout(10, null, { ref }), {
871cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_ARG_TYPE'
881cb0ef41Sopenharmony_ci      })
891cb0ef41Sopenharmony_ci    )
901cb0ef41Sopenharmony_ci  ).then(common.mustCall());
911cb0ef41Sopenharmony_ci}
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci{
941cb0ef41Sopenharmony_ci  exec(`${process.execPath} -pe "const assert = require('assert');` +
951cb0ef41Sopenharmony_ci    'require(\'timers/promises\').setTimeout(1000, null, { ref: false }).' +
961cb0ef41Sopenharmony_ci    'then(assert.fail)"').then(common.mustCall(({ stderr }) => {
971cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
981cb0ef41Sopenharmony_ci  }));
991cb0ef41Sopenharmony_ci}
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci(async () => {
1021cb0ef41Sopenharmony_ci  const signal = AbortSignal.abort('boom');
1031cb0ef41Sopenharmony_ci  await assert.rejects(timerPromises.setTimeout(1, undefined, { signal }), {
1041cb0ef41Sopenharmony_ci    cause: 'boom',
1051cb0ef41Sopenharmony_ci  });
1061cb0ef41Sopenharmony_ci})().then(common.mustCall());
107