• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const Countdown = require('../common/countdown');
4
5// This test ensures that the `maxSockets` value for `http.Agent` is respected.
6// https://github.com/nodejs/node/issues/4050
7
8const assert = require('assert');
9const http = require('http');
10
11const MAX_SOCKETS = 2;
12
13const agent = new http.Agent({
14  keepAlive: true,
15  keepAliveMsecs: 1000,
16  maxSockets: MAX_SOCKETS,
17  maxFreeSockets: 2
18});
19
20const server = http.createServer(
21  common.mustCall((req, res) => {
22    res.end('hello world');
23  }, 6)
24);
25
26const countdown = new Countdown(6, () => server.close());
27
28function get(path, callback) {
29  return http.get(
30    {
31      host: 'localhost',
32      port: server.address().port,
33      agent: agent,
34      path: path
35    },
36    callback
37  );
38}
39
40server.listen(
41  0,
42  common.mustCall(() => {
43    for (let i = 0; i < 6; i++) {
44      const request = get('/1', common.mustCall());
45      request.on(
46        'response',
47        common.mustCall(() => {
48          request.abort();
49          const sockets = agent.sockets[Object.keys(agent.sockets)[0]];
50          assert(sockets.length <= MAX_SOCKETS);
51          countdown.dec();
52        })
53      );
54    }
55  })
56);
57