From e6bb8f09559cc4bb0937c5c4009d19d2bf6b357b Mon Sep 17 00:00:00 2001 From: Jithil P Ponnan Date: Sat, 18 Jun 2022 18:23:49 +0530 Subject: [PATCH 1/2] test: simplify the test cases in http timeouts mix --- ...test-http-server-request-timeouts-mixed.js | 118 +++++++----------- 1 file changed, 48 insertions(+), 70 deletions(-) diff --git a/test/parallel/test-http-server-request-timeouts-mixed.js b/test/parallel/test-http-server-request-timeouts-mixed.js index 44c8d03e3c17ae..8735791b610cd6 100644 --- a/test/parallel/test-http-server-request-timeouts-mixed.js +++ b/test/parallel/test-http-server-request-timeouts-mixed.js @@ -4,6 +4,7 @@ const common = require('../common'); const assert = require('assert'); const { createServer } = require('http'); const { connect } = require('net'); +const { promisify } = require('util'); // This test validates that request are correct checked for both requests and headers timeout in various situations. @@ -16,10 +17,13 @@ const responseTimeout = 'HTTP/1.1 408 Request Timeout\r\n'; const headersTimeout = common.platformTimeout(2000); const connectionsCheckingInterval = headersTimeout / 4; +const requestTimeout = headersTimeout * 2; +const threadSleepDelay = requestTimeout + headersTimeout; +const delay = promisify(setTimeout); const server = createServer({ headersTimeout, - requestTimeout: headersTimeout * 2, + requestTimeout, keepAliveTimeout: 0, connectionsCheckingInterval }, common.mustCall((req, res) => { @@ -32,7 +36,7 @@ const server = createServer({ }, 4)); assert.strictEqual(server.headersTimeout, headersTimeout); -assert.strictEqual(server.requestTimeout, headersTimeout * 2); +assert.strictEqual(server.requestTimeout, requestTimeout); let i = 0; function createClient(server) { @@ -58,74 +62,48 @@ function createClient(server) { return request; } -server.listen(0, common.mustCall(() => { +server.listen(0, common.mustCall(async () => { + // Create first and second request and set headers const request1 = createClient(server); - let request2; - let request3; - let request4; - let request5; - - // Send the first request and stop before the body + const request2 = createClient(server); request1.client.write(requestBodyPart1); - - // After a little while send two new requests - setTimeout(() => { - request2 = createClient(server); - request3 = createClient(server); - - // Send the second request, stop in the middle of the headers - request2.client.write(requestBodyPart1); - // Send the second request, stop in the middle of the headers - request3.client.write(requestBodyPart1); - }, headersTimeout * 0.2); - - // After another little while send the last two new requests - setTimeout(() => { - request4 = createClient(server); - request5 = createClient(server); - - // Send the fourth request, stop in the middle of the headers - request4.client.write(requestBodyPart1); - // Send the fifth request, stop in the middle of the headers - request5.client.write(requestBodyPart1); - }, headersTimeout * 0.6); - - setTimeout(() => { - // Finish the first request - request1.client.write(requestBodyPart2 + requestBodyPart3); - - // Complete headers for all requests but second - request3.client.write(requestBodyPart2); - request4.client.write(requestBodyPart2); - request5.client.write(requestBodyPart2); - }, headersTimeout * 0.8); - - setTimeout(() => { - // After the first timeout, the first request should have been completed and second timedout - assert(request1.completed); - assert(request2.completed); - assert(!request3.completed); - assert(!request4.completed); - assert(!request5.completed); - - assert(request1.response.startsWith(responseOk)); - assert(request2.response.startsWith(responseTimeout)); // It is expired due to headersTimeout - }, headersTimeout * 1.2 + connectionsCheckingInterval); - - setTimeout(() => { - // Complete the body for the fourth request - request4.client.write(requestBodyPart3); - }, headersTimeout * 1.5); - - setTimeout(() => { - // All request should be completed now, either with 200 or 408 - assert(request3.completed); - assert(request4.completed); - assert(request5.completed); - - assert(request3.response.startsWith(responseTimeout)); // It is expired due to requestTimeout - assert(request4.response.startsWith(responseOk)); - assert(request5.response.startsWith(responseTimeout)); // It is expired due to requestTimeout - server.close(); - }, headersTimeout * 3 + connectionsCheckingInterval); + request2.client.write(requestBodyPart1); + + // Finish the first request now and wait more than the request timeout value + request1.client.write(requestBodyPart2 + requestBodyPart3); + await delay(threadSleepDelay); + + // First and second request should complete now + assert(request1.completed); + assert(request2.completed); // Completed, but a timeout request + + assert(request1.response.startsWith(responseOk)); + assert(request2.response.startsWith(responseTimeout)); // Reques expired due to headersTimeout + + // Create another 3 requests with headers + const request3 = createClient(server); + const request4 = createClient(server); + const request5 = createClient(server); + request3.client.write(requestBodyPart1 + requestBodyPart2); + request4.client.write(requestBodyPart1 + requestBodyPart2); + request5.client.write(requestBodyPart1 + requestBodyPart2); + + // None of the requests will be completed because they only recieved headers + assert(!request3.completed); + assert(!request4.completed); + assert(!request5.completed); + + // Finish the fourth request now and wait more than the request timeout value + request4.client.write(requestBodyPart3); + await delay(threadSleepDelay); + + // All three requests should complete now + assert(request3.completed); + assert(request4.completed); + assert(request5.completed); + + assert(request3.response.startsWith(responseTimeout)); // It is expired due to headersTimeout + assert(request4.response.startsWith(responseOk)); + assert(request5.response.startsWith(responseTimeout)); // It is expired due to headersTimeout + server.close(); })); From bbc053875c7e8673d8bfbe50471a7f99ded889e3 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 27 Jun 2022 15:20:13 +0200 Subject: [PATCH 2/2] ensure there are no never settling promises --- test/parallel/test-http-server-request-timeouts-mixed.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-http-server-request-timeouts-mixed.js b/test/parallel/test-http-server-request-timeouts-mixed.js index 8735791b610cd6..a172721f933f9b 100644 --- a/test/parallel/test-http-server-request-timeouts-mixed.js +++ b/test/parallel/test-http-server-request-timeouts-mixed.js @@ -62,7 +62,7 @@ function createClient(server) { return request; } -server.listen(0, common.mustCall(async () => { +server.listen(0, common.mustCall(() => (async () => { // Create first and second request and set headers const request1 = createClient(server); const request2 = createClient(server); @@ -106,4 +106,4 @@ server.listen(0, common.mustCall(async () => { assert(request4.response.startsWith(responseOk)); assert(request5.response.startsWith(responseTimeout)); // It is expired due to headersTimeout server.close(); -})); +})().then(common.mustCall())));