• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23require('../common');
24const assert = require('assert');
25const childProcess = require('child_process');
26
27// Child pipe test
28if (process.argv[2] === 'pipe') {
29  process.stdout.write('stdout message');
30  process.stderr.write('stderr message');
31
32} else if (process.argv[2] === 'ipc') {
33  // Child IPC test
34  process.send('message from child');
35  process.on('message', function() {
36    process.send('got message from master');
37  });
38
39} else if (process.argv[2] === 'parent') {
40  // Parent | start child pipe test
41
42  const child = childProcess.fork(process.argv[1], ['pipe'], { silent: true });
43
44  // Allow child process to self terminate
45  child.disconnect();
46
47  child.on('exit', function() {
48    process.exit(0);
49  });
50
51} else {
52  // Testcase | start parent && child IPC test
53
54  // testing: is stderr and stdout piped to parent
55  const args = [process.argv[1], 'parent'];
56  const parent = childProcess.spawn(process.execPath, args);
57
58  // Got any stderr or std data
59  let stdoutData = false;
60  parent.stdout.on('data', function() {
61    stdoutData = true;
62  });
63  let stderrData = false;
64  parent.stderr.on('data', function() {
65    stderrData = true;
66  });
67
68  // testing: do message system work when using silent
69  const child = childProcess.fork(process.argv[1], ['ipc'], { silent: true });
70
71  // Manual pipe so we will get errors
72  child.stderr.pipe(process.stderr, { end: false });
73  child.stdout.pipe(process.stdout, { end: false });
74
75  let childSending = false;
76  let childReceiving = false;
77  child.on('message', function(message) {
78    if (childSending === false) {
79      childSending = (message === 'message from child');
80    }
81
82    if (childReceiving === false) {
83      childReceiving = (message === 'got message from master');
84    }
85
86    if (childReceiving === true) {
87      child.kill();
88    }
89  });
90  child.send('message to child');
91
92  // Check all values
93  process.on('exit', function() {
94    // clean up
95    child.kill();
96    parent.kill();
97
98    // Check std(out|err) pipes
99    assert.ok(!stdoutData);
100    assert.ok(!stderrData);
101
102    // Check message system
103    assert.ok(childSending);
104    assert.ok(childReceiving);
105  });
106}
107