Skip to content

Commit

Permalink
fix: better method for getting an open port in cucumber tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Aug 7, 2021
1 parent 05fb77f commit 2d9f3a6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 48 deletions.
4 changes: 2 additions & 2 deletions integration_tests/helpers/baseNodeProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class BaseNodeProcess {
}

async init() {
this.port = await getFreePort(19000, 25000);
this.grpcPort = await getFreePort(19000, 25000);
this.port = await getFreePort();
this.grpcPort = await getFreePort();
this.name = `Basenode${this.port}-${this.name}`;
this.nodeFile = this.nodeFile || "nodeid.json";

Expand Down
2 changes: 1 addition & 1 deletion integration_tests/helpers/mergeMiningProxyProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MergeMiningProxyProcess {
}

async init() {
this.port = await getFreePort(19000, 25000);
this.port = await getFreePort();
this.name = `MMProxy${this.port}-${this.name}`;
this.baseDir = `./temp/base_nodes/${dateFormat(
new Date(),
Expand Down
57 changes: 14 additions & 43 deletions integration_tests/helpers/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,52 +121,23 @@ function hexSwitchEndianness(val) {
return res;
}

// Thanks to https://stackoverflow.com/questions/29860354/in-nodejs-how-do-i-check-if-a-port-is-listening-or-in-use
const portInUse = function (port, callback) {
const server = net.createServer(function (socket) {
socket.write("Echo server\r\n");
socket.pipe(socket);
});

server.listen(port, "127.0.0.1");
server.on("error", function () {
callback(true);
});
server.on("listening", function () {
server.close();
callback(false);
});
};

let index = 0;
const getFreePort = async function (from, to) {
function testPort(port) {
return new Promise((r) => {
portInUse(port, (v) => {
if (v) {
r(false);
const getFreePort = function () {
return new Promise((resolve, reject) => {
const srv = net.createServer(function (_sock) {});
srv.listen(0, function () {
let { port } = srv.address();
srv.close((err) => {
if (err) {
reject(err);
} else {
resolve(port);
}
r(true);
});
});
}

let port = from + index;
if (port > to) {
index = from;
port = from;
}
while (port < to) {
// let port = getRandomInt(from, to);
// await sleep(100);
port++;
index++;
const notInUse = await testPort(port);
// console.log("Port not in use:", notInUse);
if (notInUse) {
return port;
}
}
srv.on("error", function (err) {
reject(err);
});
});
};

const getTransactionOutputHash = function (output) {
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/helpers/walletProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class WalletProcess {
}

async init() {
this.port = await getFreePort(19000, 25000);
this.port = await getFreePort();
this.name = `Wallet${this.port}-${this.name}`;
this.grpcPort = await getFreePort(19000, 25000);
this.grpcPort = await getFreePort();
this.baseDir = `./temp/base_nodes/${dateFormat(
new Date(),
"yyyymmddHHMM"
Expand Down

0 comments on commit 2d9f3a6

Please sign in to comment.