Skip to content

Commit

Permalink
Add batch mode, and a jsshell runner that can use it
Browse files Browse the repository at this point in the history
  • Loading branch information
bterlson committed Oct 17, 2014
1 parent ab0af63 commit ea8c5a4
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 6 deletions.
54 changes: 48 additions & 6 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var args = require('minimist')(process.argv.slice(2), {
runner: 'r',
reporter: 'R',
threads: 't',
batch: 'b'
}
});

Expand All @@ -42,11 +43,53 @@ try {
}

var runner = new Runner(args);

// Run tests
var results = parser
.pipe(through(flatMap(scenarios(args))))
.pipe(throughThreaded(function(test, done) {
runner.run(test, done);
}, args.threads));
.pipe(through(flatMap(scenarios(args))));

if(args.batch) {
results = results
.pipe(batch(args.batch))
.pipe(throughThreaded(runBatch, args.threads));
} else {
results = results
.pipe(throughThreaded(runTest, args.threads));
}

function batch(size) {
var currentBatch = [];

return through(function(data) {
currentBatch.push(data);

if(currentBatch.length === size) {
this.queue(currentBatch);
currentBatch = [];
}
}, function(done) {
this.queue(currentBatch);
this.queue(null);
});
}

function runTest(test, done) {
var stream = this;
runner.run(test, function() {
stream.queue(test);
done();
});
}

function runBatch(batch, done) {
var stream = this;
runner.runBatch(batch, function() {
batch.forEach(stream.queue);
done();
});
}



if(args.reporter === 'json') {
results.pipe(jss).pipe(process.stdout);
Expand Down Expand Up @@ -108,8 +151,7 @@ function throughThreaded(cb, threads) {

if(pending >= threads) that.pause();

cb(data, function() {
that.queue(data);
cb.call(this, data, function() {
pending--;
if(done && pending === 0) that.queue(null);
if(pending < threads && that.paused) that.resume();
Expand Down
8 changes: 8 additions & 0 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ Runner.prototype.validateResult = function(test, result) {
}
}

Runner.prototype.runBatch = function(batch, done) {
batch.forEach(function(test) {
this.compile(test);
}, this);

this.executeBatch(batch, done);
}

Runner.prototype.run = function(test, done) {
this.compile(test);
this.execute(test, done);
Expand Down
96 changes: 96 additions & 0 deletions lib/runners/jsshell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright (C) 2014, Microsoft Corporation. All rights reserved.
// This code is governed by the BSD License found in the LICENSE file.

module.exports = JSShellRunner;

var fs = require('fs');
var cp = require('child_process');
var through = require('through');
var ConsoleRunner = require('./console');
var DEFAULT_BATCH_SIZE = 75;
var counter = 0;

function lineSplitter() {
var splitLines = through(function(data) {
this.buffer += data;
var lines = this.buffer.split(/\r?\n/);
this.buffer = lines.pop();

lines.forEach(function(line) { this.queue(line)}, this);
}, function() {
this.queue(this.buffer);
})

splitLines.buffer = "";

return splitLines;
}

function JSShellRunner(args) {
args.consolePrintCommand = args.consolePrintCommand || "print";

ConsoleRunner.apply(this, arguments);

if(args.batch === true) args.batch = DEFAULT_BATCH_SIZE;

if(args.batch) {
this.batchTestStart =
'var env = evalcx("");\n' +
'env.print = print;' +
'try {\n';

this.batchTestEnd =
'} catch(e) {\n' +
'if(e.hasOwnProperty("name")) ' +
this._print('"test262/error " + e.name + " " + e.message') +
'else ' + this._print('"test262/error " + e') +
'}\n';
}

}

JSShellRunner.prototype = Object.create(ConsoleRunner.prototype);

JSShellRunner.prototype.executeBatch = function(batch, batchDone) {
var runner = this;
var baseFile = '__tmp' + counter++ + '_';
var script = '';

// write test files to disk
batch.forEach(function(test, i) {
script += this._print('"test262/test-start"')
+ this.batchTestStart
+ 'evalcx(' + JSON.stringify(test.contents) + ', env);\n'
+ this.batchTestEnd
+ this._print('"test262/test-end"');
}, this);

var scriptFile = baseFile + 'main.js';

fs.writeFileSync(scriptFile, script);

cp.exec(this.command + " " + scriptFile, function(err, stdout, stderr) {
var results = { log: [] };
var lines = stdout.split(/\r?\n/);
var index = 0;

lines.forEach(function(line) {
switch(line) {
case 'test262/test-start':
break;
case 'test262/test-end':
var test = batch[index++];
runner.validateResult(test, results);
results = { log: [] };

break;
default:
results.log.push(line);
}
})

fs.unlink(scriptFile);
batchDone();
});
}

0 comments on commit ea8c5a4

Please sign in to comment.