11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst cp = require('child_process');
51cb0ef41Sopenharmony_ciconst net = require('net');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
81cb0ef41Sopenharmony_ci  process.stdout.write('this should be ignored');
91cb0ef41Sopenharmony_ci  process.stderr.write('this should not be ignored');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci  const pipe = new net.Socket({ fd: 4 });
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  process.on('disconnect', () => {
141cb0ef41Sopenharmony_ci    pipe.unref();
151cb0ef41Sopenharmony_ci  });
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  pipe.setEncoding('utf8');
181cb0ef41Sopenharmony_ci  pipe.on('data', (data) => {
191cb0ef41Sopenharmony_ci    process.send(data);
201cb0ef41Sopenharmony_ci  });
211cb0ef41Sopenharmony_ci} else {
221cb0ef41Sopenharmony_ci  assert.throws(
231cb0ef41Sopenharmony_ci    () => cp.fork(__filename, { stdio: ['pipe', 'pipe', 'pipe', 'pipe'] }),
241cb0ef41Sopenharmony_ci    { code: 'ERR_CHILD_PROCESS_IPC_REQUIRED', name: 'Error' });
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  let ipc = '';
271cb0ef41Sopenharmony_ci  let stderr = '';
281cb0ef41Sopenharmony_ci  const buf = Buffer.from('data to send via pipe');
291cb0ef41Sopenharmony_ci  const child = cp.fork(__filename, ['child'], {
301cb0ef41Sopenharmony_ci    stdio: [0, 'ignore', 'pipe', 'ipc', 'pipe']
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  assert.strictEqual(child.stdout, null);
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  child.on('message', (msg) => {
361cb0ef41Sopenharmony_ci    ipc += msg;
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci    if (ipc === buf.toString()) {
391cb0ef41Sopenharmony_ci      child.disconnect();
401cb0ef41Sopenharmony_ci    }
411cb0ef41Sopenharmony_ci  });
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  child.stderr.on('data', (chunk) => {
441cb0ef41Sopenharmony_ci    stderr += chunk;
451cb0ef41Sopenharmony_ci  });
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  child.on('exit', common.mustCall((code, signal) => {
481cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
491cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
501cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, 'this should not be ignored');
511cb0ef41Sopenharmony_ci  }));
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  child.stdio[4].write(buf);
541cb0ef41Sopenharmony_ci}
55