-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: split promisified timers test for coverage purposes
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: #37943 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
- Loading branch information
Showing
3 changed files
with
198 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, ''); | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.