test: use countdown in test file
Fixes: https://github.com/nodejs/node/issues/17169 PR-URL: https://github.com/nodejs/node/pull/17874 Fixes: https://github.com/nodejs/node/issues/17169 Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
parent
a82b1b7b30
commit
166a2ceeb7
@ -28,6 +28,7 @@ const fixtures = require('../common/fixtures');
|
|||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const Countdown = require('../common/countdown');
|
||||||
|
|
||||||
http.globalAgent.maxSockets = 1;
|
http.globalAgent.maxSockets = 1;
|
||||||
|
|
||||||
@ -38,14 +39,12 @@ const image = fixtures.readSync('/person.jpg');
|
|||||||
console.log(`image.length = ${image.length}`);
|
console.log(`image.length = ${image.length}`);
|
||||||
|
|
||||||
const total = 10;
|
const total = 10;
|
||||||
let requests = 0;
|
const responseCountdown = new Countdown(total, common.mustCall(() => {
|
||||||
let responses = 0;
|
checkFiles();
|
||||||
|
server.close();
|
||||||
|
}));
|
||||||
|
|
||||||
const server = http.Server(function(req, res) {
|
const server = http.Server(function(req, res) {
|
||||||
if (++requests === total) {
|
|
||||||
server.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
res.writeHead(200, {
|
res.writeHead(200, {
|
||||||
'content-type': 'image/jpeg',
|
'content-type': 'image/jpeg',
|
||||||
@ -74,9 +73,7 @@ server.listen(0, function() {
|
|||||||
|
|
||||||
s.on('finish', function() {
|
s.on('finish', function() {
|
||||||
console.error(`done ${x}`);
|
console.error(`done ${x}`);
|
||||||
if (++responses === total) {
|
responseCountdown.dec();
|
||||||
checkFiles();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}).on('error', function(e) {
|
}).on('error', function(e) {
|
||||||
console.error('error! ', e.message);
|
console.error('error! ', e.message);
|
||||||
@ -86,8 +83,6 @@ server.listen(0, function() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
let checkedFiles = false;
|
|
||||||
function checkFiles() {
|
function checkFiles() {
|
||||||
// Should see 1.jpg, 2.jpg, ..., 100.jpg in tmpDir
|
// Should see 1.jpg, 2.jpg, ..., 100.jpg in tmpDir
|
||||||
const files = fs.readdirSync(common.tmpDir);
|
const files = fs.readdirSync(common.tmpDir);
|
||||||
@ -101,13 +96,4 @@ function checkFiles() {
|
|||||||
image.length, stat.size,
|
image.length, stat.size,
|
||||||
`size doesn't match on '${fn}'. Got ${stat.size} bytes`);
|
`size doesn't match on '${fn}'. Got ${stat.size} bytes`);
|
||||||
}
|
}
|
||||||
|
|
||||||
checkedFiles = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
process.on('exit', function() {
|
|
||||||
assert.strictEqual(total, requests);
|
|
||||||
assert.strictEqual(total, responses);
|
|
||||||
assert.ok(checkedFiles);
|
|
||||||
});
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user