From b0226b39f2bd56b7249f3b63b879ca1deef0adcb Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Fri, 26 Mar 2021 22:47:50 -0700 Subject: [PATCH] test: split promisified timers test for coverage purposes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Because of lazy loading, running promisified timers tests for setTimeout and setImmediate from the same file means that there is a piece of code that doesn't get covered. Split into separate files to cover everything. Refs: https://coverage.nodejs.org/coverage-290c158018ac0277/lib/timers.js.html#L269 PR-URL: https://github.com/nodejs/node/pull/37943 Reviewed-By: James M Snell Reviewed-By: Michaƫl Zasso --- .../test-timers-immediate-promisified.js | 99 +++++++++++ ...js => test-timers-interval-promisified.js} | 160 ------------------ .../test-timers-timeout-promisified.js | 99 +++++++++++ 3 files changed, 198 insertions(+), 160 deletions(-) create mode 100644 test/parallel/test-timers-immediate-promisified.js rename test/parallel/{test-timers-promisified.js => test-timers-interval-promisified.js} (64%) create mode 100644 test/parallel/test-timers-timeout-promisified.js diff --git a/test/parallel/test-timers-immediate-promisified.js b/test/parallel/test-timers-immediate-promisified.js new file mode 100644 index 00000000000000..65c8411f1b2ffd --- /dev/null +++ b/test/parallel/test-timers-immediate-promisified.js @@ -0,0 +1,99 @@ +// Flags: --no-warnings --expose-internals +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const timers = require('timers'); +const { promisify } = require('util'); +const child_process = require('child_process'); + +// TODO(benjamingr) - refactor to use getEventListeners when #35991 lands +const { NodeEventTarget } = require('internal/event_target'); + +const timerPromises = require('timers/promises'); + +const setPromiseImmediate = promisify(timers.setImmediate); +const exec = promisify(child_process.exec); + +assert.strictEqual(setPromiseImmediate, timerPromises.setImmediate); + +process.on('multipleResolves', common.mustNotCall()); + +{ + const promise = setPromiseImmediate(); + promise.then(common.mustCall((value) => { + assert.strictEqual(value, undefined); + })); +} + +{ + const promise = setPromiseImmediate('foobar'); + promise.then(common.mustCall((value) => { + assert.strictEqual(value, 'foobar'); + })); +} + +{ + const ac = new AbortController(); + const signal = ac.signal; + assert.rejects(setPromiseImmediate(10, { signal }), /AbortError/) + .then(common.mustCall()); + ac.abort(); +} + +{ + const signal = AbortSignal.abort(); // Abort in advance + assert.rejects(setPromiseImmediate(10, { signal }), /AbortError/) + .then(common.mustCall()); +} + +{ + // Check that aborting after resolve will not reject. + const ac = new AbortController(); + const signal = ac.signal; + setPromiseImmediate(10, { signal }) + .then(common.mustCall(() => { ac.abort(); })) + .then(common.mustCall()); +} + +{ + // Check that timer adding signals does not leak handlers + const signal = new NodeEventTarget(); + signal.aborted = false; + setPromiseImmediate(0, { signal }).finally(common.mustCall(() => { + assert.strictEqual(signal.listenerCount('abort'), 0); + })); +} + +{ + Promise.all( + [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(setPromiseImmediate(10, { signal }), { + code: 'ERR_INVALID_ARG_TYPE' + }) + ) + ).then(common.mustCall()); + + Promise.all( + [1, '', Infinity, null, {}].map( + (ref) => assert.rejects(setPromiseImmediate(10, { ref }), { + code: 'ERR_INVALID_ARG_TYPE' + }) + ) + ).then(common.mustCall()); +} + +{ + exec(`${process.execPath} -pe "const assert = require('assert');` + + 'require(\'timers/promises\').setImmediate(null, { ref: false }).' + + 'then(assert.fail)"').then(common.mustCall(({ stderr }) => { + assert.strictEqual(stderr, ''); + })); +} diff --git a/test/parallel/test-timers-promisified.js b/test/parallel/test-timers-interval-promisified.js similarity index 64% rename from test/parallel/test-timers-promisified.js rename to test/parallel/test-timers-interval-promisified.js index 45da5036727061..60511372485963 100644 --- a/test/parallel/test-timers-promisified.js +++ b/test/parallel/test-timers-interval-promisified.js @@ -12,43 +12,12 @@ const { NodeEventTarget } = require('internal/event_target'); const timerPromises = require('timers/promises'); const setPromiseTimeout = promisify(timers.setTimeout); -const setPromiseImmediate = promisify(timers.setImmediate); const exec = promisify(child_process.exec); -assert.strictEqual(setPromiseTimeout, timerPromises.setTimeout); -assert.strictEqual(setPromiseImmediate, timerPromises.setImmediate); const { setInterval } = timerPromises; process.on('multipleResolves', common.mustNotCall()); -{ - const promise = setPromiseTimeout(1); - promise.then(common.mustCall((value) => { - assert.strictEqual(value, undefined); - })); -} - -{ - const promise = setPromiseTimeout(1, 'foobar'); - promise.then(common.mustCall((value) => { - assert.strictEqual(value, 'foobar'); - })); -} - -{ - const promise = setPromiseImmediate(); - promise.then(common.mustCall((value) => { - assert.strictEqual(value, undefined); - })); -} - -{ - const promise = setPromiseImmediate('foobar'); - promise.then(common.mustCall((value) => { - assert.strictEqual(value, 'foobar'); - })); -} - { const iterable = setInterval(1, undefined); const iterator = iterable[Symbol.asyncIterator](); @@ -89,34 +58,6 @@ process.on('multipleResolves', common.mustNotCall()); .then(common.mustCall()); } -{ - const ac = new AbortController(); - const signal = ac.signal; - assert.rejects(setPromiseTimeout(10, undefined, { signal }), /AbortError/) - .then(common.mustCall()); - ac.abort(); -} - -{ - const signal = AbortSignal.abort(); // Abort in advance - assert.rejects(setPromiseTimeout(10, undefined, { signal }), /AbortError/) - .then(common.mustCall()); -} - -{ - const ac = new AbortController(); - const signal = ac.signal; - assert.rejects(setPromiseImmediate(10, { signal }), /AbortError/) - .then(common.mustCall()); - ac.abort(); -} - -{ - const signal = AbortSignal.abort(); // Abort in advance - assert.rejects(setPromiseImmediate(10, { signal }), /AbortError/) - .then(common.mustCall()); -} - { const signal = AbortSignal.abort(); // Abort in advance @@ -155,23 +96,6 @@ process.on('multipleResolves', common.mustNotCall()); assert.rejects(abortPromise, /AbortError/).then(common.mustCall()); } -{ - // Check that aborting after resolve will not reject. - const ac = new AbortController(); - const signal = ac.signal; - 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; - setPromiseImmediate(10, { signal }) - .then(common.mustCall(() => { ac.abort(); })) - .then(common.mustCall()); -} - { [1, '', Infinity, null, {}].forEach((ref) => { const iterable = setInterval(10, undefined, { ref }); @@ -192,24 +116,6 @@ process.on('multipleResolves', common.mustNotCall()); }); } -{ - // Check that timer adding signals does not leak handlers - const signal = new NodeEventTarget(); - signal.aborted = false; - setPromiseTimeout(0, null, { signal }).finally(common.mustCall(() => { - assert.strictEqual(signal.listenerCount('abort'), 0); - })); -} - -{ - // Check that timer adding signals does not leak handlers - const signal = new NodeEventTarget(); - signal.aborted = false; - setPromiseImmediate(0, { signal }).finally(common.mustCall(() => { - assert.strictEqual(signal.listenerCount('abort'), 0); - })); -} - { // Check that timer adding signals does not leak handlers const signal = new NodeEventTarget(); @@ -247,72 +153,6 @@ process.on('multipleResolves', common.mustNotCall()); tryBreak().then(common.mustCall()); } -{ - Promise.all( - [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(setPromiseImmediate(10, { signal }), { - code: 'ERR_INVALID_ARG_TYPE' - }) - ) - ).then(common.mustCall()); - - Promise.all( - [1, '', Infinity, null, {}].map( - (ref) => assert.rejects(setPromiseImmediate(10, { ref }), { - code: 'ERR_INVALID_ARG_TYPE' - }) - ) - ).then(common.mustCall()); - - Promise.all( - [1, '', false, Infinity].map( - (i) => assert.rejects(setPromiseTimeout(10, null, i), { - code: 'ERR_INVALID_ARG_TYPE' - }) - ) - ).then(common.mustCall()); - - Promise.all( - [1, '', false, Infinity, null, {}].map( - (signal) => assert.rejects(setPromiseTimeout(10, null, { signal }), { - code: 'ERR_INVALID_ARG_TYPE' - }) - ) - ).then(common.mustCall()); - - Promise.all( - [1, '', Infinity, null, {}].map( - (ref) => assert.rejects(setPromiseTimeout(10, null, { ref }), { - code: 'ERR_INVALID_ARG_TYPE' - }) - ) - ).then(common.mustCall()); -} - -{ - exec(`${process.execPath} -pe "const assert = require('assert');` + - 'require(\'timers/promises\').setTimeout(1000, null, { ref: false }).' + - 'then(assert.fail)"').then(common.mustCall(({ stderr }) => { - assert.strictEqual(stderr, ''); - })); -} - -{ - exec(`${process.execPath} -pe "const assert = require('assert');` + - 'require(\'timers/promises\').setImmediate(null, { ref: false }).' + - 'then(assert.fail)"').then(common.mustCall(({ stderr }) => { - assert.strictEqual(stderr, ''); - })); -} - { exec(`${process.execPath} -pe "const assert = require('assert');` + 'const interval = require(\'timers/promises\')' + diff --git a/test/parallel/test-timers-timeout-promisified.js b/test/parallel/test-timers-timeout-promisified.js new file mode 100644 index 00000000000000..0b9a6b6f19a1c2 --- /dev/null +++ b/test/parallel/test-timers-timeout-promisified.js @@ -0,0 +1,99 @@ +// Flags: --no-warnings --expose-internals +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const timers = require('timers'); +const { promisify } = require('util'); +const child_process = require('child_process'); + +// TODO(benjamingr) - refactor to use getEventListeners when #35991 lands +const { NodeEventTarget } = require('internal/event_target'); + +const timerPromises = require('timers/promises'); + +const setPromiseTimeout = promisify(timers.setTimeout); +const exec = promisify(child_process.exec); + +assert.strictEqual(setPromiseTimeout, timerPromises.setTimeout); + +process.on('multipleResolves', common.mustNotCall()); + +{ + const promise = setPromiseTimeout(1); + promise.then(common.mustCall((value) => { + assert.strictEqual(value, undefined); + })); +} + +{ + const promise = setPromiseTimeout(1, 'foobar'); + promise.then(common.mustCall((value) => { + assert.strictEqual(value, 'foobar'); + })); +} + +{ + const ac = new AbortController(); + const signal = ac.signal; + assert.rejects(setPromiseTimeout(10, undefined, { signal }), /AbortError/) + .then(common.mustCall()); + ac.abort(); +} + +{ + const signal = AbortSignal.abort(); // Abort in advance + assert.rejects(setPromiseTimeout(10, undefined, { signal }), /AbortError/) + .then(common.mustCall()); +} + +{ + // Check that aborting after resolve will not reject. + const ac = new AbortController(); + const signal = ac.signal; + setPromiseTimeout(10, undefined, { signal }) + .then(common.mustCall(() => { ac.abort(); })) + .then(common.mustCall()); +} + +{ + // Check that timer adding signals does not leak handlers + const signal = new NodeEventTarget(); + signal.aborted = false; + setPromiseTimeout(0, null, { signal }).finally(common.mustCall(() => { + assert.strictEqual(signal.listenerCount('abort'), 0); + })); +} + +{ + Promise.all( + [1, '', false, Infinity].map( + (i) => assert.rejects(setPromiseTimeout(10, null, i), { + code: 'ERR_INVALID_ARG_TYPE' + }) + ) + ).then(common.mustCall()); + + Promise.all( + [1, '', false, Infinity, null, {}].map( + (signal) => assert.rejects(setPromiseTimeout(10, null, { signal }), { + code: 'ERR_INVALID_ARG_TYPE' + }) + ) + ).then(common.mustCall()); + + Promise.all( + [1, '', Infinity, null, {}].map( + (ref) => assert.rejects(setPromiseTimeout(10, null, { ref }), { + code: 'ERR_INVALID_ARG_TYPE' + }) + ) + ).then(common.mustCall()); +} + +{ + exec(`${process.execPath} -pe "const assert = require('assert');` + + 'require(\'timers/promises\').setTimeout(1000, null, { ref: false }).' + + 'then(assert.fail)"').then(common.mustCall(({ stderr }) => { + assert.strictEqual(stderr, ''); + })); +}