Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #279 from chiahrens/fixPortCheck
Browse files Browse the repository at this point in the history
Updated logic to check every port in the range, and move to the next set
  • Loading branch information
cahrens777 authored Apr 27, 2020
2 parents 189dd60 + 148e267 commit 7bee201
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/util/port_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ const util = {
let attempts = 0;

const acquire = () => {
checkPorts([util.getNextPort()], (result) => {
// check all the ports in the range. If fail, move to next set
const startPort = util.getNextPort();
const ports = [];
for (let i = startPort; i < startPort + settings.BASE_PORT_SPACING; i++) {
ports.push(i);
}
checkPorts(ports, (result) => {
if (result[0].available) {
return callback(null, result[0].port);
} else {
Expand Down
41 changes: 41 additions & 0 deletions test/utils/port_util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,44 @@ test('should throw exception after maximum retries', () => {
expect(spy.called).toEqual(true);
expect(spy.args[0][0].message).toEqual('Gave up looking for an available port after 100 attempts.');
});

test('should acquire next port if any port is not available', () => {

// can't reset port_util.portCursor, so we just have to start where it left off
expect(portUtil.getNextPort()).toEqual(12327)

checkPorts.mockImplementation((arr, cb) => {
// second port not available
arr[1] === 12331 ? cb([{
port: arr[0],
available: false
}]) : cb([{
port: arr[0],
available: true
}]);
});

const spy = sinon.spy();
portUtil.acquirePort(spy);
expect(spy.called).toEqual(true);
expect(spy.args[0]).toEqual([null, 12333]);

// can't reset port_util.portCursor, so we just have to start where it left off
expect(portUtil.getNextPort()).toEqual(12336)

checkPorts.mockImplementation((arr, cb) => {
// third port not available
arr[1] === 12340 ? cb([{
port: arr[0],
available: false
}]) : cb([{
port: arr[0],
available: true
}]);
});

const spy2 = sinon.spy();
portUtil.acquirePort(spy2);
expect(spy2.called).toEqual(true);
expect(spy2.args[0]).toEqual([null, 12342]);
});

0 comments on commit 7bee201

Please sign in to comment.