MCPcopy Index your code
hub / github.com/nodejs/node / runTest

Function runTest

test/parallel/test-http-server-consumed-timeout.js:15–84  ·  view source on GitHub ↗
(timeoutDuration)

Source from the content-addressed store, hash-verified

13runTest(TIMEOUT);
14
15function runTest(timeoutDuration) {
16 let intervalWasInvoked = false;
17 let newTimeoutDuration = 0;
18
19 const server = http.createServer(common.mustCallAtLeast((req, res) => {
20 server.close(common.mustSucceed(() => {
21 if (newTimeoutDuration) {
22 runTest(newTimeoutDuration);
23 }
24 }));
25
26 res.writeHead(200);
27 res.flushHeaders();
28
29 req.setTimeout(timeoutDuration, () => {
30 if (!intervalWasInvoked) {
31 // Interval wasn't invoked, probably because the machine is busy with
32 // other things. Try again with a longer timeout.
33 newTimeoutDuration = timeoutDuration * 2;
34 console.error('The interval was not invoked.');
35 console.error(`Trying w/ timeout of ${newTimeoutDuration}.`);
36 return;
37 }
38
39 if (timeoutTooShort) {
40 intervalWasInvoked = false;
41 timeoutTooShort = false;
42 newTimeoutDuration =
43 Math.max(...durationBetweenIntervals, timeoutDuration) * 2;
44 console.error(`Time between intervals: ${durationBetweenIntervals}`);
45 console.error(`Trying w/ timeout of ${newTimeoutDuration}`);
46 return;
47 }
48
49 assert.fail('Request timeout should not fire');
50 });
51
52 req.resume();
53 req.once('end', () => {
54 res.end();
55 });
56 }));
57
58 server.listen(0, common.mustCall(() => {
59 const req = http.request({
60 port: server.address().port,
61 method: 'POST'
62 }, () => {
63 let lastIntervalTimestamp = Date.now();
64 const interval = setInterval(() => {
65 const lastDuration = Date.now() - lastIntervalTimestamp;
66 durationBetweenIntervals.push(lastDuration);
67 lastIntervalTimestamp = Date.now();
68 if (lastDuration > timeoutDuration / 2) {
69 // The interval is supposed to be about 1/8 of the timeout duration.
70 // If it's running so infrequently that it's greater than 1/2 the
71 // timeout duration, then run the test again with a longer timeout.
72 timeoutTooShort = true;

Calls 15

clearIntervalFunction · 0.85
writeHeadMethod · 0.80
flushHeadersMethod · 0.80
listenMethod · 0.80
nowMethod · 0.80
closeMethod · 0.65
resumeMethod · 0.65
requestMethod · 0.65
addressMethod · 0.65
setIntervalFunction · 0.50
setTimeoutFunction · 0.50
setTimeoutMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…