Files
node/test/parallel/test-cluster-worker-forced-exit.js
cjihrig 19907c27a6 test: use mustCall() for simple flow tracking
Many of the tests use variables to track when callback functions
are invoked or events are emitted. These variables are then
asserted on process exit. This commit replaces this pattern in
straightforward cases with common.mustCall(). This makes the
tests easier to reason about, leads to a net reduction in lines
of code, and uncovered a few bugs in tests. This commit also
replaces some callbacks that should never be called with
common.fail().

PR-URL: https://github.com/nodejs/node/pull/7753
Reviewed-By: Wyatt Preul <wpreul@gmail.com>
Reviewed-By: Minwoo Jung <jmwsoft@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2017-01-31 20:04:31 -05:00

49 lines
1.1 KiB
JavaScript

'use strict';
const common = require('../common');
var assert = require('assert');
var cluster = require('cluster');
var SENTINEL = 42;
// workers forcibly exit when control channel is disconnected, if
// their .suicide flag isn't set
//
// test this by:
//
// 1 setup worker to wait a short time after disconnect, and exit
// with a sentinel value
// 2 disconnect worker with cluster's disconnect, confirm sentinel
// 3 disconnect worker with child_process's disconnect, confirm
// no sentinel value
if (cluster.isWorker) {
process.on('disconnect', function(msg) {
setTimeout(function() {
process.exit(SENTINEL);
}, 10);
});
return;
}
checkUnforced();
checkForced();
function checkUnforced() {
cluster.fork()
.on('online', function() {
this.disconnect();
})
.on('exit', common.mustCall(function(status) {
assert.equal(status, SENTINEL);
}));
}
function checkForced() {
cluster.fork()
.on('online', function() {
this.process.disconnect();
})
.on('exit', common.mustCall(function(status) {
assert.equal(status, 0);
}));
}