This repository was archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathlauncher.js
323 lines (293 loc) · 8.66 KB
/
launcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
* The launcher is responsible for parsing the capabilities from the
* input configuration and launching test runners.
*/
'use strict';
var child = require('child_process'),
ConfigParser = require('./configParser'),
TaskScheduler = require('./taskScheduler');
var launcherPrefix = '[launcher] ';
var RUNNERS_FAILED_EXIT_CODE = 100;
var log_ = function(stuff) {
console.log(launcherPrefix + stuff);
};
/**
* Initialize and run the tests.
*
* @param {string=} configFile
* @param {Object=} additionalConfig
*/
var init = function(configFile, additionalConfig) {
var runnerErrorCount = 0;
var configParser = new ConfigParser();
if (configFile) {
configParser.addFileConfig(configFile);
}
if (additionalConfig) {
configParser.addConfig(additionalConfig);
}
var config = configParser.getConfig();
var scheduler = new TaskScheduler(config);
/**
* A fork of a runner for running a specified task. The RunnerFork will
* start a new process that calls on '/runFromLauncher.js' and report the
* result to a reporter.
*
* @constructor
* @param {object} task Task to run.
*/
var RunnerFork = function(task) {
this.capability = task.capability;
this.specs = task.specs;
this.process = child.fork(
__dirname + '/runFromLauncher.js',
process.argv.slice(2), {
cwd: process.cwd(),
silent: true
}
);
this.reporter = reporter.addTaskReporter(task, this.process.pid);
};
/**
* Add handlers for the RunnerFork for events like stdout, stderr, testsDone,
* testPass, testFail, error, and exit. Optionally, you can pass in a
* callback function to be called when a test completes.
*
* @param {function()} testsDoneCallback Callback function for testsDone events.
*/
RunnerFork.prototype.addEventHandlers = function(testsDoneCallback) {
var self = this;
// stdout pipe
this.process.stdout.on('data', function(chunk) {
self.reporter.logStdout(chunk);
});
// stderr pipe
this.process.stderr.on('data', function(chunk) {
self.reporter.logStderr(chunk);
});
this.process.on('message', function(m) {
switch (m.event) {
case 'testPass':
process.stdout.write('.');
break;
case 'testFail':
process.stdout.write('F');
break;
case 'testsDone':
self.reporter.testsDone(m.failedCount);
if (typeof testsDoneCallback === 'function') {
testsDoneCallback();
}
break;
}
});
this.process.on('error', function(err) {
self.reporter.flush();
log_('Runner Process(' + self.process.pid + ') Error: ' + err);
runnerErrorCount += 1;
});
this.process.on('exit', function(code) {
self.reporter.flush();
if (code) {
log_('Runner Process Exited With Error Code: ' + code);
runnerErrorCount += 1;
}
log_(scheduler.countActiveTasks() +
' instance(s) of WebDriver still running');
});
};
/**
* Sends the run command.
*/
RunnerFork.prototype.run = function() {
this.process.send({
command: 'run',
configFile: configFile,
additionalConfig: additionalConfig,
capability: this.capability,
specs: this.specs
});
this.reporter.reportHeader_();
};
// Don't start new process if there is only 1 task.
var totalTasks = scheduler.numTasksRemaining();
if (totalTasks === 1) {
var Runner = require('./runner');
var task = scheduler.nextTask();
config.capabilities = task.capability;
config.specs = task.specs;
var runner = new Runner(config);
runner.run().then(function(exitCode) {
process.exit(exitCode);
}).catch(function(err) {
log_('Error: ' + err.message);
process.exit(1);
});
} else {
if (config.debug) {
throw new Error('Cannot run in debug mode with ' +
'multiCapabilities, count > 1, or sharding');
}
for (var i = 0; i < scheduler.maxConcurrentTasks(); ++i) {
var createNextRunnerFork = function() {
var task = scheduler.nextTask();
if (task) {
var done = function() {
task.done();
createNextRunnerFork();
};
var runnerFork = new RunnerFork(task);
runnerFork.addEventHandlers(done);
runnerFork.run();
}
};
createNextRunnerFork();
}
log_('Running ' + scheduler.countActiveTasks() + ' instances of WebDriver');
process.on('exit', function(code) {
if (code) {
log_('Process exited with error code ' + code);
process.exit(code);
} else if (runnerErrorCount > 0) {
reporter.reportSummary();
process.exit(RUNNERS_FAILED_EXIT_CODE);
} else {
if (scheduler.numTasksRemaining() > 0) {
throw new Error('BUG: launcher exited with ' +
scheduler.numTasksRemaining() + ' tasks remaining');
}
reporter.reportSummary();
process.exit(0);
}
});
}
};
//###### REPORTER #######//
/**
* Keeps track of a list of task reporters. Provides method to add a new
* reporter and to aggregate the reports into a summary.
*/
var reporter = {
taskReporters_: [],
addTaskReporter: function(task, pid) {
var taskReporter = new TaskReporter_(task, pid);
this.taskReporters_.push(taskReporter);
return taskReporter;
},
reportSummary: function() {
var totalFailures = 0;
this.taskReporters_.forEach(function(taskReporter) {
var capability = taskReporter.task.capability;
var shortName = (capability.browserName) ? capability.browserName : '';
shortName += (capability.version) ? capability.version : '';
shortName += (' #' + taskReporter.task.taskId);
if (taskReporter.failedCount) {
log_(shortName + ' failed ' + taskReporter.failedCount + ' test(s)');
totalFailures += taskReporter.failedCount;
} else {
log_(shortName + ' passed');
}
});
if (this.taskReporters_.length > 1 && totalFailures) {
log_('overall: ' + totalFailures + ' failure(s)');
}
}
};
/**
* A reporter for a specific task.
*
* @constructor
* @param {object} task Task that is being reported.
* @param {number} pid PID of process running the task.
*/
var TaskReporter_ = function(task, pid) {
this.task = task;
this.pid = pid;
this.failedCount = 0;
this.buffer = '';
this.insertTag = true;
};
/**
* Report the header for the current task including information such as
* PID, browser name/version, task Id, specs being run.
*/
TaskReporter_.prototype.reportHeader_ = function() {
var eol = require('os').EOL;
var output = 'PID: ' + this.pid + eol;
if (this.task.specs.length === 1) {
output += 'Specs: '+ this.task.specs.toString() + eol + eol;
}
this.log_(output);
};
/**
* Log the stdout. The reporter is responsible for reporting this data when
* appropriate.
*
* @param {string} stdout Stdout data to log
*/
TaskReporter_.prototype.logStdout = function(stdout) {
this.log_(stdout);
};
/**
* Log the stderr. The reporter is responsible for reporting this data when
* appropriate.
*
* @param {string} stderr Stderr data to log
*/
TaskReporter_.prototype.logStderr = function(stderr) {
this.log_(stderr);
};
/**
* Signal that the task is completed. This must be called at the end of a task.
*
* @param {number} failedCount Number of failures
*/
TaskReporter_.prototype.testsDone = function(failedCount) {
this.failedCount = failedCount;
this.flush();
};
/**
* Flushes the buffer to stdout.
*/
TaskReporter_.prototype.flush = function() {
if (this.buffer) {
// Flush buffer if nonempty
var eol = require('os').EOL;
process.stdout.write(eol + '------------------------------------' + eol);
process.stdout.write(this.buffer);
process.stdout.write(eol);
this.buffer = '';
}
};
/**
* Report the following data. The data will be saved to a buffer
* until it is flushed by the function testsDone.
*
* @private
* @param {string} data
*/
TaskReporter_.prototype.log_ = function(data) {
var tag = '[';
var capability = this.task.capability;
tag += (capability.browserName) ?
capability.browserName : '';
tag += (capability.version) ?
(' ' + capability.version) : '';
tag += (capability.platform) ?
(' ' + capability.platform) : '';
tag += (' #' + this.task.taskId);
tag += '] ';
data = data.toString();
for ( var i = 0; i < data.length; i++ ) {
if (this.insertTag) {
this.insertTag = false;
this.buffer += tag;
}
if (data[i] === '\n') {
this.insertTag = true;
this.buffer += '\x1B[0m'; // Prevent color from leaking into next line
}
this.buffer += data[i];
}
};
exports.init = init;