1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const cp = require('child_process');
5const net = require('net');
6
7if (process.argv[2] === 'child') {
8  process.stdout.write('this should be ignored');
9  process.stderr.write('this should not be ignored');
10
11  const pipe = new net.Socket({ fd: 4 });
12
13  process.on('disconnect', () => {
14    pipe.unref();
15  });
16
17  pipe.setEncoding('utf8');
18  pipe.on('data', (data) => {
19    process.send(data);
20  });
21} else {
22  assert.throws(
23    () => cp.fork(__filename, { stdio: ['pipe', 'pipe', 'pipe', 'pipe'] }),
24    { code: 'ERR_CHILD_PROCESS_IPC_REQUIRED', name: 'Error' });
25
26  let ipc = '';
27  let stderr = '';
28  const buf = Buffer.from('data to send via pipe');
29  const child = cp.fork(__filename, ['child'], {
30    stdio: [0, 'ignore', 'pipe', 'ipc', 'pipe']
31  });
32
33  assert.strictEqual(child.stdout, null);
34
35  child.on('message', (msg) => {
36    ipc += msg;
37
38    if (ipc === buf.toString()) {
39      child.disconnect();
40    }
41  });
42
43  child.stderr.on('data', (chunk) => {
44    stderr += chunk;
45  });
46
47  child.on('exit', common.mustCall((code, signal) => {
48    assert.strictEqual(code, 0);
49    assert.strictEqual(signal, null);
50    assert.strictEqual(stderr, 'this should not be ignored');
51  }));
52
53  child.stdio[4].write(buf);
54}
55