-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
test-http-server-consumed-timeout.js
52 lines (43 loc) · 1.25 KB
/
test-http-server-consumed-timeout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
let intervalWasInvoked = false;
const TIMEOUT = common.platformTimeout(50);
runTest(TIMEOUT);
function runTest(timeoutDuration) {
const server = http.createServer((req, res) => {
server.close();
res.writeHead(200);
res.flushHeaders();
req.setTimeout(timeoutDuration, () => {
if (!intervalWasInvoked) {
// Interval wasn't invoked, probably because the machine is busy with
// other things. Try again with a longer timeout.
console.error(`Retrying with timeout of ${timeoutDuration * 2}.`);
return setImmediate(() => { runTest(timeoutDuration * 2); });
}
assert.fail('Request timeout should not fire');
});
req.resume();
req.once('end', () => {
res.end();
});
});
server.listen(0, common.mustCall(() => {
const req = http.request({
port: server.address().port,
method: 'POST'
}, () => {
const interval = setInterval(() => {
intervalWasInvoked = true;
req.write('a');
}, 25);
setTimeout(() => {
clearInterval(interval);
req.end();
}, timeoutDuration);
});
req.write('.');
}));
}