From 1a7ca46544ff891cf1b05e514d10bbb511c0f8f4 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Fri, 23 Dec 2016 12:54:32 -0800 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/10431 Reviewed-By: James M Snell --- .../test-http-client-timeout-with-data.js | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) rename test/{sequential => parallel}/test-http-client-timeout-with-data.js (54%) diff --git a/test/sequential/test-http-client-timeout-with-data.js b/test/parallel/test-http-client-timeout-with-data.js similarity index 54% rename from test/sequential/test-http-client-timeout-with-data.js rename to test/parallel/test-http-client-timeout-with-data.js index f5b2a60dc5..c82327beb2 100644 --- a/test/sequential/test-http-client-timeout-with-data.js +++ b/test/parallel/test-http-client-timeout-with-data.js @@ -3,14 +3,8 @@ const common = require('../common'); const assert = require('assert'); const http = require('http'); -var ntimeouts = 0; var nchunks = 0; -process.on('exit', function() { - assert.equal(ntimeouts, 1); - assert.equal(nchunks, 2); -}); - const options = { method: 'GET', port: undefined, @@ -21,7 +15,7 @@ const options = { const server = http.createServer(function(req, res) { res.writeHead(200, {'Content-Length': '2'}); res.write('*'); - setTimeout(function() { res.end('*'); }, common.platformTimeout(100)); + server.once('timeout', common.mustCall(function() { res.end('*'); })); }); server.listen(0, options.host, function() { @@ -30,19 +24,19 @@ server.listen(0, options.host, function() { req.end(); function onresponse(res) { - req.setTimeout(50, function() { - assert.equal(nchunks, 1); // should have received the first chunk by now - ntimeouts++; - }); + req.setTimeout(50, common.mustCall(function() { + assert.strictEqual(nchunks, 1); // should have received the first chunk + server.emit('timeout'); + })); - res.on('data', function(data) { - assert.equal('' + data, '*'); + res.on('data', common.mustCall(function(data) { + assert.strictEqual('' + data, '*'); nchunks++; - }); + }, 2)); - res.on('end', function() { - assert.equal(nchunks, 2); + res.on('end', common.mustCall(function() { + assert.strictEqual(nchunks, 2); server.close(); - }); + })); } });