All files / server/test port.js

90.91% Statements 10/11
66.67% Branches 2/3
100% Functions 2/2
90% Lines 9/10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24      29x 29x   146x     29x 146x     146x         146x 146x     29x  
// Port - generate a random valid port number that has not been used yet
 
// Get an unused port in the user range (2000 - 10000)
const ports = [];
const limit = 1000;
 
const random = () => 1024 + parseInt(Math.random() * 48151);
 
// Keep a count of how many times we tried to find a port to avoid infinite loop
const randPort = (i = 0) => {
  const port = random();
 
  // If "i" is already taken try again
  Iif (port in ports) {
    return randPort(i + 1);
  }
 
  // Add it to the list of ports already being used and return it
  ports.push(port);
  return port;
}
 
module.exports = randPort;