Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
refactor(launcher): skip the child process if only one capability is …
Browse files Browse the repository at this point in the history
…requested

Closes #603
  • Loading branch information
juliemr committed Apr 2, 2014
1 parent ebd5dd7 commit c9dbbaa
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 79 deletions.
142 changes: 64 additions & 78 deletions lib/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

var util = require('util'),
path = require('path'),
fs = require('fs'),
glob = require('glob'),
child = require('child_process'),
ConfigParser = require('./configParser');

Expand All @@ -21,20 +19,20 @@ var noLineLog_ = function(stuff) {
process.stdout.write(launcherPrefix + stuff);
}

var reportHeader_ = function(childFork) {
var capability = childFork.capability;
var reportHeader_ = function(driverInstance) {
var capability = driverInstance.capability;
var eol = require('os').EOL;

var outputHeader = eol + '------------------------------------' + eol;
outputHeader += 'PID: ' + childFork.process.pid + ' (capability: ';
outputHeader += 'PID: ' + driverInstance.process.pid + ' (capability: ';
outputHeader += (capability.browserName) ?
capability.browserName : '';
outputHeader += (capability.version) ?
capability.version : '';
outputHeader += (capability.platform) ?
capability.platform : '';
outputHeader += (childFork.runNumber) ?
' #' + childFork.runNumber : '';
outputHeader += (driverInstance.runNumber) ?
' #' + driverInstance.runNumber : '';
outputHeader += ')' + eol;
outputHeader += '------------------------------------' + eol;

Expand All @@ -51,7 +49,7 @@ var reportHeader_ = function(childFork) {
var init = function(configFile, additionalConfig) {

var capabilityRunCount,
childForks = [],
driverInstances = [],
launcherExitCode = 0;

var configParser = new ConfigParser();
Expand All @@ -65,8 +63,8 @@ var init = function(configFile, additionalConfig) {

var listRemainingForks = function() {
var remaining = 0;
childForks.forEach(function(childFork) {
if (!childFork.done) {
driverInstances.forEach(function(driverInstance) {
if (!driverInstance.done) {
remaining++;
}
});
Expand All @@ -76,11 +74,11 @@ var init = function(configFile, additionalConfig) {
};

var logSummary = function() {
childForks.forEach(function(childFork) {
var shortChildName = childFork.capability.browserName +
(childFork.runNumber ? ' #' + childFork.runNumber : '');
if (childFork.failedCount) {
log_(shortChildName + ' failed ' + childFork.failedCount + ' test(s)');
driverInstances.forEach(function(driverInstance) {
var shortChildName = driverInstance.capability.browserName +
(driverInstance.runNumber ? ' #' + driverInstance.runNumber : '');
if (driverInstance.failedCount) {
log_(shortChildName + ' failed ' + driverInstance.failedCount + ' test(s)');
} else {
log_(shortChildName + ' passed');
}
Expand All @@ -95,12 +93,12 @@ var init = function(configFile, additionalConfig) {
'config.capabilities will be ignored');
}

// Merge 'capabilities' and 'multiCapabilities', if applicable.
// Use capabilities if multiCapabilities is empty.
if (!config.multiCapabilities.length) {
config.multiCapabilities = [config.capabilities];
}

// Loop through capabilities and launch forks of runner.js
// Loop through capabilities and set up forks of runner.js
for (var i = 0; i < config.multiCapabilities.length; i++) {

// Determine how many times to run the capability
Expand All @@ -109,7 +107,7 @@ var init = function(configFile, additionalConfig) {

// Fork the child runners.
for (var j = 0; j < capabilityRunCount; j++) {
childForks.push({
driverInstances.push({
configFile: configFile,
additionalConfig: additionalConfig,
capability: config.multiCapabilities[i],
Expand All @@ -118,67 +116,52 @@ var init = function(configFile, additionalConfig) {
}
}

// If we're launching multiple runners, aggregate output until completion.
// Otherwise, there is a single runner, let's pipe the output straight
// through to maintain realtime reporting.
if (childForks.length === 1) {
var childFork = childForks[0];
childFork.process = child.fork(
__dirname + "/runFromLauncher.js",
process.argv.slice(2),
{cwd: process.cwd()});
reportHeader_(childFork);

childFork.process.send({
command: 'run',
configFile: childFork.configFile,
additionalConfig: childFork.additionalConfig,
capability: childFork.capability
// If there is a single runner, avoid starting a separate process
// and print output directly.
// Otherwise, if we're launching multiple runners, aggregate output until
// completion.
if (driverInstances.length === 1) {
var driverInstance = driverInstances[0];

var Runner = require('./runner');
config.capabilities = driverInstance.capability;

var runner = new Runner(config);
runner.run().then(function(exitCode) {
process.exit(exitCode);
}).catch(function(err) {
log_('Error: ' + err.message);
process.exit(1);
});

childFork.process.on('error', function(err) {
log_('Runner Process(' + childFork.process.pid + ') Error: ' + err);
});


childFork.process.on('message', function(m) {
switch (m.event) {
case 'testsDone':
childFork.failedCount = m.failedCount;
break;
}
});

childFork.process.on('exit', function(code, signal) {
if (code) {
log_('Runner Process Exited With Error Code: ' + code);
launcherExitCode = 1;
}
runner.on('testsDone', function(failedCount) {
driverInstance.failedCount = failedCount;
});
} else {
noLineLog_('Running ' + childForks.length +
noLineLog_('Running ' + driverInstances.length +
' instances of WebDriver');

// Launch each fork and set up listeners
childForks.forEach(function(childFork) {
driverInstances.forEach(function(driverInstance) {

childFork.process = child.fork(
__dirname + "/runFromLauncher.js", process.argv.slice(2),
driverInstance.process = child.fork(
__dirname + "/runFromLauncher.js",
process.argv.slice(2),
{silent: true, cwd: process.cwd()});

childFork.output = '';
driverInstance.output = '';

// stdin pipe
childFork.process.stdout.on('data', function(chunk) {
childFork.output += chunk;
driverInstance.process.stdout.on('data', function(chunk) {
driverInstance.output += chunk;
});

// stderr pipe
childFork.process.stderr.on('data', function(chunk) {
childFork.output += chunk;
driverInstance.process.stderr.on('data', function(chunk) {
driverInstance.output += chunk;
});

childFork.process.on('message', function(m) {
driverInstance.process.on('message', function(m) {
switch (m.event) {
case 'testPass':
process.stdout.write('.');
Expand All @@ -187,41 +170,44 @@ var init = function(configFile, additionalConfig) {
process.stdout.write('F');
break;
case 'testsDone':
childFork.failedCount = m.failedCount;
driverInstance.failedCount = m.failedCount;
break;
}
});

// err handler
childFork.process.on('error', function(err) {
log_('Runner Process(' + childFork.process.pid + ') Error: ' + err);
driverInstance.process.on('error', function(err) {
log_('Runner Process(' + driverInstance.process.pid + ') Error: ' + err);
});

// exit handlers
childFork.process.on('exit', function(code, signal) {
driverInstance.process.on('exit', function(code, signal) {
if (code) {
log_('Runner Process Exited With Error Code: ' + code);
launcherExitCode = 1;
}
reportHeader_(childFork);
util.puts(childFork.output);
childFork.done = true;
reportHeader_(driverInstance);
util.puts(driverInstance.output);
driverInstance.done = true;
listRemainingForks();
});

childFork.process.send({
driverInstance.process.send({
command: 'run',
configFile: childFork.configFile,
additionalConfig: childFork.additionalConfig,
capability: childFork.capability
configFile: driverInstance.configFile,
additionalConfig: driverInstance.additionalConfig,
capability: driverInstance.capability
});
});
}

process.on('exit', function(code) {
logSummary();
process.exit(launcherExitCode);
});
process.on('exit', function(code) {
if (code) {
launcherExitCode = code;
}
logSummary();
process.exit(launcherExitCode);
});
}
};

exports.init = init;
2 changes: 1 addition & 1 deletion spec/multiConf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports.config = {

// Spec patterns are relative to this directory.
specs: [
'basic/*_spec.js'
'basic/lib_spec.js'
],

// Exclude patterns are relative to this directory.
Expand Down

0 comments on commit c9dbbaa

Please sign in to comment.