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_cirequire('../common');
241cb0ef41Sopenharmony_ciconst assert = require('assert');
251cb0ef41Sopenharmony_ciconst childProcess = require('child_process');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci// Child pipe test
281cb0ef41Sopenharmony_ciif (process.argv[2] === 'pipe') {
291cb0ef41Sopenharmony_ci  process.stdout.write('stdout message');
301cb0ef41Sopenharmony_ci  process.stderr.write('stderr message');
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci} else if (process.argv[2] === 'ipc') {
331cb0ef41Sopenharmony_ci  // Child IPC test
341cb0ef41Sopenharmony_ci  process.send('message from child');
351cb0ef41Sopenharmony_ci  process.on('message', function() {
361cb0ef41Sopenharmony_ci    process.send('got message from primary');
371cb0ef41Sopenharmony_ci  });
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci} else if (process.argv[2] === 'primary') {
401cb0ef41Sopenharmony_ci  // Primary | start child pipe test
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  const child = childProcess.fork(process.argv[1], ['pipe'], { silent: true });
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  // Allow child process to self terminate
451cb0ef41Sopenharmony_ci  child.disconnect();
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  child.on('exit', function() {
481cb0ef41Sopenharmony_ci    process.exit(0);
491cb0ef41Sopenharmony_ci  });
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci} else {
521cb0ef41Sopenharmony_ci  // Testcase | start primary && child IPC test
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  // testing: is stderr and stdout piped to primary
551cb0ef41Sopenharmony_ci  const args = [process.argv[1], 'primary'];
561cb0ef41Sopenharmony_ci  const primary = childProcess.spawn(process.execPath, args);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  // Got any stderr or std data
591cb0ef41Sopenharmony_ci  let stdoutData = false;
601cb0ef41Sopenharmony_ci  primary.stdout.on('data', function() {
611cb0ef41Sopenharmony_ci    stdoutData = true;
621cb0ef41Sopenharmony_ci  });
631cb0ef41Sopenharmony_ci  let stderrData = false;
641cb0ef41Sopenharmony_ci  primary.stderr.on('data', function() {
651cb0ef41Sopenharmony_ci    stderrData = true;
661cb0ef41Sopenharmony_ci  });
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  // testing: do message system work when using silent
691cb0ef41Sopenharmony_ci  const child = childProcess.fork(process.argv[1], ['ipc'], { silent: true });
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  // Manual pipe so we will get errors
721cb0ef41Sopenharmony_ci  child.stderr.pipe(process.stderr, { end: false });
731cb0ef41Sopenharmony_ci  child.stdout.pipe(process.stdout, { end: false });
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  let childSending = false;
761cb0ef41Sopenharmony_ci  let childReceiving = false;
771cb0ef41Sopenharmony_ci  child.on('message', function(message) {
781cb0ef41Sopenharmony_ci    if (childSending === false) {
791cb0ef41Sopenharmony_ci      childSending = (message === 'message from child');
801cb0ef41Sopenharmony_ci    }
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci    if (childReceiving === false) {
831cb0ef41Sopenharmony_ci      childReceiving = (message === 'got message from primary');
841cb0ef41Sopenharmony_ci    }
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci    if (childReceiving === true) {
871cb0ef41Sopenharmony_ci      child.kill();
881cb0ef41Sopenharmony_ci    }
891cb0ef41Sopenharmony_ci  });
901cb0ef41Sopenharmony_ci  child.send('message to child');
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  // Check all values
931cb0ef41Sopenharmony_ci  process.on('exit', function() {
941cb0ef41Sopenharmony_ci    // clean up
951cb0ef41Sopenharmony_ci    child.kill();
961cb0ef41Sopenharmony_ci    primary.kill();
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci    // Check std(out|err) pipes
991cb0ef41Sopenharmony_ci    assert.ok(!stdoutData);
1001cb0ef41Sopenharmony_ci    assert.ok(!stderrData);
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci    // Check message system
1031cb0ef41Sopenharmony_ci    assert.ok(childSending);
1041cb0ef41Sopenharmony_ci    assert.ok(childReceiving);
1051cb0ef41Sopenharmony_ci  });
1061cb0ef41Sopenharmony_ci}
107