summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-client-timeout-option-listeners.js
blob: 727b5fddf09624ef00d4998fc164078a2bffc2e7 (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
'use strict';
const common = require('../common');
const http = require('http');
const assert = require('assert');

const agent = new http.Agent({ keepAlive: true });

const server = http.createServer((req, res) => {
  res.end('');
});

// Maximum allowed value for timeouts
const timeout = 2 ** 31 - 1;

const options = {
  agent,
  method: 'GET',
  port: undefined,
  host: common.localhostIPv4,
  path: '/',
  timeout: timeout
};

server.listen(0, options.host, common.mustCall(() => {
  options.port = server.address().port;
  doRequest(common.mustCall((numListeners) => {
    assert.strictEqual(numListeners, 1);
    doRequest(common.mustCall((numListeners) => {
      assert.strictEqual(numListeners, 1);
      server.close();
      agent.destroy();
    }));
  }));
}));

function doRequest(cb) {
  http.request(options, common.mustCall((response) => {
    const sockets = agent.sockets[`${options.host}:${options.port}:`];
    assert.strictEqual(sockets.length, 1);
    const socket = sockets[0];
    const numListeners = socket.listeners('timeout').length;
    response.resume();
    response.once('end', common.mustCall(() => {
      process.nextTick(cb, numListeners);
    }));
  })).end();
}