summaryrefslogtreecommitdiff
path: root/test/parallel/test-cluster-fork-stdio.js
blob: 1d00e0768e5746272818f81fbce56a80e4c3845b (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
'use strict';
const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
const net = require('net');

if (cluster.isMaster) {
  const buf = Buffer.from('foobar');

  cluster.setupMaster({
    stdio: ['pipe', 'pipe', 'pipe', 'ipc', 'pipe']
  });

  const worker = cluster.fork();
  const channel = worker.process.stdio[4];
  let response = '';

  worker.on('exit', common.mustCall((code, signal) => {
    assert.strictEqual(code, 0);
    assert.strictEqual(signal, null);
  }));

  channel.setEncoding('utf8');
  channel.on('data', (data) => {
    response += data;

    if (response === buf.toString()) {
      worker.disconnect();
    }
  });
  channel.write(buf);
} else {
  const pipe = new net.Socket({ fd: 4 });

  pipe.unref();
  pipe.on('data', (data) => {
    assert.ok(data instanceof Buffer);
    pipe.write(data);
  });
}