summaryrefslogtreecommitdiff
path: root/test/sequential/test-cluster-send-handle-large-payload.js
blob: f0ffa63421bff69d7d452e418c13f9c628e67a06 (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 cluster = require('cluster');
const net = require('net');

const payload = 'a'.repeat(800004);

if (cluster.isMaster) {
  const server = net.createServer();

  server.on('connection', common.mustCall((socket) => { socket.unref(); }));

  const worker = cluster.fork();
  worker.on('message', common.mustCall(({ payload: received }, handle) => {
    assert.strictEqual(payload, received);
    assert(handle instanceof net.Socket);
    server.close();
    handle.destroy();
  }));

  server.listen(0, common.mustCall(() => {
    const port = server.address().port;
    const socket = new net.Socket();
    socket.connect(port, (err) => {
      assert.ifError(err);
      worker.send({ payload }, socket);
    });
  }));
} else {
  process.on('message', common.mustCall(({ payload: received }, handle) => {
    assert.strictEqual(payload, received);
    assert(handle instanceof net.Socket);

    // On macOS, the parent process might not receive a message if it is sent
    // to soon, and then subsequent messages are also sometimes not received.
    //
    // (Is this a bug or expected operating system behavior like the way a file
    // watcher is returned before it's actually watching the file system on
    // macOS?)
    //
    // Send a second message after a delay on macOS.
    //
    // Refs: https://github.com/nodejs/node/issues/14747
    if (common.isOSX)
      setTimeout(() => { process.send({ payload }, handle); }, 1000);
    else
      process.send({ payload }, handle);

    // Prepare for a clean exit.
    process.channel.unref();
  }));
}