Skip to content

Commit 87d5741

Browse files
BamiehMylesBorins
authored andcommitted
test: wrap countdown callback in common.mustCall
This adds a implicit common.mustCall to the callback provided to the countdown. PR-URL: #18506 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6604a2d commit 87d5741

15 files changed

+55
-29
lines changed

test/common/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ Synchronous version of `spawnPwd`.
385385
The `Countdown` module provides a simple countdown mechanism for tests that
386386
require a particular action to be taken after a given number of completed
387387
tasks (for instance, shutting down an HTTP server after a specific number of
388-
requests).
388+
requests). The Countdown will fail the test if the remainder did not reach 0.
389389

390390
<!-- eslint-disable strict, required-modules -->
391391
```js

test/common/countdown.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
const assert = require('assert');
55
const kLimit = Symbol('limit');
66
const kCallback = Symbol('callback');
7+
const common = require('./');
78

89
class Countdown {
910
constructor(limit, cb) {
1011
assert.strictEqual(typeof limit, 'number');
1112
assert.strictEqual(typeof cb, 'function');
1213
this[kLimit] = limit;
13-
this[kCallback] = cb;
14+
this[kCallback] = common.mustCall(cb);
1415
}
1516

1617
dec() {

test/fixtures/failcounter.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const Countdown = require('../common/countdown');
2+
new Countdown(2, () => {});

test/parallel/test-common-countdown.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,31 @@
33
const common = require('../common');
44
const assert = require('assert');
55
const Countdown = require('../common/countdown');
6+
const fixtures = require('../common/fixtures');
7+
const { execFile } = require('child_process');
68

79
let done = '';
8-
9-
const countdown = new Countdown(2, common.mustCall(() => done = true));
10+
const countdown = new Countdown(2, () => done = true);
1011
assert.strictEqual(countdown.remaining, 2);
1112
countdown.dec();
1213
assert.strictEqual(countdown.remaining, 1);
1314
countdown.dec();
1415
assert.strictEqual(countdown.remaining, 0);
1516
assert.strictEqual(done, true);
17+
18+
const failFixtures = [
19+
[
20+
fixtures.path('failcounter.js'),
21+
'Mismatched <anonymous> function calls. Expected exactly 1, actual 0.',
22+
]
23+
];
24+
25+
for (const p of failFixtures) {
26+
const [file, expected] = p;
27+
execFile(process.argv[0], [file], common.mustCall((ex, stdout, stderr) => {
28+
assert.ok(ex);
29+
assert.strictEqual(stderr, '');
30+
const firstLine = stdout.split('\n').shift();
31+
assert.strictEqual(firstLine, expected);
32+
}));
33+
}

test/parallel/test-http-after-connect.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const server = http.createServer(common.mustCall((req, res) => {
3232
setTimeout(() => res.end(req.url), 50);
3333
}, 2));
3434

35-
const countdown = new Countdown(2, common.mustCall(() => server.close()));
35+
const countdown = new Countdown(2, () => server.close());
3636

3737
server.on('connect', common.mustCall((req, socket) => {
3838
socket.write('HTTP/1.1 200 Connection established\r\n\r\n');

test/parallel/test-http-agent-destroyed-socket.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const server = http.createServer(common.mustCall((req, res) => {
8080
assert(request1.socket.destroyed);
8181
// assert not reusing the same socket, since it was destroyed.
8282
assert.notStrictEqual(request1.socket, request2.socket);
83-
const countdown = new Countdown(2, common.mustCall(() => server.close()));
83+
const countdown = new Countdown(2, () => server.close());
8484
request2.socket.on('close', common.mustCall(() => countdown.dec()));
8585
response.on('end', common.mustCall(() => countdown.dec()));
8686
response.resume();

test/parallel/test-http-agent-maxsockets-regress-4050.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const server = http.createServer(common.mustCall((req, res) => {
1717
res.end('hello world');
1818
}, 6));
1919

20-
const countdown = new Countdown(6, common.mustCall(() => server.close()));
20+
const countdown = new Countdown(6, () => server.close());
2121

2222
function get(path, callback) {
2323
return http.get({

test/parallel/test-http-agent-maxsockets.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ function get(path, callback) {
2626
}, callback);
2727
}
2828

29-
const countdown = new Countdown(2, common.mustCall(() => {
29+
const countdown = new Countdown(2, () => {
3030
const freepool = agent.freeSockets[Object.keys(agent.freeSockets)[0]];
3131
assert.strictEqual(freepool.length, 2,
3232
`expect keep 2 free sockets, but got ${freepool.length}`);
3333
agent.destroy();
3434
server.close();
35-
}));
35+
});
3636

3737
function dec() {
3838
process.nextTick(() => countdown.dec());

test/parallel/test-http-client-abort.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const Countdown = require('../common/countdown');
2626

2727
const N = 8;
2828

29-
const countdown = new Countdown(N, common.mustCall(() => server.close()));
29+
const countdown = new Countdown(N, () => server.close());
3030

3131
const server = http.Server(common.mustCall((req, res) => {
3232
res.writeHead(200);
@@ -37,9 +37,9 @@ const server = http.Server(common.mustCall((req, res) => {
3737
server.listen(0, common.mustCall(() => {
3838

3939
const requests = [];
40-
const reqCountdown = new Countdown(N, common.mustCall(() => {
40+
const reqCountdown = new Countdown(N, () => {
4141
requests.forEach((req) => req.abort());
42-
}));
42+
});
4343

4444
const options = { port: server.address().port };
4545

test/parallel/test-http-client-parse-error.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const http = require('http');
2525
const net = require('net');
2626
const Countdown = require('../common/countdown');
2727

28-
const countdown = new Countdown(2, common.mustCall(() => server.close()));
28+
const countdown = new Countdown(2, () => server.close());
2929

3030
const payloads = [
3131
'HTTP/1.1 302 Object Moved\r\nContent-Length: 0\r\n\r\nhi world',

test/parallel/test-http-exceptions.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121

2222
'use strict';
2323
require('../common');
24+
const Countdown = require('../common/countdown');
2425
const http = require('http');
26+
const NUMBER_OF_EXCEPTIONS = 4;
27+
const countdown = new Countdown(NUMBER_OF_EXCEPTIONS, () => {
28+
process.exit(0);
29+
});
2530

2631
const server = http.createServer(function(req, res) {
2732
intentionally_not_defined(); // eslint-disable-line no-undef
@@ -30,16 +35,16 @@ const server = http.createServer(function(req, res) {
3035
res.end();
3136
});
3237

38+
function onUncaughtException(err) {
39+
console.log(`Caught an exception: ${err}`);
40+
if (err.name === 'AssertionError') throw err;
41+
countdown.dec();
42+
}
43+
44+
process.on('uncaughtException', onUncaughtException);
45+
3346
server.listen(0, function() {
34-
for (let i = 0; i < 4; i += 1) {
47+
for (let i = 0; i < NUMBER_OF_EXCEPTIONS; i += 1) {
3548
http.get({ port: this.address().port, path: `/busy/${i}` });
3649
}
3750
});
38-
39-
let exception_count = 0;
40-
41-
process.on('uncaughtException', function(err) {
42-
console.log(`Caught an exception: ${err}`);
43-
if (err.name === 'AssertionError') throw err;
44-
if (++exception_count === 4) process.exit(0);
45-
});

test/parallel/test-http2-no-more-streams.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ server.listen(0, common.mustCall(() => {
2323

2424
assert.strictEqual(client.state.nextStreamID, nextID);
2525

26-
const countdown = new Countdown(2, common.mustCall(() => {
26+
const countdown = new Countdown(2, () => {
2727
server.close();
2828
client.close();
29-
}));
29+
});
3030

3131
{
3232
// This one will be ok

test/parallel/test-http2-server-rst-stream.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ server.on('stream', (stream, headers) => {
3232
server.listen(0, common.mustCall(() => {
3333
const client = http2.connect(`http://localhost:${server.address().port}`);
3434

35-
const countdown = new Countdown(tests.length, common.mustCall(() => {
35+
const countdown = new Countdown(tests.length, () => {
3636
client.close();
3737
server.close();
38-
}));
38+
});
3939

4040
tests.forEach((test) => {
4141
const req = client.request({

test/parallel/test-performanceobserver.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ assert.strictEqual(counts[NODE_PERFORMANCE_ENTRY_TYPE_FUNCTION], 0);
6565
new PerformanceObserver(common.mustCall(callback, 3));
6666

6767
const countdown =
68-
new Countdown(3, common.mustCall(() => {
68+
new Countdown(3, () => {
6969
observer.disconnect();
7070
assert.strictEqual(counts[NODE_PERFORMANCE_ENTRY_TYPE_MARK], 1);
71-
}));
71+
});
7272

7373
function callback(list, obs) {
7474
assert.strictEqual(obs, observer);

test/parallel/test-timers-socket-timeout-removes-other-socket-unref-timer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const server = net.createServer(function onClient(client) {
3333
});
3434

3535
server.listen(0, common.localhostIPv4, common.mustCall(() => {
36-
const countdown = new Countdown(2, common.mustCall(() => server.close()));
36+
const countdown = new Countdown(2, () => server.close());
3737

3838
{
3939
const client = net.connect({ port: server.address().port });

0 commit comments

Comments
 (0)