Skip to content

Commit

Permalink
test: dynamic port in cluster disconnect
Browse files Browse the repository at this point in the history
Removed common.PORT from test-cluster-disconnect to eliminate the
possibility that a port used in another test will collide
with common.PORT.

PR-URL: #12545
Refs: #12376
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Alexey Orlenko <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
  • Loading branch information
Sebastian Plesciuc authored and MylesBorins committed Jul 11, 2017
1 parent d71de28 commit 1cd41e7
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions test/parallel/test-cluster-disconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const net = require('net');
if (cluster.isWorker) {
net.createServer((socket) => {
socket.end('echo');
}).listen(common.PORT, '127.0.0.1');
}).listen(0, '127.0.0.1');

net.createServer((socket) => {
socket.end('echo');
}).listen(common.PORT + 1, '127.0.0.1');

}).listen(0, '127.0.0.1');
} else if (cluster.isMaster) {
const servers = 2;
const serverPorts = new Set();

// test a single TCP server
const testConnection = (port, cb) => {
Expand All @@ -26,16 +26,18 @@ if (cluster.isWorker) {
// check result
socket.on('end', common.mustCall(() => {
cb(result === 'echo');
serverPorts.delete(port);
}));
});
};

// test both servers created in the cluster
const testCluster = (cb) => {
let done = 0;
const portsArray = Array.from(serverPorts);

for (let i = 0; i < servers; i++) {
testConnection(common.PORT + i, (success) => {
testConnection(portsArray[i], (success) => {
assert.ok(success);
done += 1;
if (done === servers) {
Expand All @@ -51,7 +53,9 @@ if (cluster.isWorker) {
let online = 0;

for (let i = 0, l = workers; i < l; i++) {
cluster.fork().on('listening', common.mustCall(() => {
cluster.fork().on('listening', common.mustCall((address) => {
serverPorts.add(address.port);

online += 1;
if (online === workers * servers) {
cb();
Expand Down

0 comments on commit 1cd41e7

Please sign in to comment.