forked from atsuya/parallel-mocha
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
123 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,44 @@ | ||
var EventEmitter = require('events').EventEmitter | ||
, util = require('util') | ||
, DoneCriteria = require('done-criteria') | ||
, underscore = require('underscore'); | ||
'use strict'; | ||
|
||
function Pool(paths, workers) { | ||
EventEmitter.call(this); | ||
const EventEmitter = require('events').EventEmitter; | ||
const DoneCriteria = require('done-criteria'); | ||
const underscore = require('underscore'); | ||
|
||
this.paths = paths; | ||
this.workers = workers; | ||
} | ||
|
||
util.inherits(Pool, EventEmitter); | ||
class Pool extends EventEmitter { | ||
|
||
Pool.prototype.start = function() { | ||
var self = this | ||
, doneCriteria = new DoneCriteria(this.paths, function() { | ||
self.emit('done'); | ||
}); | ||
constructor(paths, workers) { | ||
super(Pool); | ||
this.paths = paths; | ||
this.workers = workers; | ||
} | ||
|
||
self.on('next', function() { | ||
self.next(function(path) { | ||
doneCriteria.done(path); | ||
start() { | ||
let doneCriteria = new DoneCriteria(this.paths, () => { | ||
this.emit('done'); | ||
}); | ||
}); | ||
|
||
|
||
underscore.range(this.workers).forEach(function() { | ||
self.next(function(path) { | ||
doneCriteria.done(path); | ||
self.emit('next'); | ||
this.on('next', ()=> { | ||
this.next((path)=> { | ||
doneCriteria.done(path); | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
Pool.prototype.next = function(callback) { | ||
var path = this.paths.shift(); | ||
if (path) { | ||
this.emit('ready', path, function() { | ||
return callback(path); | ||
|
||
underscore.range(this.workers).forEach(()=> { | ||
this.next((path)=> { | ||
doneCriteria.done(path); | ||
this.emit('next'); | ||
}); | ||
}); | ||
}; | ||
|
||
next(callback) { | ||
let path = this.paths.shift(); | ||
if (path) { | ||
this.emit('ready', path, ()=> { | ||
return callback(path); | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
module.exports = Pool; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,73 @@ | ||
var spawn = require('child_process').spawn | ||
var fs = require('fs') | ||
var underscore = require('underscore'); | ||
'use strict'; | ||
|
||
var Pool = require('./pool'); | ||
const spawn = require('child_process').spawn; | ||
const fs = require('fs'); | ||
const underscore = require('underscore'); | ||
|
||
function Runner(paths, config) { | ||
const Pool = require('./pool'); | ||
|
||
class Runner { | ||
|
||
constructor(paths, config) { | ||
this.paths = paths; | ||
this.config = underscore.extend( | ||
{ | ||
bin: ['./node_modules/.bin/mocha'] | ||
, processes: 2 | ||
} | ||
, config | ||
{ | ||
bin: ['./node_modules/.bin/mocha'] | ||
, processes: 2 | ||
} | ||
, config | ||
); | ||
this.mocha = null; | ||
this.exitCodes = []; | ||
} | ||
} | ||
|
||
Runner.prototype.run = function (callback) { | ||
var self = this | ||
, pool = new Pool(self.paths, self.config.processes); | ||
run(callback) { | ||
let pool = new Pool(this.paths, this.config.processes); | ||
let exitCodes = []; | ||
|
||
pool.on('done', function () { | ||
var success = underscore.every(self.exitCodes, function (code) { | ||
return code === 0; | ||
}); | ||
// TODO: return better result rather than just error or not | ||
var error = success ? null : new Error('Not all tests passed'); | ||
return callback(error); | ||
}); | ||
pool.on('ready', (path, callback) => { | ||
var args = { | ||
path: path, | ||
timeout: this.config.timeout, | ||
slow: this.config.slow, | ||
env: this.config.env | ||
}; | ||
|
||
pool.on('ready', function (path, callback) { | ||
var args = { | ||
path: path, | ||
timeout: self.config.timeout, | ||
slow: self.config.slow | ||
}; | ||
this.spawn(args, (code)=> { | ||
exitCodes.push(code); | ||
callback(); | ||
}); | ||
}); | ||
|
||
self.spawn(args, callback); | ||
pool.on('done', () => { | ||
var success = underscore.every(this.exitCodes, code=>code === 0); | ||
var error = success ? null : new Error('Not all tests passed'); | ||
return callback(error); | ||
}); | ||
|
||
pool.start(); | ||
}; | ||
} | ||
|
||
Runner.prototype.spawn = function (args, callback) { | ||
var self = this | ||
, mocha = self.findMocha() | ||
, child = spawn(mocha, [ | ||
spawn(args, callback) { | ||
let mocha = this.findMocha(); | ||
let child = spawn(mocha, [ | ||
'--timeout', args.timeout, | ||
'--slow', args.timeout, | ||
args.path | ||
], {stdio: "inherit", env: underscore.extend(process.env, {NODE_ENV: 'test'})}); | ||
|
||
/*child.stdout.once('data', function (data) { | ||
process.stdout.write('.'); | ||
self.exitCodes.push(0); | ||
return callback(null); | ||
}); | ||
child.stderr.on('data', function (data) { | ||
process.stdout.write('%s', data); | ||
process.exit(); | ||
});*/ | ||
|
||
child.on('exit', function (code) { | ||
|
||
}); | ||
], | ||
{stdio: "inherit", env: underscore.extend(process.env, {NODE_ENV: args.env})} | ||
); | ||
|
||
}; | ||
child.on('exit', callback); | ||
} | ||
|
||
Runner.prototype.findMocha = function () { | ||
findMocha() { | ||
if (!this.mocha) { | ||
this.mocha = underscore.find(this.config.bin, function (bin) { | ||
return fs.existsSync(bin); | ||
}); | ||
this.mocha = this.mocha || 'mocha'; | ||
this.mocha = underscore.find(this.config.bin, bin =>fs.existsSync(bin)); | ||
this.mocha = this.mocha || 'mocha'; | ||
} | ||
|
||
return this.mocha; | ||
}; | ||
} | ||
} | ||
|
||
module.exports = Runner; |