11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst cluster = require('cluster');
61cb0ef41Sopenharmony_ciconst net = require('net');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst payload = 'a'.repeat(800004);
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
111cb0ef41Sopenharmony_ci  const server = net.createServer();
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  server.on('connection', common.mustCall((socket) => { socket.unref(); }));
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  const worker = cluster.fork();
161cb0ef41Sopenharmony_ci  worker.on('message', common.mustCall(({ payload: received }, handle) => {
171cb0ef41Sopenharmony_ci    assert.strictEqual(payload, received);
181cb0ef41Sopenharmony_ci    assert(handle instanceof net.Socket);
191cb0ef41Sopenharmony_ci    server.close();
201cb0ef41Sopenharmony_ci    handle.destroy();
211cb0ef41Sopenharmony_ci  }));
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
241cb0ef41Sopenharmony_ci    const port = server.address().port;
251cb0ef41Sopenharmony_ci    const socket = new net.Socket();
261cb0ef41Sopenharmony_ci    socket.connect(port, (err) => {
271cb0ef41Sopenharmony_ci      assert.ifError(err);
281cb0ef41Sopenharmony_ci      worker.send({ payload }, socket);
291cb0ef41Sopenharmony_ci    });
301cb0ef41Sopenharmony_ci  }));
311cb0ef41Sopenharmony_ci} else {
321cb0ef41Sopenharmony_ci  process.on('message', common.mustCall(({ payload: received }, handle) => {
331cb0ef41Sopenharmony_ci    assert.strictEqual(payload, received);
341cb0ef41Sopenharmony_ci    assert(handle instanceof net.Socket);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci    // On macOS, the primary process might not receive a message if it is sent
371cb0ef41Sopenharmony_ci    // to soon, and then subsequent messages are also sometimes not received.
381cb0ef41Sopenharmony_ci    //
391cb0ef41Sopenharmony_ci    // (Is this a bug or expected operating system behavior like the way a file
401cb0ef41Sopenharmony_ci    // watcher is returned before it's actually watching the file system on
411cb0ef41Sopenharmony_ci    // macOS?)
421cb0ef41Sopenharmony_ci    //
431cb0ef41Sopenharmony_ci    // Send a second message after a delay on macOS.
441cb0ef41Sopenharmony_ci    //
451cb0ef41Sopenharmony_ci    // Refs: https://github.com/nodejs/node/issues/14747
461cb0ef41Sopenharmony_ci    if (common.isOSX)
471cb0ef41Sopenharmony_ci      setTimeout(() => { process.send({ payload }, handle); }, 1000);
481cb0ef41Sopenharmony_ci    else
491cb0ef41Sopenharmony_ci      process.send({ payload }, handle);
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci    // Prepare for a clean exit.
521cb0ef41Sopenharmony_ci    process.channel.unref();
531cb0ef41Sopenharmony_ci  }));
541cb0ef41Sopenharmony_ci}
55