Skip to content

Commit

Permalink
added a unit test for parallel execution
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Sep 23, 2014
1 parent 77026a4 commit de977d5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 28 deletions.
29 changes: 19 additions & 10 deletions bin/_clirunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ function CliRunner(argv) {
}

CliRunner.prototype = {
init : function() {
/**
* @param {function} [done]
* @returns {CliRunner}
*/
init : function(done) {
this
.readSettings()
.setOutputFolder()
.parseTestSettings();
.parseTestSettings(done);

return this;
},
Expand Down Expand Up @@ -254,7 +258,7 @@ CliRunner.prototype = {
* Starts the test runner
* @returns {CliRunner}
*/
runTests : function(callback) {
runTests : function() {
if (this.parallelMode) {
return this;
}
Expand All @@ -274,7 +278,6 @@ CliRunner.prototype = {
var context = self.test_settings && self.test_settings.globals || null;
afterGlobal.call(context, function() {
self.globalErrorHandler(err);
callback();
});

});
Expand All @@ -299,7 +302,7 @@ CliRunner.prototype = {
* Validates and parses the test settings
* @returns {CliRunner}
*/
parseTestSettings : function() {
parseTestSettings : function(callback) {
// checking if the env passed is valid
if (!this.settings.test_settings) {
throw new Error('No testing environment specified.');
Expand All @@ -313,7 +316,7 @@ CliRunner.prototype = {
}

if (envs.length > 1) {
this.setupParallelMode(envs);
this.setupParallelMode(envs, callback);
return this;
}

Expand Down Expand Up @@ -418,16 +421,22 @@ CliRunner.prototype = {
/**
* Enables parallel execution mode
* @param {Array} envs
* @param {function} [callback]
* @returns {CliRunner}
*/
setupParallelMode : function(envs) {
setupParallelMode : function(envs, callback) {
this.parallelMode = true;
var self = this;

this.startSelenium(function() {
self.startChildProcesses(envs, function(o, code) {
self.stopSelenium();
process.exit(code);
if (callback) {
callback();
}
if (code) {
process.exit(code);
}
});
});

Expand Down Expand Up @@ -519,7 +528,7 @@ CliRunner.prototype = {

envs.forEach(function(item, index) {
var cliArgs = self.getChildProcessArgs(mainModule);
cliArgs.push('-e', item, '__parallel-mode');
cliArgs.push('-e', item, '--parallel-mode');
var env = process.env;

setTimeout(function() {
Expand Down Expand Up @@ -555,7 +564,7 @@ CliRunner.prototype = {
globalExitCode = 2;
}
if (!self.settings.live_output) {
var child_output = output[item];
var child_output = output[item] || '';
for (var i = 0; i < child_output.length; i++) {
process.stdout.write(child_output[i]);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/extra/globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {

before : function() {
console.log('before')
},

after : function() {
console.log('after')
}
};
2 changes: 1 addition & 1 deletion tests/extra/nightwatch.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"output_folder" : "./output",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"globals_path" : "",
"globals_path" : "./globals.js",

"selenium" : {
"start_process" : false,
Expand Down
15 changes: 7 additions & 8 deletions tests/run_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ try {
var server = require('mockserver').init();
server.on('listening', function() {
reporter.run([
//'src',
//'src/index',
'src/cli',
// 'src/runner',
// 'src/assertions',
// 'src/commands',
// 'src/protocol',
// 'src/http'
'src',
'src/index',
'src/runner',
'src/assertions',
'src/commands',
'src/protocol',
'src/http'
], options, function(err) {
server.close();
if (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var events = require('events');
var mockery = require('mockery');
module.exports = {
setUp: function(callback) {
mockery.enable({ useCleanCache: true, warnOnUnregistered: true });
mockery.enable({ useCleanCache: true, warnOnUnregistered: false });
callback();
},

Expand All @@ -30,6 +30,10 @@ module.exports = {
var Child = function() {
this.stdout = new Stdout();
this.stderr = new Stderr();
setTimeout(function() {
this.emit('exit');
this.emit('close');
}.bind(this), 11);
};

util.inherits(Child, events.EventEmitter);
Expand All @@ -38,22 +42,23 @@ module.exports = {
});

mockery.registerMock('../lib/runner/run.js', {
run : function(source, settings, opts, callback) {
console.log('Run', source, settings)
//callback();
}
run : function(source, settings, opts, callback) {}
});

var CliRunner = require('../../../' + BASE_PATH + '/../bin/_clirunner.js');
var runner = new CliRunner({
c : './extra/nightwatch.json',
e : 'default,mixed'
}).init();
});

runner.runTests(function() {
console.log(allArgs);
console.log(allOpts);
runner.init(function() {
test.ok(runner.isParallelMode());
test.equals(allArgs.length, 2);
test.equals(allArgs[0][2], 'default');
test.equals(allArgs[1][2], 'mixed');
test.done();
});

test.ok(runner.parallelMode);
}
};

0 comments on commit de977d5

Please sign in to comment.