Skip to content
Permalink
Browse files
Adds tests
  • Loading branch information
Prithvirajbilla committed Jul 30, 2018
1 parent 31bfe9f commit 90be7f1d82a95baf1ceb0129367ec673fca3adf1
Showing 4 changed files with 52 additions and 9 deletions.
@@ -11,14 +11,18 @@ interface PortFinderOptions{
* Host to find available port on.
*/
host?: string;
/**
* search start port (equals to basePort when not provided)
*/
startPort?: number;
/**
* Minimum port (takes precedence over `basePort`).
*/
port?: number;
/**
* Maximum port
*/
stopPort?: number;
stopPort?: number;
}

/**
@@ -27,8 +27,6 @@ internals.testPort = function(options, callback) {
options = {};
}

options.port = Number(options.port) || Number(exports.basePort);
options.host = options.host || null;
options.server = options.server || net.createServer(function () {
//
// Create an empty listener for the port testing server.
@@ -72,6 +70,12 @@ internals.testPort = function(options, callback) {
//
exports.basePort = 8000;

//
// ### @highestPort {Number}
// Largest port number is an unsigned short 2**16 -1=65335
//
exports.highestPort = 65535;

//
// ### @basePath {string}
// Default path to begin any socket search from
@@ -88,10 +92,16 @@ exports.getPort = function (options, callback) {
if (!callback) {
callback = options;
options = {};

}

// Largest port number is an unsigned short 2**16 -1=65335
options.stopPort = Number(options.stopPort) || 65535
options.port = Number(options.port) || Number(exports.basePort);
options.host = options.host || null;
options.stopPort = Number(options.stopPort) || Number(exports.highestPort)

if(!options.startPort) {
options.startPort = options.port
}

if (options.host) {

@@ -160,16 +170,16 @@ exports.getPort = function (options, callback) {

if (openPorts[0] === openPorts[openPorts.length-1]) {
// if first === last, we found an open port
if(openPorts[0] < option.stopPort) {
if(openPorts[0] <= options.stopPort) {
return callback(null, openPorts[0]);
}
else {
var msg = 'No open ports found in between '+ options.basePort + ' and ' + options.stopPort;
var msg = 'No open ports found in between '+ options.startPort + ' and ' + options.stopPort;
return callback(Error(msg));
}
} else {
// otherwise, try again, using sorted port, aka, highest open for >= 1 host
return exports.getPort({ port: openPorts.pop(), host: options.host }, callback);
return exports.getPort({ port: openPorts.pop(), host: options.host, startPort: options.startPort, stopPort: options.stopPort }, callback);
}

});
@@ -1,7 +1,7 @@
{
"name": "portfinder",
"description": "A simple tool to find an open port on the current machine",
"version": "1.0.13",
"version": "1.0.14",
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
"repository": {
"type": "git",
@@ -46,6 +46,35 @@ vows.describe('portfinder').addBatch({
assert.isTrue(!err);
assert.equal(port, 32773);
}
},
"the getPort() method with stopPort smaller than available port": {
topic: function() {
// stopPort: 32722 is smaller than available port 32773 (32768 + 5)
setTimeout(function() {
portfinder.getPort({ stopPort: 32772 }, this.callback);
}.bind(this), 3000); //wait for cleanup of bound hosts.
},
"should return error": function(err, port) {
assert.isTrue(!!err);
assert.equal(
err.message,
'No open ports found in between 32768 and 32772'
);
return;
}
},
"the getPort() method with stopPort greater than available port": {
topic: function() {
// stopPort: 32774 is greater than available port 32773 (32768 + 5)
setTimeout(function() {
portfinder.getPort({ stopPort: 32774 }, this.callback);
}.bind(this), 3000); //wait for cleanup of bound hosts.
},
"should respond with the first free port (32773) less than provided stopPort": function(err, port) {
if (err) { debugVows(err); }
assert.isTrue(!err);
assert.equal(port, 32773);
}
}
}
}

0 comments on commit 90be7f1

Please sign in to comment.