2020-04-01 12:22:07 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Ref: https://github.com/nodejs/node/issues/32106
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
|
|
|
|
const assert = require('assert');
|
|
|
|
const cluster = require('cluster');
|
|
|
|
const os = require('os');
|
|
|
|
|
2020-12-10 21:53:44 +00:00
|
|
|
if (cluster.isPrimary) {
|
2020-04-01 12:22:07 +00:00
|
|
|
const workers = [];
|
2022-12-29 01:38:04 +00:00
|
|
|
const numCPUs = os.availableParallelism();
|
2020-04-01 12:22:07 +00:00
|
|
|
let waitOnline = numCPUs;
|
|
|
|
for (let i = 0; i < numCPUs; i++) {
|
|
|
|
const worker = cluster.fork();
|
|
|
|
workers[i] = worker;
|
|
|
|
worker.once('online', common.mustCall(() => {
|
|
|
|
if (--waitOnline === 0)
|
|
|
|
for (const worker of workers)
|
|
|
|
if (worker.isConnected())
|
|
|
|
worker.send(i % 2 ? 'disconnect' : 'destroy');
|
|
|
|
}));
|
|
|
|
|
|
|
|
// These errors can occur due to the nature of the test, we might be trying
|
|
|
|
// to send messages when the worker is disconnecting.
|
|
|
|
worker.on('error', (err) => {
|
|
|
|
assert.strictEqual(err.syscall, 'write');
|
2024-07-15 20:32:26 +00:00
|
|
|
if (common.isMacOS) {
|
2021-11-21 20:44:39 +00:00
|
|
|
assert(['EPIPE', 'ENOTCONN'].includes(err.code), err);
|
|
|
|
} else {
|
2022-07-26 13:03:18 +00:00
|
|
|
assert(['EPIPE', 'ECONNRESET'].includes(err.code), err);
|
2021-11-21 20:44:39 +00:00
|
|
|
}
|
2020-04-01 12:22:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
worker.once('disconnect', common.mustCall(() => {
|
|
|
|
for (const worker of workers)
|
|
|
|
if (worker.isConnected())
|
|
|
|
worker.send('disconnect');
|
|
|
|
}));
|
|
|
|
|
|
|
|
worker.once('exit', common.mustCall((code, signal) => {
|
|
|
|
assert.strictEqual(code, 0);
|
|
|
|
assert.strictEqual(signal, null);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
process.on('message', (msg) => {
|
|
|
|
if (cluster.worker.isConnected())
|
|
|
|
cluster.worker[msg]();
|
|
|
|
});
|
|
|
|
}
|