11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ciconst common = require('../common');
241cb0ef41Sopenharmony_ciconst assert = require('assert');
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciconst net = require('net');
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci// This test creates 20 connections to a server and sets the server's
291cb0ef41Sopenharmony_ci// maxConnections property to 10. The first 10 connections make it through
301cb0ef41Sopenharmony_ci// and the last 10 connections are rejected.
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciconst N = 20;
331cb0ef41Sopenharmony_cilet closes = 0;
341cb0ef41Sopenharmony_ciconst waits = [];
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciconst server = net.createServer(common.mustCall(function(connection) {
371cb0ef41Sopenharmony_ci  connection.write('hello');
381cb0ef41Sopenharmony_ci  waits.push(function() { connection.end(); });
391cb0ef41Sopenharmony_ci}, N / 2));
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciserver.listen(0, function() {
421cb0ef41Sopenharmony_ci  makeConnection(0);
431cb0ef41Sopenharmony_ci});
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciserver.maxConnections = N / 2;
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_cifunction makeConnection(index) {
491cb0ef41Sopenharmony_ci  const c = net.createConnection(server.address().port);
501cb0ef41Sopenharmony_ci  let gotData = false;
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  c.on('connect', function() {
531cb0ef41Sopenharmony_ci    if (index + 1 < N) {
541cb0ef41Sopenharmony_ci      makeConnection(index + 1);
551cb0ef41Sopenharmony_ci    }
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci    c.on('close', function() {
581cb0ef41Sopenharmony_ci      console.error(`closed ${index}`);
591cb0ef41Sopenharmony_ci      closes++;
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci      if (closes < N / 2) {
621cb0ef41Sopenharmony_ci        assert.ok(
631cb0ef41Sopenharmony_ci          server.maxConnections <= index,
641cb0ef41Sopenharmony_ci          `${index} should not have been one of the first closed connections`
651cb0ef41Sopenharmony_ci        );
661cb0ef41Sopenharmony_ci      }
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci      if (closes === N / 2) {
691cb0ef41Sopenharmony_ci        let cb;
701cb0ef41Sopenharmony_ci        console.error('calling wait callback.');
711cb0ef41Sopenharmony_ci        while ((cb = waits.shift()) !== undefined) {
721cb0ef41Sopenharmony_ci          cb();
731cb0ef41Sopenharmony_ci        }
741cb0ef41Sopenharmony_ci        server.close();
751cb0ef41Sopenharmony_ci      }
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci      if (index < server.maxConnections) {
781cb0ef41Sopenharmony_ci        assert.strictEqual(gotData, true,
791cb0ef41Sopenharmony_ci                           `${index} didn't get data, but should have`);
801cb0ef41Sopenharmony_ci      } else {
811cb0ef41Sopenharmony_ci        assert.strictEqual(gotData, false,
821cb0ef41Sopenharmony_ci                           `${index} got data, but shouldn't have`);
831cb0ef41Sopenharmony_ci      }
841cb0ef41Sopenharmony_ci    });
851cb0ef41Sopenharmony_ci  });
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  c.on('end', function() { c.end(); });
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  c.on('data', function(b) {
901cb0ef41Sopenharmony_ci    gotData = true;
911cb0ef41Sopenharmony_ci    assert.ok(b.length > 0);
921cb0ef41Sopenharmony_ci  });
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  c.on('error', function(e) {
951cb0ef41Sopenharmony_ci    // Retry if SmartOS and ECONNREFUSED. See
961cb0ef41Sopenharmony_ci    // https://github.com/nodejs/node/issues/2663.
971cb0ef41Sopenharmony_ci    if (common.isSunOS && (e.code === 'ECONNREFUSED')) {
981cb0ef41Sopenharmony_ci      c.connect(server.address().port);
991cb0ef41Sopenharmony_ci    }
1001cb0ef41Sopenharmony_ci    console.error(`error ${index}: ${e}`);
1011cb0ef41Sopenharmony_ci  });
1021cb0ef41Sopenharmony_ci}
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ciprocess.on('exit', function() {
1061cb0ef41Sopenharmony_ci  assert.strictEqual(closes, N);
1071cb0ef41Sopenharmony_ci});
108