node/test/parallel/test-cluster-concurrent-disconnect.js
Michael Dawson 15164cebce lib,src: update cluster to use Parent
Doc deprecate isMaster and setupMaster in favor
of isPrimary and setupPrimary.

Signed-off-by: Michael Dawson <mdawson@devrus.com>

PR-URL: https://github.com/nodejs/node/pull/36478
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Beth Griggs <bgriggs@redhat.com>
2021-01-05 15:41:45 -05:00

49 lines
1.3 KiB
JavaScript

'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');
if (cluster.isPrimary) {
const workers = [];
const numCPUs = os.cpus().length;
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');
assert.strictEqual(err.code, 'EPIPE');
});
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]();
});
}