0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-28 22:46:31 +01:00
nodejs/test/parallel/test-cluster-worker-disconnect.js
isaacs 3e1b1dd4a9 Remove excessive copyright/license boilerplate
The copyright and license notice is already in the LICENSE file.  There
is no justifiable reason to also require that it be included in every
file, since the individual files are not individually distributed except
as part of the entire package.
2015-01-12 15:30:28 -08:00

89 lines
2.0 KiB
JavaScript

var common = require('../common');
var assert = require('assert');
var cluster = require('cluster');
if (cluster.isWorker) {
var http = require('http');
http.Server(function() {
}).listen(common.PORT, '127.0.0.1');
} else if (cluster.isMaster) {
var checks = {
cluster: {
emitDisconnect: false,
emitExit: false,
callback: false
},
worker: {
emitDisconnect: false,
emitExit: false,
state: false,
suicideMode: false,
died: false
}
};
// helper function to check if a process is alive
var alive = function(pid) {
try {
process.kill(pid, 0);
return true;
} catch (e) {
return false;
}
};
// start worker
var worker = cluster.fork();
// Disconnect worker when it is ready
worker.once('listening', function() {
worker.disconnect();
});
// Check cluster events
cluster.once('disconnect', function() {
checks.cluster.emitDisconnect = true;
});
cluster.once('exit', function() {
checks.cluster.emitExit = true;
});
// Check worker events and properties
worker.once('disconnect', function() {
checks.worker.emitDisconnect = true;
checks.worker.suicideMode = worker.suicide;
checks.worker.state = worker.state;
});
// Check that the worker died
worker.once('exit', function() {
checks.worker.emitExit = true;
checks.worker.died = !alive(worker.process.pid);
process.nextTick(function() {
process.exit(0);
});
});
process.once('exit', function() {
var w = checks.worker;
var c = checks.cluster;
// events
assert.ok(w.emitDisconnect, 'Disconnect event did not emit');
assert.ok(c.emitDisconnect, 'Disconnect event did not emit');
assert.ok(w.emitExit, 'Exit event did not emit');
assert.ok(c.emitExit, 'Exit event did not emit');
// flags
assert.equal(w.state, 'disconnected', 'The state property was not set');
assert.equal(w.suicideMode, true, 'Suicide mode was not set');
// is process alive
assert.ok(w.died, 'The worker did not die');
});
}