11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_cirequire('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst net = require('net'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci// Sets the server's maxConnections property to 1. 81cb0ef41Sopenharmony_ci// Open 2 connections (connection 0 and connection 1). 91cb0ef41Sopenharmony_ci// Connection 0 should be accepted. 101cb0ef41Sopenharmony_ci// Connection 1 should be rejected. 111cb0ef41Sopenharmony_ci// Closes connection 0. 121cb0ef41Sopenharmony_ci// Open 2 more connections (connection 2 and 3). 131cb0ef41Sopenharmony_ci// Connection 2 should be accepted. 141cb0ef41Sopenharmony_ci// Connection 3 should be rejected. 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciconst connections = []; 171cb0ef41Sopenharmony_ciconst received = []; 181cb0ef41Sopenharmony_ciconst sent = []; 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_cifunction createConnection(index) { 211cb0ef41Sopenharmony_ci console.error(`creating connection ${index}`); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci return new Promise(function(resolve, reject) { 241cb0ef41Sopenharmony_ci const connection = net.createConnection(server.address().port, function() { 251cb0ef41Sopenharmony_ci const msg = String(index); 261cb0ef41Sopenharmony_ci console.error(`sending message: ${msg}`); 271cb0ef41Sopenharmony_ci this.write(msg); 281cb0ef41Sopenharmony_ci sent.push(msg); 291cb0ef41Sopenharmony_ci }); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci connection.on('error', function(err) { 321cb0ef41Sopenharmony_ci assert.strictEqual(err.code, 'ECONNRESET'); 331cb0ef41Sopenharmony_ci resolve(); 341cb0ef41Sopenharmony_ci }); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci connection.on('data', function(e) { 371cb0ef41Sopenharmony_ci console.error(`connection ${index} received response`); 381cb0ef41Sopenharmony_ci resolve(); 391cb0ef41Sopenharmony_ci }); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci connection.on('end', function() { 421cb0ef41Sopenharmony_ci console.error(`ending ${index}`); 431cb0ef41Sopenharmony_ci resolve(); 441cb0ef41Sopenharmony_ci }); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci connections[index] = connection; 471cb0ef41Sopenharmony_ci }); 481cb0ef41Sopenharmony_ci} 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_cifunction closeConnection(index) { 511cb0ef41Sopenharmony_ci console.error(`closing connection ${index}`); 521cb0ef41Sopenharmony_ci return new Promise(function(resolve, reject) { 531cb0ef41Sopenharmony_ci connections[index].on('end', function() { 541cb0ef41Sopenharmony_ci resolve(); 551cb0ef41Sopenharmony_ci }); 561cb0ef41Sopenharmony_ci connections[index].end(); 571cb0ef41Sopenharmony_ci }); 581cb0ef41Sopenharmony_ci} 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ciconst server = net.createServer(function(socket) { 611cb0ef41Sopenharmony_ci socket.on('data', function(data) { 621cb0ef41Sopenharmony_ci console.error(`received message: ${data}`); 631cb0ef41Sopenharmony_ci received.push(String(data)); 641cb0ef41Sopenharmony_ci socket.write('acknowledged'); 651cb0ef41Sopenharmony_ci }); 661cb0ef41Sopenharmony_ci}); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ciserver.maxConnections = 1; 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ciserver.listen(0, function() { 711cb0ef41Sopenharmony_ci createConnection(0) 721cb0ef41Sopenharmony_ci .then(createConnection.bind(null, 1)) 731cb0ef41Sopenharmony_ci .then(closeConnection.bind(null, 0)) 741cb0ef41Sopenharmony_ci .then(createConnection.bind(null, 2)) 751cb0ef41Sopenharmony_ci .then(createConnection.bind(null, 3)) 761cb0ef41Sopenharmony_ci .then(server.close.bind(server)) 771cb0ef41Sopenharmony_ci .then(closeConnection.bind(null, 2)); 781cb0ef41Sopenharmony_ci}); 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ciprocess.on('exit', function() { 811cb0ef41Sopenharmony_ci // Confirm that all connections tried to send data... 821cb0ef41Sopenharmony_ci assert.deepStrictEqual(sent, ['0', '1', '2', '3']); 831cb0ef41Sopenharmony_ci // ...but that only connections 0 and 2 were successful. 841cb0ef41Sopenharmony_ci assert.deepStrictEqual(received, ['0', '2']); 851cb0ef41Sopenharmony_ci}); 86