-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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: use consistent timeouts #42893
Closed
ShogunPanda
wants to merge
2
commits into
nodejs:master
from
ShogunPanda:http-test-timeouts-consistency
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
test/parallel/test-http-server-request-timeouts-mixed.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { createServer } = require('http'); | ||
const { connect } = require('net'); | ||
|
||
// This test validates that request are correct checked for both requests and headers timeout in various situations. | ||
|
||
const requestBodyPart1 = 'POST / HTTP/1.1\r\nContent-Length: 20\r\n'; | ||
const requestBodyPart2 = 'Connection: close\r\n\r\n1234567890'; | ||
const requestBodyPart3 = '1234567890'; | ||
|
||
const responseOk = 'HTTP/1.1 200 OK\r\n'; | ||
const responseTimeout = 'HTTP/1.1 408 Request Timeout\r\n'; | ||
|
||
const headersTimeout = common.platformTimeout(2000); | ||
const connectionsCheckingInterval = headersTimeout / 4; | ||
|
||
const server = createServer({ | ||
headersTimeout, | ||
requestTimeout: headersTimeout * 2, | ||
keepAliveTimeout: 0, | ||
connectionsCheckingInterval | ||
}, common.mustCall((req, res) => { | ||
req.resume(); | ||
|
||
req.on('end', () => { | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
res.end(); | ||
}); | ||
}, 4)); | ||
|
||
assert.strictEqual(server.headersTimeout, headersTimeout); | ||
assert.strictEqual(server.requestTimeout, headersTimeout * 2); | ||
|
||
let i = 0; | ||
function createClient(server) { | ||
const request = { | ||
index: i++, | ||
client: connect(server.address().port), | ||
response: '', | ||
completed: false | ||
}; | ||
|
||
request.client.on('data', common.mustCallAtLeast((chunk) => { | ||
request.response += chunk.toString('utf-8'); | ||
})); | ||
|
||
request.client.on('end', common.mustCall(() => { | ||
request.completed = true; | ||
})); | ||
|
||
request.client.on('error', common.mustNotCall()); | ||
|
||
request.client.resume(); | ||
|
||
return request; | ||
} | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const request1 = createClient(server); | ||
let request2; | ||
let request3; | ||
let request4; | ||
let request5; | ||
|
||
// Send the first request and stop before the body | ||
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); | ||
})); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Relying on timers is the most frequent source of flakiness in tests. Apart from doing it concurrently with multiple requests, does this test tests something not already tested in existing tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, timers are messy. But unfortunately this test is about testing timers. :(
Yes, it checks that the concurrent checking of
requestTimeout
andheadersTimeout
works properly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My point is, are
requestTimeout
andheadersTimeout
already tested with a single request? If so what makes this test special? Why wouldn't the options work with multiple concurrent request if they work with a single one?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, they should, but I thought adding a more complex "real world like" scenario would have helped for future regression testing.