2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2012-05-02 18:38:31 +02:00
|
|
|
// test-cluster-worker-exit.js
|
|
|
|
// verifies that, when a child process exits (by calling `process.exit(code)`)
|
|
|
|
// - the parent receives the proper events in the proper order, no duplicates
|
|
|
|
// - the exitCode and signalCode are correct in the 'exit' event
|
2015-11-10 20:14:34 +01:00
|
|
|
// - the worker.exitedAfterDisconnect flag, and worker.state are correct
|
2012-05-02 18:38:31 +02:00
|
|
|
// - the worker process actually goes away
|
|
|
|
|
2016-08-24 22:55:12 +02:00
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const cluster = require('cluster');
|
2012-05-02 18:38:31 +02:00
|
|
|
|
2016-08-24 22:55:12 +02:00
|
|
|
const EXIT_CODE = 42;
|
2012-05-02 18:38:31 +02:00
|
|
|
|
|
|
|
if (cluster.isWorker) {
|
2016-08-24 22:55:12 +02:00
|
|
|
const http = require('http');
|
|
|
|
const server = http.Server(() => { });
|
2012-05-02 18:38:31 +02:00
|
|
|
|
2016-08-24 22:55:12 +02:00
|
|
|
server.once('listening', common.mustCall(() => {
|
2012-05-02 18:38:31 +02:00
|
|
|
process.exit(EXIT_CODE);
|
2016-08-24 22:55:12 +02:00
|
|
|
}));
|
2012-05-02 18:38:31 +02:00
|
|
|
server.listen(common.PORT, '127.0.0.1');
|
|
|
|
|
|
|
|
} else if (cluster.isMaster) {
|
|
|
|
|
2016-08-24 22:55:12 +02:00
|
|
|
const expected_results = {
|
2016-01-13 21:42:45 +01:00
|
|
|
cluster_emitDisconnect: [1, "the cluster did not emit 'disconnect'"],
|
|
|
|
cluster_emitExit: [1, "the cluster did not emit 'exit'"],
|
|
|
|
cluster_exitCode: [EXIT_CODE, 'the cluster exited w/ incorrect exitCode'],
|
|
|
|
cluster_signalCode: [null, 'the cluster exited w/ incorrect signalCode'],
|
|
|
|
worker_emitDisconnect: [1, "the worker did not emit 'disconnect'"],
|
|
|
|
worker_emitExit: [1, "the worker did not emit 'exit'"],
|
|
|
|
worker_state: ['disconnected', 'the worker state is incorrect'],
|
|
|
|
worker_suicideMode: [false, 'the worker.suicide flag is incorrect'],
|
2015-11-10 20:14:34 +01:00
|
|
|
worker_exitedAfterDisconnect: [false,
|
|
|
|
'the .exitedAfterDisconnect flag is incorrect'],
|
2016-01-13 21:42:45 +01:00
|
|
|
worker_died: [true, 'the worker is still running'],
|
|
|
|
worker_exitCode: [EXIT_CODE, 'the worker exited w/ incorrect exitCode'],
|
|
|
|
worker_signalCode: [null, 'the worker exited w/ incorrect signalCode']
|
2012-05-02 18:38:31 +02:00
|
|
|
};
|
2016-08-24 22:55:12 +02:00
|
|
|
const results = {
|
2016-01-13 21:42:45 +01:00
|
|
|
cluster_emitDisconnect: 0,
|
|
|
|
cluster_emitExit: 0,
|
|
|
|
worker_emitDisconnect: 0,
|
|
|
|
worker_emitExit: 0
|
2012-05-02 18:38:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// start worker
|
2016-08-24 22:55:12 +02:00
|
|
|
const worker = cluster.fork();
|
2012-05-02 18:38:31 +02:00
|
|
|
|
|
|
|
// Check cluster events
|
2016-08-24 22:55:12 +02:00
|
|
|
cluster.on('disconnect', common.mustCall(() => {
|
2012-05-02 18:38:31 +02:00
|
|
|
results.cluster_emitDisconnect += 1;
|
2016-08-24 22:55:12 +02:00
|
|
|
}));
|
|
|
|
cluster.on('exit', common.mustCall((worker) => {
|
2012-05-02 18:38:31 +02:00
|
|
|
results.cluster_exitCode = worker.process.exitCode;
|
|
|
|
results.cluster_signalCode = worker.process.signalCode;
|
|
|
|
results.cluster_emitExit += 1;
|
2016-08-24 22:55:12 +02:00
|
|
|
}));
|
2012-05-02 18:38:31 +02:00
|
|
|
|
2013-08-13 00:01:05 +02:00
|
|
|
// Check worker events and properties
|
2016-08-24 22:55:12 +02:00
|
|
|
worker.on('disconnect', common.mustCall(() => {
|
2012-05-02 18:38:31 +02:00
|
|
|
results.worker_emitDisconnect += 1;
|
|
|
|
results.worker_suicideMode = worker.suicide;
|
2015-11-10 20:14:34 +01:00
|
|
|
results.worker_exitedAfterDisconnect = worker.exitedAfterDisconnect;
|
2012-05-02 18:38:31 +02:00
|
|
|
results.worker_state = worker.state;
|
2015-11-04 23:08:07 +01:00
|
|
|
if (results.worker_emitExit > 0) {
|
2016-08-24 22:55:12 +02:00
|
|
|
process.nextTick(() => finish_test());
|
2015-11-04 23:08:07 +01:00
|
|
|
}
|
2016-08-24 22:55:12 +02:00
|
|
|
}));
|
2012-05-02 18:38:31 +02:00
|
|
|
|
|
|
|
// Check that the worker died
|
2016-08-24 22:55:12 +02:00
|
|
|
worker.once('exit', common.mustCall((exitCode, signalCode) => {
|
2012-05-02 18:38:31 +02:00
|
|
|
results.worker_exitCode = exitCode;
|
|
|
|
results.worker_signalCode = signalCode;
|
|
|
|
results.worker_emitExit += 1;
|
2016-08-24 22:55:12 +02:00
|
|
|
results.worker_died = !common.isAlive(worker.process.pid);
|
2015-11-04 23:08:07 +01:00
|
|
|
if (results.worker_emitDisconnect > 0) {
|
2016-08-24 22:55:12 +02:00
|
|
|
process.nextTick(() => finish_test());
|
2015-11-04 23:08:07 +01:00
|
|
|
}
|
2016-08-24 22:55:12 +02:00
|
|
|
}));
|
2012-05-02 18:38:31 +02:00
|
|
|
|
2016-08-24 22:55:12 +02:00
|
|
|
const finish_test = function() {
|
2012-05-02 18:38:31 +02:00
|
|
|
try {
|
|
|
|
checkResults(expected_results, results);
|
|
|
|
} catch (exc) {
|
2016-08-24 22:55:12 +02:00
|
|
|
if (exc.name !== 'AssertionError') {
|
2012-05-02 18:38:31 +02:00
|
|
|
console.trace(exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
process.exit(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
process.exit(0);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// some helper functions ...
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
function checkResults(expected_results, results) {
|
2016-08-24 22:55:12 +02:00
|
|
|
for (const k in expected_results) {
|
2016-01-13 21:42:45 +01:00
|
|
|
const actual = results[k];
|
|
|
|
const expected = expected_results[k];
|
2012-05-02 18:38:31 +02:00
|
|
|
|
2016-08-24 22:55:12 +02:00
|
|
|
assert.strictEqual(actual,
|
|
|
|
expected && expected.length ? expected[0] : expected,
|
|
|
|
(expected[1] || '') +
|
|
|
|
` [expected: ${expected[0]} / actual: ${actual}]`);
|
2012-05-02 18:38:31 +02:00
|
|
|
}
|
2015-05-19 13:00:06 +02:00
|
|
|
}
|