Skip to content

Commit

Permalink
now checks ports before assign to chromy instance
Browse files Browse the repository at this point in the history
getFreePorts now blocks pMap until a vaid portlist has been validated.
  • Loading branch information
garris committed Feb 12, 2018
1 parent c238b40 commit 8dde56d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
14 changes: 12 additions & 2 deletions core/util/createBitmaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ var fs = require('./fs');
var each = require('./each');
var pMap = require('p-map');

var getFreePorts = require('./getFreePorts');
var runCasper = require('./runCasper');
var runChromy = require('./runChromy');
const ensureDirectoryPath = require('./ensureDirectoryPath');
var logger = require('./logger')('createBitmaps');

var CONCURRENCY_DEFAULT = 10;
const CHROMY_STARTING_PORT_NUMBER = 9222;
var GENERATE_BITMAPS_SCRIPT = 'capture/genBitmaps.js';

function regexTest (string, search) {
Expand Down Expand Up @@ -120,9 +122,17 @@ function delegateScenarios (config) {
});
});

var asyncCaptureLimit = config.asyncCaptureLimit === 0 ? 1 : config.asyncCaptureLimit || CONCURRENCY_DEFAULT;
const PORT = (config.startingPort || CHROMY_STARTING_PORT_NUMBER);
const asyncCaptureLimit = config.asyncCaptureLimit === 0 ? 1 : config.asyncCaptureLimit || CONCURRENCY_DEFAULT;

return getFreePorts(PORT, scenarioViews.length).then(freeports => {
console.log('These ports will be used:', JSON.stringify(freeports));
scenarioViews.forEach((scenarioView, i) => {
scenarioView.assignedPort = freeports[i];
});
return pMap(scenarioViews, runChromy, {concurrency: asyncCaptureLimit});
});

return pMap(scenarioViews, runChromy, {concurrency: asyncCaptureLimit});
}

function pad (number) {
Expand Down
34 changes: 34 additions & 0 deletions core/util/getFreePorts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const portfinder = require('portfinder');
/**
* Gets the free ports.
*
* @param {number} startingPort The starting port
* @param {number} requestedPorts how many ports should we find?
* @return {Array} The free ports.
*/
module.exports = function getFreePorts(startingPort, requestedPorts) {
return new Promise((resolve, reject) => {
const R = resolve;
console.log(`searching for ${requestedPorts} available ports.`);
const requestedAmount = requestedPorts;
let freePorts = [];

function findFreePorts(startPort, pointer) {
const PTR = pointer || 1;
// console.log('freePorts > ', PTR, JSON.stringify(freePorts));
if (PTR > requestedAmount) {
R(freePorts);
return;
}
portfinder.basePort = startPort;
portfinder.getPort(function (err, port) {
if (err) {
reject(new Error(err));
}
freePorts[PTR-1] = port;
return findFreePorts(port + 1, PTR + 1);
});
}
findFreePorts(startingPort);
})
}
10 changes: 6 additions & 4 deletions core/util/runChromy.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ module.exports = function (args) {
const viewport = args.viewport;
const config = args.config;
const runId = args.id;
const assignedPort = args.assignedPort;
const scenarioLabelSafe = makeSafe(scenario.label);
const variantOrScenarioLabelSafe = scenario._parent ? makeSafe(scenario._parent.label) : scenarioLabelSafe;
return processScenarioView(scenario, variantOrScenarioLabelSafe, scenarioLabelSafe, viewport, config, runId);

return processScenarioView(scenario, variantOrScenarioLabelSafe, scenarioLabelSafe, viewport, config, runId, assignedPort);
};

/**
Expand All @@ -40,7 +41,7 @@ module.exports = function (args) {
* @param {[type]} config [description]
* @return {[type]} [description]
*/
function processScenarioView (scenario, variantOrScenarioLabelSafe, scenarioLabelSafe, viewport, config, runId) {
function processScenarioView (scenario, variantOrScenarioLabelSafe, scenarioLabelSafe, viewport, config, runId, assignedPort) {
if (!config.paths) {
config.paths = {};
}
Expand All @@ -60,7 +61,8 @@ function processScenarioView (scenario, variantOrScenarioLabelSafe, scenarioLabe
const VP_H = viewport.height || viewport.viewport.height;

const DEFAULT_CHROME_FLAGS = ['--disable-gpu', '--force-device-scale-factor=1', '--disable-infobars=true'];
const PORT = (config.startingPort || CHROMY_STARTING_PORT_NUMBER) + runId;
// const PORT = (config.startingPort || CHROMY_STARTING_PORT_NUMBER) + runId;
const PORT = assignedPort;
let defaultOptions = {
chromeFlags: undefined,
port: PORT,
Expand Down

0 comments on commit 8dde56d

Please sign in to comment.