Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 18 additions & 23 deletions test/sequential/test-cluster-disconnect-suicide-race.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
'use strict';

// Test should fail in Node.js 5.4.1 and pass in later versions.

const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
const os = require('os');

if (cluster.isMaster) {
function forkWorker(action) {
const worker = cluster.fork({ action });
worker.on('disconnect', common.mustCall(() => {
assert.strictEqual(worker.suicide, true);
}));
cluster.on('exit', (worker, code) => {
assert.strictEqual(code, 0, 'worker exited with error');
});

return cluster.fork();
}

worker.on('exit', common.mustCall(() => {
assert.strictEqual(worker.suicide, true);
}));
}
var eventFired = false;

const cpus = os.cpus().length;
const tries = cpus > 8 ? 64 : cpus * 8;
cluster.worker.disconnect();

cluster.on('exit', common.mustCall((worker, code) => {
assert.strictEqual(code, 0, 'worker exited with error');
}, tries * 2));

for (let i = 0; i < tries; ++i) {
forkWorker('disconnect');
forkWorker('kill');
}
} else {
cluster.worker[process.env.action]();
}
process.nextTick(common.mustCall(() => {
assert.strictEqual(eventFired, false, 'disconnect event should wait for ack');
}));

cluster.worker.on('disconnect', common.mustCall(() => {
eventFired = true;
}));