Skip to content
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

test: dynamic port in test cluster disconnect #12545

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions test/parallel/test-cluster-disconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,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 @@ -47,16 +47,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 @@ -72,7 +74,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