-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cluster: wait for all servers closing before disconnect #1400
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
var common = require('../common'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lint will want a |
||
var assert = require('assert'); | ||
var cluster = require('cluster'); | ||
var net = require('net'); | ||
|
||
if (cluster.isWorker) { | ||
net.createServer(function(socket) { | ||
// Wait for any data, then close connection | ||
socket.on('data', socket.end.bind(socket)); | ||
}).listen(common.PORT, common.localhostIPv4); | ||
} else if (cluster.isMaster) { | ||
|
||
var connectionDone; | ||
var checks = { | ||
disconnectedOnClientsEnd: false, | ||
workerDied: 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() { | ||
net.createConnection(common.PORT, common.localhostIPv4, function() { | ||
var socket = this; | ||
setTimeout(function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This outer setTimeout is unnecessary. Now that the tests run in parallel, perhaps it does no harm, but I would remove it. |
||
worker.disconnect(); | ||
setTimeout(function() { | ||
socket.write('.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test is generally nicely commented, thanks! I'd suggest changing |
||
connectionDone = true; | ||
}, 1000); | ||
}, 1000); | ||
}); | ||
}); | ||
|
||
// Check worker events and properties | ||
worker.once('disconnect', function() { | ||
checks.disconnectedOnClientsEnd = connectionDone; | ||
}); | ||
|
||
// Check that the worker died | ||
worker.once('exit', function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is a no-op. It confirms what the |
||
checks.workerDied = !alive(worker.process.pid); | ||
process.nextTick(function() { | ||
process.exit(0); | ||
}); | ||
}); | ||
|
||
process.once('exit', function() { | ||
assert.ok(checks.disconnectedOnClientsEnd, 'The worker disconnected before all clients are ended'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. run |
||
assert.ok(checks.workerDied, 'The worker did not die'); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this block necessary? Won't
checkRemainingHandles
itself handle this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we have no any handles yet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. Thanks for clarifying :-)