Skip to content

Commit

Permalink
cluster: disconnect event was not emitted correctly
Browse files Browse the repository at this point in the history
Fix for #1304
Inside of a worker, disconnect event was not emitted on cluster.worker
  • Loading branch information
Olegas committed Apr 9, 2015
1 parent 1219e74 commit ded311c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ function workerInit() {
});
cluster.worker = worker;
process.once('disconnect', function() {
worker.emit('disconnect');
if (!worker.suicide) {
// Unexpected disconnect, master exited, or some such nastiness, so
// worker exits immediately.
Expand Down
10 changes: 9 additions & 1 deletion test/parallel/test-cluster-worker-disconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ if (cluster.isWorker) {

}).listen(common.PORT, '127.0.0.1');

cluster.worker.on('disconnect', function() {
process.exit(42);
});

} else if (cluster.isMaster) {

var checks = {
Expand All @@ -18,6 +22,7 @@ if (cluster.isWorker) {
},
worker: {
emitDisconnect: false,
emitDisconnectInsideWorker: false,
emitExit: false,
state: false,
suicideMode: false,
Expand Down Expand Up @@ -59,9 +64,11 @@ if (cluster.isWorker) {
});

// Check that the worker died
worker.once('exit', function() {
worker.once('exit', function(code) {
checks.worker.emitExit = true;
checks.worker.died = !alive(worker.process.pid);
checks.worker.emitDisconnectInsideWorker = code === 42;

process.nextTick(function() {
process.exit(0);
});
Expand All @@ -74,6 +81,7 @@ if (cluster.isWorker) {

// events
assert.ok(w.emitDisconnect, 'Disconnect event did not emit');
assert.ok(w.emitDisconnectInsideWorker, 'Disconnect event did not emit inside worker');
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');
Expand Down

0 comments on commit ded311c

Please sign in to comment.