• 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';
23// Verify that connect reqs are properly cleaned up.
24
25const common = require('../common');
26const assert = require('assert');
27const net = require('net');
28
29const ROUNDS = 5;
30const ATTEMPTS_PER_ROUND = 50;
31let rounds = 1;
32let reqs = 0;
33
34let port;
35const server = net.createServer().listen(0, common.mustCall(() => {
36  port = server.address().port;
37  server.close(common.mustCall(pummel));
38}));
39
40function pummel() {
41  let pending;
42  for (pending = 0; pending < ATTEMPTS_PER_ROUND; pending++) {
43    net.createConnection(port).on('error', function(err) {
44      console.log('pending', pending, 'rounds', rounds);
45      assert.strictEqual(err.code, 'ECONNREFUSED');
46      if (--pending > 0) return;
47      if (rounds === ROUNDS) return check();
48      rounds++;
49      pummel();
50    });
51    reqs++;
52  }
53}
54
55function check() {
56  setTimeout(common.mustCall(function() {
57    assert.strictEqual(process._getActiveRequests().length, 0);
58    const activeHandles = process._getActiveHandles();
59    assert.ok(activeHandles.every((val) => val.constructor.name !== 'Socket'));
60  }), 0);
61}
62
63process.on('exit', function() {
64  assert.strictEqual(rounds, ROUNDS);
65  assert.strictEqual(reqs, ROUNDS * ATTEMPTS_PER_ROUND);
66});
67