summaryrefslogtreecommitdiff
path: root/test/parallel/test-child-process-fork-stdio.js
blob: d8e7abfe28944d88c6398169f6b61720648f1ec4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');
const net = require('net');

if (process.argv[2] === 'child') {
  process.stdout.write('this should be ignored');
  process.stderr.write('this should not be ignored');

  const pipe = new net.Socket({ fd: 4 });

  process.on('disconnect', () => {
    pipe.unref();
  });

  pipe.setEncoding('utf8');
  pipe.on('data', (data) => {
    process.send(data);
  });
} else {
  common.expectsError(
    () => cp.fork(__filename, { stdio: ['pipe', 'pipe', 'pipe', 'pipe'] }),
    { code: 'ERR_CHILD_PROCESS_IPC_REQUIRED', type: Error });

  let ipc = '';
  let stderr = '';
  const buf = Buffer.from('data to send via pipe');
  const child = cp.fork(__filename, ['child'], {
    stdio: [0, 'ignore', 'pipe', 'ipc', 'pipe']
  });

  assert.strictEqual(child.stdout, null);

  child.on('message', (msg) => {
    ipc += msg;

    if (ipc === buf.toString()) {
      child.disconnect();
    }
  });

  child.stderr.on('data', (chunk) => {
    stderr += chunk;
  });

  child.on('exit', common.mustCall((code, signal) => {
    assert.strictEqual(code, 0);
    assert.strictEqual(signal, null);
    assert.strictEqual(stderr, 'this should not be ignored');
  }));

  child.stdio[4].write(buf);
}