• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3
4const assert = require('assert');
5const cluster = require('cluster');
6const net = require('net');
7
8const payload = 'a'.repeat(800004);
9
10if (cluster.isMaster) {
11  const server = net.createServer();
12
13  server.on('connection', common.mustCall((socket) => { socket.unref(); }));
14
15  const worker = cluster.fork();
16  worker.on('message', common.mustCall(({ payload: received }, handle) => {
17    assert.strictEqual(payload, received);
18    assert(handle instanceof net.Socket);
19    server.close();
20    handle.destroy();
21  }));
22
23  server.listen(0, common.mustCall(() => {
24    const port = server.address().port;
25    const socket = new net.Socket();
26    socket.connect(port, (err) => {
27      assert.ifError(err);
28      worker.send({ payload }, socket);
29    });
30  }));
31} else {
32  process.on('message', common.mustCall(({ payload: received }, handle) => {
33    assert.strictEqual(payload, received);
34    assert(handle instanceof net.Socket);
35
36    // On macOS, the parent process might not receive a message if it is sent
37    // to soon, and then subsequent messages are also sometimes not received.
38    //
39    // (Is this a bug or expected operating system behavior like the way a file
40    // watcher is returned before it's actually watching the file system on
41    // macOS?)
42    //
43    // Send a second message after a delay on macOS.
44    //
45    // Refs: https://github.com/nodejs/node/issues/14747
46    if (common.isOSX)
47      setTimeout(() => { process.send({ payload }, handle); }, 1000);
48    else
49      process.send({ payload }, handle);
50
51    // Prepare for a clean exit.
52    process.channel.unref();
53  }));
54}
55