Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This demonstrates how server.listen with port set to zero
- * does not function properly in a cluster environment.
- *
- * EXPECTED RESULT: each worker's server starts listening on a
- * unique port as per documentation "A port value of zero will assign a random port."
- *
- * ACTUAL DEMONSTRATED RESULT: each worker's server starts listening
- * on the exact same port as every other worker.
- */
- "use strict";
- var cluster = require('cluster')
- , net = require('net')
- , numForks = 5
- , i
- ;
- if (cluster.isMaster) {
- // fork workers
- for (i = 0; i < numForks; ++i) {
- cluster.fork();
- }
- } else {
- // worker code - create variable scope with executed function
- (function() {
- var server = net.createServer();
- /**
- * server.listen(port, [host], [listeningListener])
- * Begin accepting connections on the specified port and host. If the host
- * is omitted, the server will accept connections directed to any IPv4 address
- * (INADDR_ANY). A port value of zero will assign a random port.
- */
- server.listen(0);
- server.on('listening', function() {
- var address = server.address()
- , port = address.port
- ;
- console.log(port);
- });
- })();
- }
Add Comment
Please, Sign In to add comment