summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-agent-maxtotalsockets.js
blob: c44c8db627d330c30ccac047f0dd7b0165ffc08e (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');
const Countdown = require('../common/countdown');

assert.throws(() => new http.Agent({
  maxTotalSockets: 'test',
}), {
  code: 'ERR_INVALID_ARG_TYPE',
  name: 'TypeError',
  message: 'The "maxTotalSockets" argument must be of type number. ' +
    "Received type string ('test')",
});

[-1, 0, NaN].forEach((item) => {
  assert.throws(() => new http.Agent({
    maxTotalSockets: item,
  }), {
    code: 'ERR_OUT_OF_RANGE',
    name: 'RangeError',
    message: 'The value of "maxTotalSockets" is out of range. ' +
      `It must be > 0. Received ${item}`,
  });
});

assert.ok(new http.Agent({
  maxTotalSockets: Infinity,
}));

function start(param = {}) {
  const { maxTotalSockets, maxSockets } = param;

  const agent = new http.Agent({
    keepAlive: true,
    keepAliveMsecs: 1000,
    maxTotalSockets,
    maxSockets,
    maxFreeSockets: 3
  });

  const server = http.createServer(common.mustCall((req, res) => {
    res.end('hello world');
  }, 6));
  const server2 = http.createServer(common.mustCall((req, res) => {
    res.end('hello world');
  }, 6));

  server.keepAliveTimeout = 0;
  server2.keepAliveTimeout = 0;

  const countdown = new Countdown(12, () => {
    assert.strictEqual(getRequestCount(), 0);
    agent.destroy();
    server.close();
    server2.close();
  });

  function handler(s) {
    for (let i = 0; i < 6; i++) {
      http.get({
        host: 'localhost',
        port: s.address().port,
        agent,
        path: `/${i}`,
      }, common.mustCall((res) => {
        assert.strictEqual(res.statusCode, 200);
        res.resume();
        res.on('end', common.mustCall(() => {
          for (const key of Object.keys(agent.sockets)) {
            assert(agent.sockets[key].length <= maxSockets);
          }
          assert(getTotalSocketsCount() <= maxTotalSockets);
          countdown.dec();
        }));
      }));
    }
  }

  function getTotalSocketsCount() {
    let num = 0;
    for (const key of Object.keys(agent.sockets)) {
      num += agent.sockets[key].length;
    }
    return num;
  }

  function getRequestCount() {
    let num = 0;
    for (const key of Object.keys(agent.requests)) {
      num += agent.requests[key].length;
    }
    return num;
  }

  server.listen(0, common.mustCall(() => handler(server)));
  server2.listen(0, common.mustCall(() => handler(server2)));
}

// If maxTotalSockets is larger than maxSockets,
// then the origin check will be skipped
// when the socket is removed.
[{
  maxTotalSockets: 2,
  maxSockets: 3,
}, {
  maxTotalSockets: 3,
  maxSockets: 2,
}, {
  maxTotalSockets: 2,
  maxSockets: 2,
}].forEach(start);