• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23require('../common');
24const assert = require('assert');
25
26const http = require('http');
27
28
29const serverSockets = [];
30const server = http.createServer(function(req, res) {
31  if (!serverSockets.includes(req.socket)) {
32    serverSockets.push(req.socket);
33  }
34  res.end(req.url);
35});
36server.listen(0, function() {
37  const agent = http.Agent({
38    keepAlive: true,
39    maxSockets: 5,
40    maxFreeSockets: 2
41  });
42
43  let closed = false;
44  makeReqs(10, function(er) {
45    assert.ifError(er);
46    assert.strictEqual(count(agent.freeSockets), 2);
47    assert.strictEqual(count(agent.sockets), 0);
48    assert.strictEqual(serverSockets.length, 5);
49
50    // Now make 10 more reqs.
51    // should use the 2 free reqs from the pool first.
52    makeReqs(10, function(er) {
53      assert.ifError(er);
54      assert.strictEqual(count(agent.freeSockets), 2);
55      assert.strictEqual(count(agent.sockets), 0);
56      assert.strictEqual(serverSockets.length, 8);
57
58      agent.destroy();
59      server.close(function() {
60        closed = true;
61      });
62    });
63  });
64
65  process.on('exit', function() {
66    assert(closed);
67    console.log('ok');
68  });
69
70  // make 10 requests in parallel,
71  // then 10 more when they all finish.
72  function makeReqs(n, cb) {
73    for (let i = 0; i < n; i++)
74      makeReq(i, then);
75
76    function then(er) {
77      if (er)
78        return cb(er);
79      else if (--n === 0)
80        setTimeout(cb, 100);
81    }
82  }
83
84  function makeReq(i, cb) {
85    http.request({
86      port: server.address().port,
87      path: `/${i}`,
88      agent: agent
89    }, function(res) {
90      let data = '';
91      res.setEncoding('ascii');
92      res.on('data', function(c) {
93        data += c;
94      });
95      res.on('end', function() {
96        assert.strictEqual(data, `/${i}`);
97        cb();
98      });
99    }).end();
100  }
101
102  function count(sockets) {
103    return Object.keys(sockets).reduce(function(n, name) {
104      return n + sockets[name].length;
105    }, 0);
106  }
107});
108