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_ciconst cluster = require('cluster');
261cb0ef41Sopenharmony_ciconst net = require('net');
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciif (cluster.isWorker) {
291cb0ef41Sopenharmony_ci  net.createServer((socket) => {
301cb0ef41Sopenharmony_ci    socket.end('echo');
311cb0ef41Sopenharmony_ci  }).listen(0, '127.0.0.1');
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  net.createServer((socket) => {
341cb0ef41Sopenharmony_ci    socket.end('echo');
351cb0ef41Sopenharmony_ci  }).listen(0, '127.0.0.1');
361cb0ef41Sopenharmony_ci} else if (cluster.isPrimary) {
371cb0ef41Sopenharmony_ci  const servers = 2;
381cb0ef41Sopenharmony_ci  const serverPorts = new Set();
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  // Test a single TCP server
411cb0ef41Sopenharmony_ci  const testConnection = (port, cb) => {
421cb0ef41Sopenharmony_ci    const socket = net.connect(port, '127.0.0.1', () => {
431cb0ef41Sopenharmony_ci      // buffer result
441cb0ef41Sopenharmony_ci      let result = '';
451cb0ef41Sopenharmony_ci      socket.on('data', (chunk) => { result += chunk; });
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci      // check result
481cb0ef41Sopenharmony_ci      socket.on('end', common.mustCall(() => {
491cb0ef41Sopenharmony_ci        cb(result === 'echo');
501cb0ef41Sopenharmony_ci        serverPorts.delete(port);
511cb0ef41Sopenharmony_ci      }));
521cb0ef41Sopenharmony_ci    });
531cb0ef41Sopenharmony_ci  };
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  // Test both servers created in the cluster
561cb0ef41Sopenharmony_ci  const testCluster = (cb) => {
571cb0ef41Sopenharmony_ci    let done = 0;
581cb0ef41Sopenharmony_ci    const portsArray = Array.from(serverPorts);
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci    for (let i = 0; i < servers; i++) {
611cb0ef41Sopenharmony_ci      testConnection(portsArray[i], (success) => {
621cb0ef41Sopenharmony_ci        assert.ok(success);
631cb0ef41Sopenharmony_ci        done += 1;
641cb0ef41Sopenharmony_ci        if (done === servers) {
651cb0ef41Sopenharmony_ci          cb();
661cb0ef41Sopenharmony_ci        }
671cb0ef41Sopenharmony_ci      });
681cb0ef41Sopenharmony_ci    }
691cb0ef41Sopenharmony_ci  };
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  // Start two workers and execute callback when both is listening
721cb0ef41Sopenharmony_ci  const startCluster = (cb) => {
731cb0ef41Sopenharmony_ci    const workers = 8;
741cb0ef41Sopenharmony_ci    let online = 0;
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci    for (let i = 0, l = workers; i < l; i++) {
771cb0ef41Sopenharmony_ci      cluster.fork().on('listening', common.mustCall((address) => {
781cb0ef41Sopenharmony_ci        serverPorts.add(address.port);
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci        online += 1;
811cb0ef41Sopenharmony_ci        if (online === workers * servers) {
821cb0ef41Sopenharmony_ci          cb();
831cb0ef41Sopenharmony_ci        }
841cb0ef41Sopenharmony_ci      }, servers));
851cb0ef41Sopenharmony_ci    }
861cb0ef41Sopenharmony_ci  };
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  const test = (again) => {
891cb0ef41Sopenharmony_ci    // 1. start cluster
901cb0ef41Sopenharmony_ci    startCluster(common.mustCall(() => {
911cb0ef41Sopenharmony_ci      // 2. test cluster
921cb0ef41Sopenharmony_ci      testCluster(common.mustCall(() => {
931cb0ef41Sopenharmony_ci        // 3. disconnect cluster
941cb0ef41Sopenharmony_ci        cluster.disconnect(common.mustCall(() => {
951cb0ef41Sopenharmony_ci          // Run test again to confirm cleanup
961cb0ef41Sopenharmony_ci          if (again) {
971cb0ef41Sopenharmony_ci            test();
981cb0ef41Sopenharmony_ci          }
991cb0ef41Sopenharmony_ci        }));
1001cb0ef41Sopenharmony_ci      }));
1011cb0ef41Sopenharmony_ci    }));
1021cb0ef41Sopenharmony_ci  };
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci  test(true);
1051cb0ef41Sopenharmony_ci}
106