node/test/parallel/test-http-client-timeout-option-listeners.js
Rich Trott 4a5c719be5 test: fix http-client-timeout-option-listeners
test-http-client-timeout-option-listeners is flaky due to depending on
completing operations before a 100ms socket timeout. The socket timeout
is an integral part of the test but can be very large. Set to the
maximum allowable value.

PR-URL: https://github.com/nodejs/node/pull/10224
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
2016-12-14 12:45:20 -08:00

48 lines
1.2 KiB
JavaScript

'use strict';
const common = require('../common');
const http = require('http');
const assert = require('assert');
const agent = new http.Agent({ keepAlive: true });
const server = http.createServer((req, res) => {
res.end('');
});
// Maximum allowed value for timeouts
const timeout = 2 ** 31 - 1;
const options = {
agent,
method: 'GET',
port: undefined,
host: common.localhostIPv4,
path: '/',
timeout: timeout
};
server.listen(0, options.host, common.mustCall(() => {
options.port = server.address().port;
doRequest(common.mustCall((numListeners) => {
assert.strictEqual(numListeners, 1);
doRequest(common.mustCall((numListeners) => {
assert.strictEqual(numListeners, 1);
server.close();
agent.destroy();
}));
}));
}));
function doRequest(cb) {
http.request(options, common.mustCall((response) => {
const sockets = agent.sockets[`${options.host}:${options.port}:`];
assert.strictEqual(sockets.length, 1);
const socket = sockets[0];
const numListeners = socket.listeners('timeout').length;
response.resume();
response.once('end', common.mustCall(() => {
process.nextTick(cb, numListeners);
}));
})).end();
}