summaryrefslogtreecommitdiff
path: root/test/sequential/test-http-server-consumed-timeout.js
blob: 56217226503be1f64d1da13fa6f8b11ab33bc696 (plain)
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
'use strict';

const common = require('../common');

const assert = require('assert');
const http = require('http');

let time = Date.now();
let intervalWasInvoked = false;
const TIMEOUT = common.platformTimeout(200);

const server = http.createServer((req, res) => {
  server.close();

  res.writeHead(200);
  res.flushHeaders();

  req.setTimeout(TIMEOUT, () => {
    if (!intervalWasInvoked)
      return common.skip('interval was not invoked quickly enough for test');
    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'
  }, (res) => {
    const interval = setInterval(() => {
      intervalWasInvoked = true;
      // If machine is busy enough that the interval takes more than TIMEOUT ms
      // to be invoked, skip the test.
      const now = Date.now();
      if (now - time > TIMEOUT)
        return common.skip('interval is not invoked quickly enough for test');
      time = now;
      req.write('a');
    }, common.platformTimeout(25));
    setTimeout(() => {
      clearInterval(interval);
      req.end();
    }, TIMEOUT);
  });
  req.write('.');
}));