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
25const http = require('http');
26const Countdown = require('../common/countdown');
27const MAX_COUNT = 11;
28
29const server = http.createServer(common.mustCall(function(req, res) {
30  res.writeHead(200, { 'Content-Type': 'text/plain' });
31  res.end('OK');
32}, MAX_COUNT));
33
34const agent = new http.Agent({ maxSockets: 1 });
35const countdown = new Countdown(MAX_COUNT, () => server.close());
36
37server.listen(0, function() {
38
39  for (let i = 0; i < MAX_COUNT; ++i) {
40    createRequest().end();
41  }
42
43  function callback() {}
44
45  function createRequest() {
46    const req = http.request(
47      { port: server.address().port, path: '/', agent: agent },
48      function(res) {
49        req.clearTimeout(callback);
50
51        res.on('end', function() {
52          countdown.dec();
53        });
54
55        res.resume();
56      }
57    );
58
59    req.setTimeout(1000, callback);
60    return req;
61  }
62});
63