• 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';
23const common = require('../common');
24const assert = require('assert');
25const http = require('http');
26const Countdown = require('../common/countdown');
27
28const server = http.createServer(common.mustCall((req, res) => {
29  res.writeHead(200, { 'Content-Type': 'text/plain' });
30  res.end('Hello World\n');
31}, 2)).listen(0, common.mustCall(() => {
32  const agent = new http.Agent({ maxSockets: 1 });
33
34  agent.on('free', common.mustCall(3));
35
36  const requestOptions = {
37    agent: agent,
38    host: 'localhost',
39    port: server.address().port,
40    path: '/'
41  };
42
43  const request1 = http.get(requestOptions, common.mustCall((response) => {
44    // Assert request2 is queued in the agent
45    const key = agent.getName(requestOptions);
46    assert.strictEqual(agent.requests[key].length, 1);
47    response.resume();
48    response.on('end', common.mustCall(() => {
49      request1.socket.destroy();
50
51      request1.socket.once('close', common.mustCall(() => {
52        // Assert request2 was removed from the queue
53        assert(!agent.requests[key]);
54        process.nextTick(() => {
55          // Assert that the same socket was not assigned to request2,
56          // since it was destroyed.
57          assert.notStrictEqual(request1.socket, request2.socket);
58          assert(!request2.socket.destroyed, 'the socket is destroyed');
59        });
60      }));
61    }));
62  }));
63
64  const request2 = http.get(requestOptions, common.mustCall((response) => {
65    assert(!request2.socket.destroyed);
66    assert(request1.socket.destroyed);
67    // Assert not reusing the same socket, since it was destroyed.
68    assert.notStrictEqual(request1.socket, request2.socket);
69    const countdown = new Countdown(2, () => server.close());
70    request2.socket.on('close', common.mustCall(() => countdown.dec()));
71    response.on('end', common.mustCall(() => countdown.dec()));
72    response.resume();
73  }));
74}));
75