Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 50 additions & 40 deletions test/parallel/test-timers-promisified.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,39 @@ const { NodeEventTarget } = require('internal/event_target');

const timerPromises = require('timers/promises');

/* eslint-disable no-restricted-syntax */

const setTimeout = promisify(timers.setTimeout);
const setImmediate = promisify(timers.setImmediate);
const setPromiseTimeout = promisify(timers.setTimeout);
const setPromiseImmediate = promisify(timers.setImmediate);
const exec = promisify(child_process.exec);

assert.strictEqual(setTimeout, timerPromises.setTimeout);
assert.strictEqual(setImmediate, timerPromises.setImmediate);
assert.strictEqual(setPromiseTimeout, timerPromises.setTimeout);
assert.strictEqual(setPromiseImmediate, timerPromises.setImmediate);
const { setInterval } = timerPromises;

process.on('multipleResolves', common.mustNotCall());

{
const promise = setTimeout(1);
const promise = setPromiseTimeout(1);
promise.then(common.mustCall((value) => {
assert.strictEqual(value, undefined);
}));
}

{
const promise = setTimeout(1, 'foobar');
const promise = setPromiseTimeout(1, 'foobar');
promise.then(common.mustCall((value) => {
assert.strictEqual(value, 'foobar');
}));
}

{
const promise = setImmediate();
const promise = setPromiseImmediate();
promise.then(common.mustCall((value) => {
assert.strictEqual(value, undefined);
}));
}

{
const promise = setImmediate('foobar');
const promise = setPromiseImmediate('foobar');
promise.then(common.mustCall((value) => {
assert.strictEqual(value, 'foobar');
}));
Expand Down Expand Up @@ -94,7 +92,7 @@ process.on('multipleResolves', common.mustNotCall());
{
const ac = new AbortController();
const signal = ac.signal;
assert.rejects(setTimeout(10, undefined, { signal }), /AbortError/)
assert.rejects(setPromiseTimeout(10, undefined, { signal }), /AbortError/)
.then(common.mustCall());
ac.abort();
}
Expand All @@ -103,14 +101,14 @@ process.on('multipleResolves', common.mustNotCall());
const ac = new AbortController();
const signal = ac.signal;
ac.abort(); // Abort in advance
assert.rejects(setTimeout(10, undefined, { signal }), /AbortError/)
assert.rejects(setPromiseTimeout(10, undefined, { signal }), /AbortError/)
.then(common.mustCall());
}

{
const ac = new AbortController();
const signal = ac.signal;
assert.rejects(setImmediate(10, { signal }), /AbortError/)
assert.rejects(setPromiseImmediate(10, { signal }), /AbortError/)
.then(common.mustCall());
ac.abort();
}
Expand All @@ -119,7 +117,7 @@ process.on('multipleResolves', common.mustNotCall());
const ac = new AbortController();
const signal = ac.signal;
ac.abort(); // Abort in advance
assert.rejects(setImmediate(10, { signal }), /AbortError/)
assert.rejects(setPromiseImmediate(10, { signal }), /AbortError/)
.then(common.mustCall());
}

Expand Down Expand Up @@ -167,18 +165,17 @@ process.on('multipleResolves', common.mustNotCall());
// Check that aborting after resolve will not reject.
const ac = new AbortController();
const signal = ac.signal;
assert.doesNotReject(setTimeout(10, undefined, { signal })
.then(common.mustCall(() => {
ac.abort();
}))).then(common.mustCall());
setPromiseTimeout(10, undefined, { signal })
.then(common.mustCall(() => { ac.abort(); }))
.then(common.mustCall());
}
{
// Check that aborting after resolve will not reject.
const ac = new AbortController();
const signal = ac.signal;
assert.doesNotReject(setImmediate(10, { signal }).then(common.mustCall(() => {
ac.abort();
}))).then(common.mustCall());
setPromiseImmediate(10, { signal })
.then(common.mustCall(() => { ac.abort(); }))
.then(common.mustCall());
}

{
Expand All @@ -205,7 +202,7 @@ process.on('multipleResolves', common.mustNotCall());
// Check that timer adding signals does not leak handlers
const signal = new NodeEventTarget();
signal.aborted = false;
setTimeout(0, null, { signal }).finally(common.mustCall(() => {
setPromiseTimeout(0, null, { signal }).finally(common.mustCall(() => {
assert.strictEqual(signal.listenerCount('abort'), 0);
}));
}
Expand All @@ -214,7 +211,7 @@ process.on('multipleResolves', common.mustNotCall());
// Check that timer adding signals does not leak handlers
const signal = new NodeEventTarget();
signal.aborted = false;
setImmediate(0, { signal }).finally(common.mustCall(() => {
setPromiseImmediate(0, { signal }).finally(common.mustCall(() => {
assert.strictEqual(signal.listenerCount('abort'), 0);
}));
}
Expand Down Expand Up @@ -258,39 +255,52 @@ process.on('multipleResolves', common.mustNotCall());

{
Promise.all(
[1, '', false, Infinity].map((i) => assert.rejects(setImmediate(10, i)), {
code: 'ERR_INVALID_ARG_TYPE'
})).then(common.mustCall());
[1, '', false, Infinity].map(
(i) => assert.rejects(setPromiseImmediate(10, i), {
code: 'ERR_INVALID_ARG_TYPE'
})
)
).then(common.mustCall());

Promise.all(
[1, '', false, Infinity, null, {}].map(
(signal) => assert.rejects(setImmediate(10, { signal })), {
(signal) => assert.rejects(setPromiseImmediate(10, { signal }), {
code: 'ERR_INVALID_ARG_TYPE'
})).then(common.mustCall());
})
)
).then(common.mustCall());

Promise.all(
[1, '', Infinity, null, {}].map(
(ref) => assert.rejects(setImmediate(10, { ref })), {
(ref) => assert.rejects(setPromiseImmediate(10, { ref }), {
code: 'ERR_INVALID_ARG_TYPE'
})).then(common.mustCall());
})
)
).then(common.mustCall());

Promise.all(
[1, '', false, Infinity].map(
(i) => assert.rejects(setTimeout(10, null, i)), {
(i) => assert.rejects(setPromiseTimeout(10, null, i), {
code: 'ERR_INVALID_ARG_TYPE'
})).then(common.mustCall());
})
)
).then(common.mustCall());

Promise.all(
[1, '', false, Infinity, null, {}].map(
(signal) => assert.rejects(setTimeout(10, null, { signal })), {
(signal) => assert.rejects(setPromiseTimeout(10, null, { signal }), {
code: 'ERR_INVALID_ARG_TYPE'
})).then(common.mustCall());
})
)
).then(common.mustCall());

Promise.all(
[1, '', Infinity, null, {}].map(
(ref) => assert.rejects(setTimeout(10, null, { ref })), {
(ref) => assert.rejects(setPromiseTimeout(10, null, { ref }), {
code: 'ERR_INVALID_ARG_TYPE'
})).then(common.mustCall());
})
)
).then(common.mustCall());
}

{
Expand Down Expand Up @@ -357,7 +367,7 @@ process.on('multipleResolves', common.mustNotCall());
const delay = 10;
let totalIterations = 0;
const timeoutLoop = runInterval(async (iterationNumber) => {
await setTimeout(delay * 4);
await setPromiseTimeout(delay * 4);
if (iterationNumber <= 2) {
assert.strictEqual(signal.aborted, false);
}
Expand All @@ -382,8 +392,8 @@ process.on('multipleResolves', common.mustNotCall());
// Check that the timing is correct
let pre = false;
let post = false;
setTimeout(1).then(() => pre = true);
const iterable = setInterval(2);
setPromiseTimeout(1).then(() => pre = true);
const iterable = setInterval(() => {}, 2);
const iterator = iterable[Symbol.asyncIterator]();

iterator.next().then(common.mustCall(() => {
Expand All @@ -395,5 +405,5 @@ process.on('multipleResolves', common.mustNotCall());
return iterator.return();
}));

setTimeout(3).then(() => post = true);
setPromiseTimeout(3).then(() => post = true);
}