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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 20 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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',

0 commit comments

Comments
 (0)