Skip to content

Commit

Permalink
test: fix flaky timeout-delayed-body and headers tests
Browse files Browse the repository at this point in the history
fix the flaky test-http-server-request-timeout-delayed-body
and test-http-server-request-timeout-delayed-headers which
sometimes fail on slow systems.

PR-URL: #38045
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
Linkgoron authored and targos committed May 1, 2021
1 parent 5d2f0d0 commit 1cb3d89
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 24 deletions.
12 changes: 9 additions & 3 deletions test/parallel/test-http-server-request-timeout-delayed-body.js
Expand Up @@ -9,6 +9,7 @@ const { connect } = require('net');
// after server.requestTimeout if the client
// pauses before start sending the body.

let sendDelayedRequestBody;
const server = createServer(common.mustCall((req, res) => {
let body = '';
req.setEncoding('utf-8');
Expand All @@ -22,6 +23,9 @@ const server = createServer(common.mustCall((req, res) => {
res.write(body);
res.end();
});

assert.strictEqual(typeof sendDelayedRequestBody, 'function');
sendDelayedRequestBody();
}));

// 0 seconds is the default
Expand All @@ -44,9 +48,11 @@ server.listen(0, common.mustCall(() => {
client.write('Connection: close\r\n');
client.write('\r\n');

setTimeout(() => {
client.write('12345678901234567890\r\n\r\n');
}, common.platformTimeout(2000)).unref();
sendDelayedRequestBody = common.mustCall(() => {
setTimeout(() => {
client.write('12345678901234567890\r\n\r\n');
}, common.platformTimeout(2000)).unref();
});

const errOrEnd = common.mustCall(function(err) {
console.log(err);
Expand Down
21 changes: 13 additions & 8 deletions test/parallel/test-http-server-request-timeout-delayed-headers.js
Expand Up @@ -8,9 +8,12 @@ const { connect } = require('net');
// This test validates that the server returns 408
// after server.requestTimeout if the client
// pauses before start sending the request.

let sendDelayedRequestHeaders;
const server = createServer(common.mustNotCall());

server.on('connection', common.mustCall(() => {
assert.strictEqual(typeof sendDelayedRequestHeaders, 'function');
sendDelayedRequestHeaders();
}));
// 0 seconds is the default
assert.strictEqual(server.requestTimeout, 0);
const requestTimeout = common.platformTimeout(1000);
Expand Down Expand Up @@ -39,10 +42,12 @@ server.listen(0, common.mustCall(() => {

client.resume();

setTimeout(() => {
client.write('POST / HTTP/1.1\r\n');
client.write('Content-Length: 20\r\n');
client.write('Connection: close\r\n\r\n');
client.write('12345678901234567890\r\n\r\n');
}, common.platformTimeout(2000)).unref();
sendDelayedRequestHeaders = common.mustCall(() => {
setTimeout(() => {
client.write('POST / HTTP/1.1\r\n');
client.write('Content-Length: 20\r\n');
client.write('Connection: close\r\n\r\n');
client.write('12345678901234567890\r\n\r\n');
}, common.platformTimeout(2000)).unref();
});
}));
Expand Up @@ -8,7 +8,7 @@ const { connect } = require('net');
// This test validates that the server returns 408
// after server.requestTimeout if the client
// pauses sending in the middle of the body.

let sendDelayedRequestBody;
const server = createServer(common.mustCall((req, res) => {
let body = '';
req.setEncoding('utf-8');
Expand All @@ -22,6 +22,9 @@ const server = createServer(common.mustCall((req, res) => {
res.write(body);
res.end();
});

assert.strictEqual(typeof sendDelayedRequestBody, 'function');
sendDelayedRequestBody();
}));

// 0 seconds is the default
Expand Down Expand Up @@ -57,7 +60,9 @@ server.listen(0, common.mustCall(() => {
client.write('\r\n');
client.write('1234567890');

setTimeout(() => {
client.write('1234567890\r\n\r\n');
}, common.platformTimeout(2000)).unref();
sendDelayedRequestBody = common.mustCall(() => {
setTimeout(() => {
client.write('1234567890\r\n\r\n');
}, common.platformTimeout(2000)).unref();
});
}));
Expand Up @@ -8,8 +8,12 @@ const { connect } = require('net');
// This test validates that the server returns 408
// after server.requestTimeout if the client
// pauses sending in the middle of a header.

let sendDelayedRequestHeaders;
const server = createServer(common.mustNotCall());
server.on('connection', common.mustCall(() => {
assert.strictEqual(typeof sendDelayedRequestHeaders, 'function');
sendDelayedRequestHeaders();
}));

// 120 seconds is the default
assert.strictEqual(server.requestTimeout, 0);
Expand Down Expand Up @@ -42,7 +46,9 @@ server.listen(0, common.mustCall(() => {
client.write('Connection: close\r\n');
client.write('X-CRASH: ');

setTimeout(() => {
client.write('1234567890\r\n\r\n');
}, common.platformTimeout(2000)).unref();
sendDelayedRequestHeaders = common.mustCall(() => {
setTimeout(() => {
client.write('1234567890\r\n\r\n');
}, common.platformTimeout(2000)).unref();
});
}));
16 changes: 11 additions & 5 deletions test/parallel/test-http-server-request-timeout-upgrade.js
Expand Up @@ -7,8 +7,12 @@ const { connect } = require('net');

// This test validates that the requestTimeoout
// is disabled after the connection is upgraded.

let sendDelayedRequestHeaders;
const server = createServer(common.mustNotCall());
server.on('connection', common.mustCall(() => {
assert.strictEqual(typeof sendDelayedRequestHeaders, 'function');
sendDelayedRequestHeaders();
}));

// 0 seconds is the default
assert.strictEqual(server.requestTimeout, 0);
Expand Down Expand Up @@ -48,8 +52,10 @@ server.listen(0, common.mustCall(() => {
client.write('Upgrade: WebSocket\r\n');
client.write('Connection: Upgrade\r\n\r\n');

setTimeout(() => {
client.write('12345678901234567890');
client.end();
}, common.platformTimeout(2000)).unref();
sendDelayedRequestHeaders = common.mustCall(() => {
setTimeout(() => {
client.write('12345678901234567890');
client.end();
}, common.platformTimeout(2000)).unref();
});
}));

0 comments on commit 1cb3d89

Please sign in to comment.