test: refactor two http client timeout tests
Refactor test-http-client-set-timeout and test-http-client-timeout-option-with-agent. PR-URL: https://github.com/nodejs/node/pull/25473 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
40a8a73916
commit
b361f9577f
@ -1,46 +1,48 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
const common = require('../common');
|
|
||||||
|
|
||||||
// Test that `req.setTimeout` will fired exactly once.
|
// Test that the `'timeout'` event is emitted exactly once if the `timeout`
|
||||||
|
// option and `request.setTimeout()` are used together.
|
||||||
|
|
||||||
const assert = require('assert');
|
const { expectsError, mustCall } = require('../common');
|
||||||
const http = require('http');
|
const { strictEqual } = require('assert');
|
||||||
|
const { createServer, get } = require('http');
|
||||||
|
|
||||||
const HTTP_CLIENT_TIMEOUT = 2000;
|
const server = createServer(() => {
|
||||||
|
|
||||||
const options = {
|
|
||||||
method: 'GET',
|
|
||||||
port: undefined,
|
|
||||||
host: '127.0.0.1',
|
|
||||||
path: '/',
|
|
||||||
timeout: HTTP_CLIENT_TIMEOUT,
|
|
||||||
};
|
|
||||||
|
|
||||||
const server = http.createServer(() => {
|
|
||||||
// Never respond.
|
// Never respond.
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(0, options.host, function() {
|
server.listen(0, mustCall(() => {
|
||||||
doRequest();
|
const req = get({
|
||||||
|
port: server.address().port,
|
||||||
|
timeout: 2000,
|
||||||
});
|
});
|
||||||
|
|
||||||
function doRequest() {
|
req.setTimeout(1000);
|
||||||
options.port = server.address().port;
|
|
||||||
const req = http.request(options);
|
|
||||||
req.setTimeout(HTTP_CLIENT_TIMEOUT / 2);
|
|
||||||
req.on('error', () => {
|
|
||||||
// This space is intentionally left blank.
|
|
||||||
});
|
|
||||||
req.on('close', common.mustCall(() => server.close()));
|
|
||||||
|
|
||||||
let timeout_events = 0;
|
req.on('socket', mustCall((socket) => {
|
||||||
req.on('timeout', common.mustCall(() => {
|
strictEqual(socket.timeout, 2000);
|
||||||
timeout_events += 1;
|
|
||||||
|
socket.on('connect', mustCall(() => {
|
||||||
|
strictEqual(socket.timeout, 1000);
|
||||||
|
|
||||||
|
// Reschedule the timer to not wait 1 sec and make the test finish faster.
|
||||||
|
socket.setTimeout(10);
|
||||||
|
strictEqual(socket.timeout, 10);
|
||||||
|
}));
|
||||||
}));
|
}));
|
||||||
req.end();
|
|
||||||
|
|
||||||
setTimeout(function() {
|
req.on('error', expectsError({
|
||||||
|
type: Error,
|
||||||
|
code: 'ECONNRESET',
|
||||||
|
message: 'socket hang up'
|
||||||
|
}));
|
||||||
|
|
||||||
|
req.on('close', mustCall(() => {
|
||||||
|
server.close();
|
||||||
|
}));
|
||||||
|
|
||||||
|
req.on('timeout', mustCall(() => {
|
||||||
|
strictEqual(req.socket.listenerCount('timeout'), 0);
|
||||||
req.destroy();
|
req.destroy();
|
||||||
assert.strictEqual(timeout_events, 1);
|
}));
|
||||||
}, common.platformTimeout(HTTP_CLIENT_TIMEOUT));
|
}));
|
||||||
}
|
|
||||||
|
@ -1,58 +1,23 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
const common = require('../common');
|
|
||||||
|
|
||||||
// Test that when http request uses both timeout and agent,
|
// Test that the request `timeout` option has precedence over the agent
|
||||||
// timeout will work as expected.
|
// `timeout` option.
|
||||||
|
|
||||||
const assert = require('assert');
|
const { mustCall } = require('../common');
|
||||||
const http = require('http');
|
const { Agent, get } = require('http');
|
||||||
|
const { strictEqual } = require('assert');
|
||||||
|
|
||||||
const HTTP_AGENT_TIMEOUT = 1000;
|
const request = get({
|
||||||
const HTTP_CLIENT_TIMEOUT = 3000;
|
agent: new Agent({ timeout: 50 }),
|
||||||
|
lookup: () => {},
|
||||||
const agent = new http.Agent({ timeout: HTTP_AGENT_TIMEOUT });
|
timeout: 100
|
||||||
const options = {
|
|
||||||
method: 'GET',
|
|
||||||
port: undefined,
|
|
||||||
host: '127.0.0.1',
|
|
||||||
path: '/',
|
|
||||||
timeout: HTTP_CLIENT_TIMEOUT,
|
|
||||||
agent,
|
|
||||||
};
|
|
||||||
|
|
||||||
const server = http.createServer(() => {
|
|
||||||
// Never respond.
|
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(0, options.host, () => {
|
request.on('socket', mustCall((socket) => {
|
||||||
doRequest();
|
strictEqual(socket.timeout, 100);
|
||||||
});
|
|
||||||
|
|
||||||
function doRequest() {
|
const listeners = socket.listeners('timeout');
|
||||||
options.port = server.address().port;
|
|
||||||
const start = process.hrtime.bigint();
|
|
||||||
const req = http.request(options);
|
|
||||||
req.on('error', () => {
|
|
||||||
// This space is intentionally left blank.
|
|
||||||
});
|
|
||||||
req.on('close', common.mustCall(() => server.close()));
|
|
||||||
|
|
||||||
let timeout_events = 0;
|
strictEqual(listeners.length, 1);
|
||||||
req.on('timeout', common.mustCall(() => {
|
strictEqual(listeners[0], request.timeoutCb);
|
||||||
timeout_events += 1;
|
|
||||||
const duration = process.hrtime.bigint() - start;
|
|
||||||
// The timeout event cannot be precisely timed. It will delay
|
|
||||||
// some number of milliseconds.
|
|
||||||
assert.ok(
|
|
||||||
duration >= BigInt(HTTP_CLIENT_TIMEOUT * 1e6),
|
|
||||||
`duration ${duration}ms less than timeout ${HTTP_CLIENT_TIMEOUT}ms`
|
|
||||||
);
|
|
||||||
}));
|
}));
|
||||||
req.end();
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
req.destroy();
|
|
||||||
assert.strictEqual(timeout_events, 1);
|
|
||||||
// Ensure the `timeout` event fired only once.
|
|
||||||
}, common.platformTimeout(HTTP_CLIENT_TIMEOUT * 2));
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user