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';
23const common = require('../common');
24
25// Make sure http server doesn't wait for socket pool to establish connections
26// https://github.com/nodejs/node-v0.x-archive/issues/877
27
28const http = require('http');
29const assert = require('assert');
30
31const N = 20;
32let responses = 0;
33let maxQueued = 0;
34
35const agent = http.globalAgent;
36agent.maxSockets = 10;
37
38const server = http.createServer(function(req, res) {
39  res.writeHead(200);
40  res.end('Hello World\n');
41});
42
43const addrString = agent.getName({ host: '127.0.0.1', port: common.PORT });
44
45server.listen(common.PORT, '127.0.0.1', function() {
46  for (let i = 0; i < N; i++) {
47    const options = {
48      host: '127.0.0.1',
49      port: common.PORT
50    };
51
52    const req = http.get(options, function(res) {
53      if (++responses === N) {
54        server.close();
55      }
56      res.resume();
57    });
58
59    assert.strictEqual(req.agent, agent);
60
61    console.log(
62      `Socket: ${agent.sockets[addrString].length}/${
63        agent.maxSockets} queued: ${
64        agent.requests[addrString] ? agent.requests[addrString].length : 0}`);
65
66    const agentRequests = agent.requests[addrString] ?
67      agent.requests[addrString].length : 0;
68
69    if (maxQueued < agentRequests) {
70      maxQueued = agentRequests;
71    }
72  }
73});
74
75process.on('exit', function() {
76  assert.strictEqual(responses, N);
77  assert.ok(maxQueued <= 10);
78});
79