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

Commit c9dbbaa

Browse files
committed
refactor(launcher): skip the child process if only one capability is requested
Closes #603
1 parent ebd5dd7 commit c9dbbaa

File tree

2 files changed

+65
-79
lines changed

2 files changed

+65
-79
lines changed

lib/launcher.js

+64-78
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
var util = require('util'),
88
path = require('path'),
9-
fs = require('fs'),
10-
glob = require('glob'),
119
child = require('child_process'),
1210
ConfigParser = require('./configParser');
1311

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

24-
var reportHeader_ = function(childFork) {
25-
var capability = childFork.capability;
22+
var reportHeader_ = function(driverInstance) {
23+
var capability = driverInstance.capability;
2624
var eol = require('os').EOL;
2725

2826
var outputHeader = eol + '------------------------------------' + eol;
29-
outputHeader += 'PID: ' + childFork.process.pid + ' (capability: ';
27+
outputHeader += 'PID: ' + driverInstance.process.pid + ' (capability: ';
3028
outputHeader += (capability.browserName) ?
3129
capability.browserName : '';
3230
outputHeader += (capability.version) ?
3331
capability.version : '';
3432
outputHeader += (capability.platform) ?
3533
capability.platform : '';
36-
outputHeader += (childFork.runNumber) ?
37-
' #' + childFork.runNumber : '';
34+
outputHeader += (driverInstance.runNumber) ?
35+
' #' + driverInstance.runNumber : '';
3836
outputHeader += ')' + eol;
3937
outputHeader += '------------------------------------' + eol;
4038

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

5351
var capabilityRunCount,
54-
childForks = [],
52+
driverInstances = [],
5553
launcherExitCode = 0;
5654

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

6664
var listRemainingForks = function() {
6765
var remaining = 0;
68-
childForks.forEach(function(childFork) {
69-
if (!childFork.done) {
66+
driverInstances.forEach(function(driverInstance) {
67+
if (!driverInstance.done) {
7068
remaining++;
7169
}
7270
});
@@ -76,11 +74,11 @@ var init = function(configFile, additionalConfig) {
7674
};
7775

7876
var logSummary = function() {
79-
childForks.forEach(function(childFork) {
80-
var shortChildName = childFork.capability.browserName +
81-
(childFork.runNumber ? ' #' + childFork.runNumber : '');
82-
if (childFork.failedCount) {
83-
log_(shortChildName + ' failed ' + childFork.failedCount + ' test(s)');
77+
driverInstances.forEach(function(driverInstance) {
78+
var shortChildName = driverInstance.capability.browserName +
79+
(driverInstance.runNumber ? ' #' + driverInstance.runNumber : '');
80+
if (driverInstance.failedCount) {
81+
log_(shortChildName + ' failed ' + driverInstance.failedCount + ' test(s)');
8482
} else {
8583
log_(shortChildName + ' passed');
8684
}
@@ -95,12 +93,12 @@ var init = function(configFile, additionalConfig) {
9593
'config.capabilities will be ignored');
9694
}
9795

98-
// Merge 'capabilities' and 'multiCapabilities', if applicable.
96+
// Use capabilities if multiCapabilities is empty.
9997
if (!config.multiCapabilities.length) {
10098
config.multiCapabilities = [config.capabilities];
10199
}
102100

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

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

110108
// Fork the child runners.
111109
for (var j = 0; j < capabilityRunCount; j++) {
112-
childForks.push({
110+
driverInstances.push({
113111
configFile: configFile,
114112
additionalConfig: additionalConfig,
115113
capability: config.multiCapabilities[i],
@@ -118,67 +116,52 @@ var init = function(configFile, additionalConfig) {
118116
}
119117
}
120118

121-
// If we're launching multiple runners, aggregate output until completion.
122-
// Otherwise, there is a single runner, let's pipe the output straight
123-
// through to maintain realtime reporting.
124-
if (childForks.length === 1) {
125-
var childFork = childForks[0];
126-
childFork.process = child.fork(
127-
__dirname + "/runFromLauncher.js",
128-
process.argv.slice(2),
129-
{cwd: process.cwd()});
130-
reportHeader_(childFork);
131-
132-
childFork.process.send({
133-
command: 'run',
134-
configFile: childFork.configFile,
135-
additionalConfig: childFork.additionalConfig,
136-
capability: childFork.capability
119+
// If there is a single runner, avoid starting a separate process
120+
// and print output directly.
121+
// Otherwise, if we're launching multiple runners, aggregate output until
122+
// completion.
123+
if (driverInstances.length === 1) {
124+
var driverInstance = driverInstances[0];
125+
126+
var Runner = require('./runner');
127+
config.capabilities = driverInstance.capability;
128+
129+
var runner = new Runner(config);
130+
runner.run().then(function(exitCode) {
131+
process.exit(exitCode);
132+
}).catch(function(err) {
133+
log_('Error: ' + err.message);
134+
process.exit(1);
137135
});
138136

139-
childFork.process.on('error', function(err) {
140-
log_('Runner Process(' + childFork.process.pid + ') Error: ' + err);
141-
});
142-
143-
144-
childFork.process.on('message', function(m) {
145-
switch (m.event) {
146-
case 'testsDone':
147-
childFork.failedCount = m.failedCount;
148-
break;
149-
}
150-
});
151-
152-
childFork.process.on('exit', function(code, signal) {
153-
if (code) {
154-
log_('Runner Process Exited With Error Code: ' + code);
155-
launcherExitCode = 1;
156-
}
137+
runner.on('testsDone', function(failedCount) {
138+
driverInstance.failedCount = failedCount;
157139
});
158140
} else {
159-
noLineLog_('Running ' + childForks.length +
141+
noLineLog_('Running ' + driverInstances.length +
160142
' instances of WebDriver');
161143

162144
// Launch each fork and set up listeners
163-
childForks.forEach(function(childFork) {
145+
driverInstances.forEach(function(driverInstance) {
164146

165-
childFork.process = child.fork(
166-
__dirname + "/runFromLauncher.js", process.argv.slice(2),
147+
driverInstance.process = child.fork(
148+
__dirname + "/runFromLauncher.js",
149+
process.argv.slice(2),
167150
{silent: true, cwd: process.cwd()});
168151

169-
childFork.output = '';
152+
driverInstance.output = '';
170153

171154
// stdin pipe
172-
childFork.process.stdout.on('data', function(chunk) {
173-
childFork.output += chunk;
155+
driverInstance.process.stdout.on('data', function(chunk) {
156+
driverInstance.output += chunk;
174157
});
175158

176159
// stderr pipe
177-
childFork.process.stderr.on('data', function(chunk) {
178-
childFork.output += chunk;
160+
driverInstance.process.stderr.on('data', function(chunk) {
161+
driverInstance.output += chunk;
179162
});
180163

181-
childFork.process.on('message', function(m) {
164+
driverInstance.process.on('message', function(m) {
182165
switch (m.event) {
183166
case 'testPass':
184167
process.stdout.write('.');
@@ -187,41 +170,44 @@ var init = function(configFile, additionalConfig) {
187170
process.stdout.write('F');
188171
break;
189172
case 'testsDone':
190-
childFork.failedCount = m.failedCount;
173+
driverInstance.failedCount = m.failedCount;
191174
break;
192175
}
193176
});
194177

195178
// err handler
196-
childFork.process.on('error', function(err) {
197-
log_('Runner Process(' + childFork.process.pid + ') Error: ' + err);
179+
driverInstance.process.on('error', function(err) {
180+
log_('Runner Process(' + driverInstance.process.pid + ') Error: ' + err);
198181
});
199182

200183
// exit handlers
201-
childFork.process.on('exit', function(code, signal) {
184+
driverInstance.process.on('exit', function(code, signal) {
202185
if (code) {
203186
log_('Runner Process Exited With Error Code: ' + code);
204187
launcherExitCode = 1;
205188
}
206-
reportHeader_(childFork);
207-
util.puts(childFork.output);
208-
childFork.done = true;
189+
reportHeader_(driverInstance);
190+
util.puts(driverInstance.output);
191+
driverInstance.done = true;
209192
listRemainingForks();
210193
});
211194

212-
childFork.process.send({
195+
driverInstance.process.send({
213196
command: 'run',
214-
configFile: childFork.configFile,
215-
additionalConfig: childFork.additionalConfig,
216-
capability: childFork.capability
197+
configFile: driverInstance.configFile,
198+
additionalConfig: driverInstance.additionalConfig,
199+
capability: driverInstance.capability
217200
});
218201
});
219-
}
220202

221-
process.on('exit', function(code) {
222-
logSummary();
223-
process.exit(launcherExitCode);
224-
});
203+
process.on('exit', function(code) {
204+
if (code) {
205+
launcherExitCode = code;
206+
}
207+
logSummary();
208+
process.exit(launcherExitCode);
209+
});
210+
}
225211
};
226212

227213
exports.init = init;

spec/multiConf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports.config = {
44

55
// Spec patterns are relative to this directory.
66
specs: [
7-
'basic/*_spec.js'
7+
'basic/lib_spec.js'
88
],
99

1010
// Exclude patterns are relative to this directory.

0 commit comments

Comments
 (0)