Skip to content

Commit b0226b3

Browse files
Trottruyadorno
authored andcommitted
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 <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent e60bd1a commit b0226b3

3 files changed

+198
-160
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Flags: --no-warnings --expose-internals
2+
'use strict';
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const timers = require('timers');
6+
const { promisify } = require('util');
7+
const child_process = require('child_process');
8+
9+
// TODO(benjamingr) - refactor to use getEventListeners when #35991 lands
10+
const { NodeEventTarget } = require('internal/event_target');
11+
12+
const timerPromises = require('timers/promises');
13+
14+
const setPromiseImmediate = promisify(timers.setImmediate);
15+
const exec = promisify(child_process.exec);
16+
17+
assert.strictEqual(setPromiseImmediate, timerPromises.setImmediate);
18+
19+
process.on('multipleResolves', common.mustNotCall());
20+
21+
{
22+
const promise = setPromiseImmediate();
23+
promise.then(common.mustCall((value) => {
24+
assert.strictEqual(value, undefined);
25+
}));
26+
}
27+
28+
{
29+
const promise = setPromiseImmediate('foobar');
30+
promise.then(common.mustCall((value) => {
31+
assert.strictEqual(value, 'foobar');
32+
}));
33+
}
34+
35+
{
36+
const ac = new AbortController();
37+
const signal = ac.signal;
38+
assert.rejects(setPromiseImmediate(10, { signal }), /AbortError/)
39+
.then(common.mustCall());
40+
ac.abort();
41+
}
42+
43+
{
44+
const signal = AbortSignal.abort(); // Abort in advance
45+
assert.rejects(setPromiseImmediate(10, { signal }), /AbortError/)
46+
.then(common.mustCall());
47+
}
48+
49+
{
50+
// Check that aborting after resolve will not reject.
51+
const ac = new AbortController();
52+
const signal = ac.signal;
53+
setPromiseImmediate(10, { signal })
54+
.then(common.mustCall(() => { ac.abort(); }))
55+
.then(common.mustCall());
56+
}
57+
58+
{
59+
// Check that timer adding signals does not leak handlers
60+
const signal = new NodeEventTarget();
61+
signal.aborted = false;
62+
setPromiseImmediate(0, { signal }).finally(common.mustCall(() => {
63+
assert.strictEqual(signal.listenerCount('abort'), 0);
64+
}));
65+
}
66+
67+
{
68+
Promise.all(
69+
[1, '', false, Infinity].map(
70+
(i) => assert.rejects(setPromiseImmediate(10, i), {
71+
code: 'ERR_INVALID_ARG_TYPE'
72+
})
73+
)
74+
).then(common.mustCall());
75+
76+
Promise.all(
77+
[1, '', false, Infinity, null, {}].map(
78+
(signal) => assert.rejects(setPromiseImmediate(10, { signal }), {
79+
code: 'ERR_INVALID_ARG_TYPE'
80+
})
81+
)
82+
).then(common.mustCall());
83+
84+
Promise.all(
85+
[1, '', Infinity, null, {}].map(
86+
(ref) => assert.rejects(setPromiseImmediate(10, { ref }), {
87+
code: 'ERR_INVALID_ARG_TYPE'
88+
})
89+
)
90+
).then(common.mustCall());
91+
}
92+
93+
{
94+
exec(`${process.execPath} -pe "const assert = require('assert');` +
95+
'require(\'timers/promises\').setImmediate(null, { ref: false }).' +
96+
'then(assert.fail)"').then(common.mustCall(({ stderr }) => {
97+
assert.strictEqual(stderr, '');
98+
}));
99+
}

test/parallel/test-timers-promisified.js test/parallel/test-timers-interval-promisified.js

-160
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,12 @@ const { NodeEventTarget } = require('internal/event_target');
1212
const timerPromises = require('timers/promises');
1313

1414
const setPromiseTimeout = promisify(timers.setTimeout);
15-
const setPromiseImmediate = promisify(timers.setImmediate);
1615
const exec = promisify(child_process.exec);
1716

18-
assert.strictEqual(setPromiseTimeout, timerPromises.setTimeout);
19-
assert.strictEqual(setPromiseImmediate, timerPromises.setImmediate);
2017
const { setInterval } = timerPromises;
2118

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

24-
{
25-
const promise = setPromiseTimeout(1);
26-
promise.then(common.mustCall((value) => {
27-
assert.strictEqual(value, undefined);
28-
}));
29-
}
30-
31-
{
32-
const promise = setPromiseTimeout(1, 'foobar');
33-
promise.then(common.mustCall((value) => {
34-
assert.strictEqual(value, 'foobar');
35-
}));
36-
}
37-
38-
{
39-
const promise = setPromiseImmediate();
40-
promise.then(common.mustCall((value) => {
41-
assert.strictEqual(value, undefined);
42-
}));
43-
}
44-
45-
{
46-
const promise = setPromiseImmediate('foobar');
47-
promise.then(common.mustCall((value) => {
48-
assert.strictEqual(value, 'foobar');
49-
}));
50-
}
51-
5221
{
5322
const iterable = setInterval(1, undefined);
5423
const iterator = iterable[Symbol.asyncIterator]();
@@ -89,34 +58,6 @@ process.on('multipleResolves', common.mustNotCall());
8958
.then(common.mustCall());
9059
}
9160

92-
{
93-
const ac = new AbortController();
94-
const signal = ac.signal;
95-
assert.rejects(setPromiseTimeout(10, undefined, { signal }), /AbortError/)
96-
.then(common.mustCall());
97-
ac.abort();
98-
}
99-
100-
{
101-
const signal = AbortSignal.abort(); // Abort in advance
102-
assert.rejects(setPromiseTimeout(10, undefined, { signal }), /AbortError/)
103-
.then(common.mustCall());
104-
}
105-
106-
{
107-
const ac = new AbortController();
108-
const signal = ac.signal;
109-
assert.rejects(setPromiseImmediate(10, { signal }), /AbortError/)
110-
.then(common.mustCall());
111-
ac.abort();
112-
}
113-
114-
{
115-
const signal = AbortSignal.abort(); // Abort in advance
116-
assert.rejects(setPromiseImmediate(10, { signal }), /AbortError/)
117-
.then(common.mustCall());
118-
}
119-
12061
{
12162
const signal = AbortSignal.abort(); // Abort in advance
12263

@@ -155,23 +96,6 @@ process.on('multipleResolves', common.mustNotCall());
15596
assert.rejects(abortPromise, /AbortError/).then(common.mustCall());
15697
}
15798

158-
{
159-
// Check that aborting after resolve will not reject.
160-
const ac = new AbortController();
161-
const signal = ac.signal;
162-
setPromiseTimeout(10, undefined, { signal })
163-
.then(common.mustCall(() => { ac.abort(); }))
164-
.then(common.mustCall());
165-
}
166-
{
167-
// Check that aborting after resolve will not reject.
168-
const ac = new AbortController();
169-
const signal = ac.signal;
170-
setPromiseImmediate(10, { signal })
171-
.then(common.mustCall(() => { ac.abort(); }))
172-
.then(common.mustCall());
173-
}
174-
17599
{
176100
[1, '', Infinity, null, {}].forEach((ref) => {
177101
const iterable = setInterval(10, undefined, { ref });
@@ -192,24 +116,6 @@ process.on('multipleResolves', common.mustNotCall());
192116
});
193117
}
194118

195-
{
196-
// Check that timer adding signals does not leak handlers
197-
const signal = new NodeEventTarget();
198-
signal.aborted = false;
199-
setPromiseTimeout(0, null, { signal }).finally(common.mustCall(() => {
200-
assert.strictEqual(signal.listenerCount('abort'), 0);
201-
}));
202-
}
203-
204-
{
205-
// Check that timer adding signals does not leak handlers
206-
const signal = new NodeEventTarget();
207-
signal.aborted = false;
208-
setPromiseImmediate(0, { signal }).finally(common.mustCall(() => {
209-
assert.strictEqual(signal.listenerCount('abort'), 0);
210-
}));
211-
}
212-
213119
{
214120
// Check that timer adding signals does not leak handlers
215121
const signal = new NodeEventTarget();
@@ -247,72 +153,6 @@ process.on('multipleResolves', common.mustNotCall());
247153
tryBreak().then(common.mustCall());
248154
}
249155

250-
{
251-
Promise.all(
252-
[1, '', false, Infinity].map(
253-
(i) => assert.rejects(setPromiseImmediate(10, i), {
254-
code: 'ERR_INVALID_ARG_TYPE'
255-
})
256-
)
257-
).then(common.mustCall());
258-
259-
Promise.all(
260-
[1, '', false, Infinity, null, {}].map(
261-
(signal) => assert.rejects(setPromiseImmediate(10, { signal }), {
262-
code: 'ERR_INVALID_ARG_TYPE'
263-
})
264-
)
265-
).then(common.mustCall());
266-
267-
Promise.all(
268-
[1, '', Infinity, null, {}].map(
269-
(ref) => assert.rejects(setPromiseImmediate(10, { ref }), {
270-
code: 'ERR_INVALID_ARG_TYPE'
271-
})
272-
)
273-
).then(common.mustCall());
274-
275-
Promise.all(
276-
[1, '', false, Infinity].map(
277-
(i) => assert.rejects(setPromiseTimeout(10, null, i), {
278-
code: 'ERR_INVALID_ARG_TYPE'
279-
})
280-
)
281-
).then(common.mustCall());
282-
283-
Promise.all(
284-
[1, '', false, Infinity, null, {}].map(
285-
(signal) => assert.rejects(setPromiseTimeout(10, null, { signal }), {
286-
code: 'ERR_INVALID_ARG_TYPE'
287-
})
288-
)
289-
).then(common.mustCall());
290-
291-
Promise.all(
292-
[1, '', Infinity, null, {}].map(
293-
(ref) => assert.rejects(setPromiseTimeout(10, null, { ref }), {
294-
code: 'ERR_INVALID_ARG_TYPE'
295-
})
296-
)
297-
).then(common.mustCall());
298-
}
299-
300-
{
301-
exec(`${process.execPath} -pe "const assert = require('assert');` +
302-
'require(\'timers/promises\').setTimeout(1000, null, { ref: false }).' +
303-
'then(assert.fail)"').then(common.mustCall(({ stderr }) => {
304-
assert.strictEqual(stderr, '');
305-
}));
306-
}
307-
308-
{
309-
exec(`${process.execPath} -pe "const assert = require('assert');` +
310-
'require(\'timers/promises\').setImmediate(null, { ref: false }).' +
311-
'then(assert.fail)"').then(common.mustCall(({ stderr }) => {
312-
assert.strictEqual(stderr, '');
313-
}));
314-
}
315-
316156
{
317157
exec(`${process.execPath} -pe "const assert = require('assert');` +
318158
'const interval = require(\'timers/promises\')' +

0 commit comments

Comments
 (0)