summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-client-timeout-connect-listener.js
blob: ea09aff718d5575ee986b6333df7d40b69c6ff7c (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
'use strict';
const common = require('../common');

// This test ensures that `ClientRequest.prototype.setTimeout()` does
// not add a listener for the `'connect'` event to the socket if the
// socket is already connected.

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

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

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

server.listen(0, common.mustCall(() => {
  const agent = new http.Agent({ keepAlive: true, maxSockets: 1 });
  const options = { port: server.address().port, agent: agent };

  doRequest(options, common.mustCall(() => {
    const req = doRequest(options, common.mustCall(() => {
      agent.destroy();
      server.close();
    }));

    req.on('socket', common.mustCall((socket) => {
      assert.strictEqual(socket.listenerCount('connect'), 0);
    }));
  }));
}));

function doRequest(options, callback) {
  const req = http.get(options, (res) => {
    res.on('end', callback);
    res.resume();
  });

  req.setTimeout(timeout);
  return req;
}