Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix flaky http(s)-set-timeout-server tests #14380

Closed
wants to merge 8 commits into from
Closed
59 changes: 49 additions & 10 deletions test/parallel/test-http-set-timeout-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ function run() {
}

test(function serverTimeout(cb) {
const server = http.createServer((req, res) => {
// Do nothing. We should get a timeout event.
// Might not be invoked. Do not wrap in common.mustCall().
});
const server = http.createServer();
server.listen(common.mustCall(() => {
const s = server.setTimeout(50, common.mustCall((socket) => {
socket.destroy();
Expand Down Expand Up @@ -125,11 +122,14 @@ test(function serverResponseTimeoutWithPipeline(cb) {
const server = http.createServer((req, res) => {
if (req.url === '/2')
secReceived = true;
if (req.url === '/1') {
res.end();
return;
}
const s = res.setTimeout(50, () => {
caughtTimeout += req.url;
});
assert.ok(s instanceof http.OutgoingMessage);
if (req.url === '/1') res.end();
});
server.on('timeout', common.mustCall((socket) => {
if (secReceived) {
Expand All @@ -152,17 +152,56 @@ test(function serverResponseTimeoutWithPipeline(cb) {
});

test(function idleTimeout(cb) {
const server = http.createServer(common.mustCall((req, res) => {
req.on('timeout', common.mustNotCall());
res.on('timeout', common.mustNotCall());
res.end();
}));
// Test that the an idle connection invokes the timeout callback.
const server = http.createServer();
const s = server.setTimeout(50, common.mustCall((socket) => {
socket.destroy();
server.close();
cb();
}));
assert.ok(s instanceof http.Server);
server.listen(common.mustCall(() => {
const options = {
port: server.address().port,
allowHalfOpen: true,
};
const c = net.connect(options, () => {
// ECONNRESET could happen on a heavily-loaded server.
c.on('error', (e) => {
if (e.message !== 'read ECONNRESET')
throw e;
});
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
// Keep-Alive
});
}));
});

test(function fastTimeout(cb) {
let connectionHandlerInvoked = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment... (this one tests that timeout callback is fired for the socket but not for req)

let timeoutHandlerInvoked = false;
let connectionSocket;

function invokeCallbackIfDone() {
if (connectionHandlerInvoked && timeoutHandlerInvoked) {
connectionSocket.destroy();
server.close();
cb();
}
}

const server = http.createServer(common.mustCall((req, res) => {
req.on('timeout', common.mustNotCall());
res.end();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened to res.on('timeout', common.mustNotCall());?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be emitted so common.mustNotCall() was wrong. See #14380 (comment).

connectionHandlerInvoked = true;
invokeCallbackIfDone();
}));
const s = server.setTimeout(1, common.mustCall((socket) => {
connectionSocket = socket;
timeoutHandlerInvoked = true;
invokeCallbackIfDone();
}));
assert.ok(s instanceof http.Server);
server.listen(common.mustCall(() => {
const options = {
port: server.address().port,
Expand Down
67 changes: 53 additions & 14 deletions test/parallel/test-https-set-timeout-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ function run() {
}

test(function serverTimeout(cb) {
const server = https.createServer(
serverOptions,
(req, res) => {
// Do nothing. We should get a timeout event.
// Might not be invoked. Do not wrap in common.mustCall().
});
const server = https.createServer(serverOptions);
server.listen(common.mustCall(() => {
const s = server.setTimeout(50, common.mustCall((socket) => {
socket.destroy();
Expand Down Expand Up @@ -147,11 +142,14 @@ test(function serverResponseTimeoutWithPipeline(cb) {
const server = https.createServer(serverOptions, (req, res) => {
if (req.url === '/2')
secReceived = true;
if (req.url === '/1') {
res.end();
return;
}
const s = res.setTimeout(50, () => {
caughtTimeout += req.url;
});
assert.ok(s instanceof http.OutgoingMessage);
if (req.url === '/1') res.end();
});
server.on('timeout', common.mustCall((socket) => {
if (secReceived) {
Expand All @@ -175,19 +173,60 @@ test(function serverResponseTimeoutWithPipeline(cb) {
});

test(function idleTimeout(cb) {
const server = https.createServer(
serverOptions,
common.mustCall((req, res) => {
req.on('timeout', common.mustNotCall());
res.on('timeout', common.mustNotCall());
res.end();
}));
// Test that the an idle connection invokes the timeout callback.
const server = https.createServer(serverOptions);
const s = server.setTimeout(50, common.mustCall((socket) => {
socket.destroy();
server.close();
cb();
}));
assert.ok(s instanceof https.Server);
server.listen(common.mustCall(() => {
const options = {
port: server.address().port,
allowHalfOpen: true,
rejectUnauthorized: false
};
const c = tls.connect(options, () => {
// ECONNRESET could happen on a heavily-loaded server.
c.on('error', (e) => {
if (e.message !== 'read ECONNRESET')
throw e;
});
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
// Keep-Alive
});
}));
});

test(function fastTimeout(cb) {
// Test that the socket timeout fires but no timeout fires for the request.
let connectionHandlerInvoked = false;
let timeoutHandlerInvoked = false;
let connectionSocket;

function invokeCallbackIfDone() {
if (connectionHandlerInvoked && timeoutHandlerInvoked) {
connectionSocket.destroy();
server.close();
cb();
}
}

const server = https.createServer(serverOptions, common.mustCall(
(req, res) => {
req.on('timeout', common.mustNotCall());
res.end();
connectionHandlerInvoked = true;
invokeCallbackIfDone();
}
));
const s = server.setTimeout(1, common.mustCall((socket) => {
connectionSocket = socket;
timeoutHandlerInvoked = true;
invokeCallbackIfDone();
}));
assert.ok(s instanceof https.Server);
server.listen(common.mustCall(() => {
const options = {
port: server.address().port,
Expand Down