node/test/parallel/test-http-client-timeout-with-data.js
Rich Trott 0b33ef80f1 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 <jasnell@gmail.com>
2016-12-30 22:33:31 -08:00

43 lines
1014 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
var nchunks = 0;
const options = {
method: 'GET',
port: undefined,
host: '127.0.0.1',
path: '/'
};
const server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Length': '2'});
res.write('*');
server.once('timeout', common.mustCall(function() { res.end('*'); }));
});
server.listen(0, options.host, function() {
options.port = this.address().port;
const req = http.request(options, onresponse);
req.end();
function onresponse(res) {
req.setTimeout(50, common.mustCall(function() {
assert.strictEqual(nchunks, 1); // should have received the first chunk
server.emit('timeout');
}));
res.on('data', common.mustCall(function(data) {
assert.strictEqual('' + data, '*');
nchunks++;
}, 2));
res.on('end', common.mustCall(function() {
assert.strictEqual(nchunks, 2);
server.close();
}));
}
});