• 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');
25
26const net = require('net');
27
28// This test creates 20 connections to a server and sets the server's
29// maxConnections property to 10. The first 10 connections make it through
30// and the last 10 connections are rejected.
31
32const N = 20;
33let closes = 0;
34const waits = [];
35
36const server = net.createServer(common.mustCall(function(connection) {
37  connection.write('hello');
38  waits.push(function() { connection.end(); });
39}, N / 2));
40
41server.listen(0, function() {
42  makeConnection(0);
43});
44
45server.maxConnections = N / 2;
46
47
48function makeConnection(index) {
49  const c = net.createConnection(server.address().port);
50  let gotData = false;
51
52  c.on('connect', function() {
53    if (index + 1 < N) {
54      makeConnection(index + 1);
55    }
56
57    c.on('close', function() {
58      console.error(`closed ${index}`);
59      closes++;
60
61      if (closes < N / 2) {
62        assert.ok(
63          server.maxConnections <= index,
64          `${index} should not have been one of the first closed connections`
65        );
66      }
67
68      if (closes === N / 2) {
69        let cb;
70        console.error('calling wait callback.');
71        while (cb = waits.shift()) {
72          cb();
73        }
74        server.close();
75      }
76
77      if (index < server.maxConnections) {
78        assert.strictEqual(gotData, true,
79                           `${index} didn't get data, but should have`);
80      } else {
81        assert.strictEqual(gotData, false,
82                           `${index} got data, but shouldn't have`);
83      }
84    });
85  });
86
87  c.on('end', function() { c.end(); });
88
89  c.on('data', function(b) {
90    gotData = true;
91    assert.ok(b.length > 0);
92  });
93
94  c.on('error', function(e) {
95    // Retry if SmartOS and ECONNREFUSED. See
96    // https://github.com/nodejs/node/issues/2663.
97    if (common.isSunOS && (e.code === 'ECONNREFUSED')) {
98      c.connect(server.address().port);
99    }
100    console.error(`error ${index}: ${e}`);
101  });
102}
103
104
105process.on('exit', function() {
106  assert.strictEqual(closes, N);
107});
108