Skip to content

Commit 7b8622f

Browse files
onneriMylesBorins
authored andcommitted
test: update http test to use Countdown
PR-URL: #17477 Refs: #17169 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Jon Moss <[email protected]>
1 parent 6aa6d41 commit 7b8622f

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

test/parallel/test-http-status-code.js

+8-17
Original file line numberDiff line numberDiff line change
@@ -23,45 +23,36 @@
2323
require('../common');
2424
const assert = require('assert');
2525
const http = require('http');
26+
const Countdown = require('../common/countdown');
2627

2728
// Simple test of Node's HTTP ServerResponse.statusCode
2829
// ServerResponse.prototype.statusCode
2930

30-
let testsComplete = 0;
3131
const tests = [200, 202, 300, 404, 451, 500];
32-
let testIdx = 0;
32+
let test;
33+
const countdown = new Countdown(tests.length, () => s.close());
3334

3435
const s = http.createServer(function(req, res) {
35-
const t = tests[testIdx];
36-
res.writeHead(t, { 'Content-Type': 'text/plain' });
36+
res.writeHead(test, { 'Content-Type': 'text/plain' });
3737
console.log(`--\nserver: statusCode after writeHead: ${res.statusCode}`);
38-
assert.strictEqual(res.statusCode, t);
38+
assert.strictEqual(res.statusCode, test);
3939
res.end('hello world\n');
4040
});
4141

4242
s.listen(0, nextTest);
4343

4444

4545
function nextTest() {
46-
if (testIdx + 1 === tests.length) {
47-
return s.close();
48-
}
49-
const test = tests[testIdx];
46+
test = tests.shift();
5047

5148
http.get({ port: s.address().port }, function(response) {
5249
console.log(`client: expected status: ${test}`);
5350
console.log(`client: statusCode: ${response.statusCode}`);
5451
assert.strictEqual(response.statusCode, test);
5552
response.on('end', function() {
56-
testsComplete++;
57-
testIdx += 1;
58-
nextTest();
53+
if (countdown.dec())
54+
nextTest();
5955
});
6056
response.resume();
6157
});
6258
}
63-
64-
65-
process.on('exit', function() {
66-
assert.strictEqual(5, testsComplete);
67-
});

0 commit comments

Comments
 (0)