Skip to content

Commit 380a5d5

Browse files
TrottMylesBorins
authored andcommitted
test: fix flaky test-http-client-timeout-with-data
test-http-client-timeout-with-data has failed here and there in CI on FreeBSD and OS X. The test has a socket timeout set to 50ms and a timer set for 100ms. However, they are not necessarily set in the same tick of the event loop and their ordering is therefore not guaranteed. Instead of using a timer, this change listens for an event on the listener to know when the socket timeout has occurred and then runs the code originally in the timer. Additional refactoring: Replaced `process.on('exit', ...)` checks with `common.mustCall()` and replaced usage of `assert.equal()` with `assert.strictEqual()`. PR-URL: #10431 Reviewed-By: James M Snell <[email protected]>
1 parent 14e07c9 commit 380a5d5

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

test/parallel/test-http-client-timeout-with-data.js

+11-17
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@ const common = require('../common');
33
const assert = require('assert');
44
const http = require('http');
55

6-
var ntimeouts = 0;
76
var nchunks = 0;
87

9-
process.on('exit', function() {
10-
assert.equal(ntimeouts, 1);
11-
assert.equal(nchunks, 2);
12-
});
13-
148
const options = {
159
method: 'GET',
1610
port: undefined,
@@ -21,7 +15,7 @@ const options = {
2115
const server = http.createServer(function(req, res) {
2216
res.writeHead(200, {'Content-Length': '2'});
2317
res.write('*');
24-
setTimeout(function() { res.end('*'); }, common.platformTimeout(100));
18+
server.once('timeout', common.mustCall(function() { res.end('*'); }));
2519
});
2620

2721
server.listen(0, options.host, function() {
@@ -30,19 +24,19 @@ server.listen(0, options.host, function() {
3024
req.end();
3125

3226
function onresponse(res) {
33-
req.setTimeout(50, function() {
34-
assert.equal(nchunks, 1); // should have received the first chunk by now
35-
ntimeouts++;
36-
});
27+
req.setTimeout(50, common.mustCall(function() {
28+
assert.strictEqual(nchunks, 1); // should have received the first chunk
29+
server.emit('timeout');
30+
}));
3731

38-
res.on('data', function(data) {
39-
assert.equal('' + data, '*');
32+
res.on('data', common.mustCall(function(data) {
33+
assert.strictEqual('' + data, '*');
4034
nchunks++;
41-
});
35+
}, 2));
4236

43-
res.on('end', function() {
44-
assert.equal(nchunks, 2);
37+
res.on('end', common.mustCall(function() {
38+
assert.strictEqual(nchunks, 2);
4539
server.close();
46-
});
40+
}));
4741
}
4842
});

0 commit comments

Comments
 (0)