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_cifunction forEach(obj, fn) {
291cb0ef41Sopenharmony_ci  Object.keys(obj).forEach(function(name, index) {
301cb0ef41Sopenharmony_ci    fn(obj[name], name);
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciif (cluster.isWorker) {
351cb0ef41Sopenharmony_ci  // Create a tcp server. This will be used as cluster-shared-server and as an
361cb0ef41Sopenharmony_ci  // alternative IPC channel.
371cb0ef41Sopenharmony_ci  const server = net.Server();
381cb0ef41Sopenharmony_ci  let socket, message;
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  function maybeReply() {
411cb0ef41Sopenharmony_ci    if (!socket || !message) return;
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci    // Tell primary using TCP socket that a message is received.
441cb0ef41Sopenharmony_ci    socket.write(JSON.stringify({
451cb0ef41Sopenharmony_ci      code: 'received message',
461cb0ef41Sopenharmony_ci      echo: message
471cb0ef41Sopenharmony_ci    }));
481cb0ef41Sopenharmony_ci  }
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  server.on('connection', function(socket_) {
511cb0ef41Sopenharmony_ci    socket = socket_;
521cb0ef41Sopenharmony_ci    maybeReply();
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    // Send a message back over the IPC channel.
551cb0ef41Sopenharmony_ci    process.send('message from worker');
561cb0ef41Sopenharmony_ci  });
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  process.on('message', function(message_) {
591cb0ef41Sopenharmony_ci    message = message_;
601cb0ef41Sopenharmony_ci    maybeReply();
611cb0ef41Sopenharmony_ci  });
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  server.listen(0);
641cb0ef41Sopenharmony_ci} else if (cluster.isPrimary) {
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  const checks = {
671cb0ef41Sopenharmony_ci    global: {
681cb0ef41Sopenharmony_ci      'receive': false,
691cb0ef41Sopenharmony_ci      'correct': false
701cb0ef41Sopenharmony_ci    },
711cb0ef41Sopenharmony_ci    primary: {
721cb0ef41Sopenharmony_ci      'receive': false,
731cb0ef41Sopenharmony_ci      'correct': false
741cb0ef41Sopenharmony_ci    },
751cb0ef41Sopenharmony_ci    worker: {
761cb0ef41Sopenharmony_ci      'receive': false,
771cb0ef41Sopenharmony_ci      'correct': false
781cb0ef41Sopenharmony_ci    }
791cb0ef41Sopenharmony_ci  };
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  let client;
831cb0ef41Sopenharmony_ci  const check = (type, result) => {
841cb0ef41Sopenharmony_ci    checks[type].receive = true;
851cb0ef41Sopenharmony_ci    checks[type].correct = result;
861cb0ef41Sopenharmony_ci    console.error('check', checks);
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci    let missing = false;
891cb0ef41Sopenharmony_ci    forEach(checks, function(type) {
901cb0ef41Sopenharmony_ci      if (type.receive === false) missing = true;
911cb0ef41Sopenharmony_ci    });
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci    if (missing === false) {
941cb0ef41Sopenharmony_ci      console.error('end client');
951cb0ef41Sopenharmony_ci      client.end();
961cb0ef41Sopenharmony_ci    }
971cb0ef41Sopenharmony_ci  };
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  // Spawn worker
1001cb0ef41Sopenharmony_ci  const worker = cluster.fork();
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  // When a IPC message is received from the worker
1031cb0ef41Sopenharmony_ci  worker.on('message', function(message) {
1041cb0ef41Sopenharmony_ci    check('primary', message === 'message from worker');
1051cb0ef41Sopenharmony_ci  });
1061cb0ef41Sopenharmony_ci  cluster.on('message', function(worker_, message) {
1071cb0ef41Sopenharmony_ci    assert.strictEqual(worker_, worker);
1081cb0ef41Sopenharmony_ci    check('global', message === 'message from worker');
1091cb0ef41Sopenharmony_ci  });
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci  // When a TCP server is listening in the worker connect to it
1121cb0ef41Sopenharmony_ci  worker.on('listening', function(address) {
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci    client = net.connect(address.port, function() {
1151cb0ef41Sopenharmony_ci      // Send message to worker.
1161cb0ef41Sopenharmony_ci      worker.send('message from primary');
1171cb0ef41Sopenharmony_ci    });
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci    client.on('data', function(data) {
1201cb0ef41Sopenharmony_ci      // All data is JSON
1211cb0ef41Sopenharmony_ci      data = JSON.parse(data.toString());
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci      if (data.code === 'received message') {
1241cb0ef41Sopenharmony_ci        check('worker', data.echo === 'message from primary');
1251cb0ef41Sopenharmony_ci      } else {
1261cb0ef41Sopenharmony_ci        throw new Error(`wrong TCP message received: ${data}`);
1271cb0ef41Sopenharmony_ci      }
1281cb0ef41Sopenharmony_ci    });
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci    // When the connection ends kill worker and shutdown process
1311cb0ef41Sopenharmony_ci    client.on('end', function() {
1321cb0ef41Sopenharmony_ci      worker.kill();
1331cb0ef41Sopenharmony_ci    });
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci    worker.on('exit', common.mustCall(function() {
1361cb0ef41Sopenharmony_ci      process.exit(0);
1371cb0ef41Sopenharmony_ci    }));
1381cb0ef41Sopenharmony_ci  });
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci  process.once('exit', function() {
1411cb0ef41Sopenharmony_ci    forEach(checks, function(check, type) {
1421cb0ef41Sopenharmony_ci      assert.ok(check.receive, `The ${type} did not receive any message`);
1431cb0ef41Sopenharmony_ci      assert.ok(check.correct, `The ${type} did not get the correct message`);
1441cb0ef41Sopenharmony_ci    });
1451cb0ef41Sopenharmony_ci  });
1461cb0ef41Sopenharmony_ci}
147