Skip to content

Commit ea8c5a4

Browse files
committed
Add batch mode, and a jsshell runner that can use it
1 parent ab0af63 commit ea8c5a4

File tree

3 files changed

+152
-6
lines changed

3 files changed

+152
-6
lines changed

bin/run.js

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var args = require('minimist')(process.argv.slice(2), {
2424
runner: 'r',
2525
reporter: 'R',
2626
threads: 't',
27+
batch: 'b'
2728
}
2829
});
2930

@@ -42,11 +43,53 @@ try {
4243
}
4344

4445
var runner = new Runner(args);
46+
47+
// Run tests
4548
var results = parser
46-
.pipe(through(flatMap(scenarios(args))))
47-
.pipe(throughThreaded(function(test, done) {
48-
runner.run(test, done);
49-
}, args.threads));
49+
.pipe(through(flatMap(scenarios(args))));
50+
51+
if(args.batch) {
52+
results = results
53+
.pipe(batch(args.batch))
54+
.pipe(throughThreaded(runBatch, args.threads));
55+
} else {
56+
results = results
57+
.pipe(throughThreaded(runTest, args.threads));
58+
}
59+
60+
function batch(size) {
61+
var currentBatch = [];
62+
63+
return through(function(data) {
64+
currentBatch.push(data);
65+
66+
if(currentBatch.length === size) {
67+
this.queue(currentBatch);
68+
currentBatch = [];
69+
}
70+
}, function(done) {
71+
this.queue(currentBatch);
72+
this.queue(null);
73+
});
74+
}
75+
76+
function runTest(test, done) {
77+
var stream = this;
78+
runner.run(test, function() {
79+
stream.queue(test);
80+
done();
81+
});
82+
}
83+
84+
function runBatch(batch, done) {
85+
var stream = this;
86+
runner.runBatch(batch, function() {
87+
batch.forEach(stream.queue);
88+
done();
89+
});
90+
}
91+
92+
5093

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

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

111-
cb(data, function() {
112-
that.queue(data);
154+
cb.call(this, data, function() {
113155
pending--;
114156
if(done && pending === 0) that.queue(null);
115157
if(pending < threads && that.paused) that.resume();

lib/runner.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ Runner.prototype.validateResult = function(test, result) {
140140
}
141141
}
142142

143+
Runner.prototype.runBatch = function(batch, done) {
144+
batch.forEach(function(test) {
145+
this.compile(test);
146+
}, this);
147+
148+
this.executeBatch(batch, done);
149+
}
150+
143151
Runner.prototype.run = function(test, done) {
144152
this.compile(test);
145153
this.execute(test, done);

lib/runners/jsshell.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (C) 2014, Microsoft Corporation. All rights reserved.
2+
// This code is governed by the BSD License found in the LICENSE file.
3+
4+
module.exports = JSShellRunner;
5+
6+
var fs = require('fs');
7+
var cp = require('child_process');
8+
var through = require('through');
9+
var ConsoleRunner = require('./console');
10+
var DEFAULT_BATCH_SIZE = 75;
11+
var counter = 0;
12+
13+
function lineSplitter() {
14+
var splitLines = through(function(data) {
15+
this.buffer += data;
16+
var lines = this.buffer.split(/\r?\n/);
17+
this.buffer = lines.pop();
18+
19+
lines.forEach(function(line) { this.queue(line)}, this);
20+
}, function() {
21+
this.queue(this.buffer);
22+
})
23+
24+
splitLines.buffer = "";
25+
26+
return splitLines;
27+
}
28+
29+
function JSShellRunner(args) {
30+
args.consolePrintCommand = args.consolePrintCommand || "print";
31+
32+
ConsoleRunner.apply(this, arguments);
33+
34+
if(args.batch === true) args.batch = DEFAULT_BATCH_SIZE;
35+
36+
if(args.batch) {
37+
this.batchTestStart =
38+
'var env = evalcx("");\n' +
39+
'env.print = print;' +
40+
'try {\n';
41+
42+
this.batchTestEnd =
43+
'} catch(e) {\n' +
44+
'if(e.hasOwnProperty("name")) ' +
45+
this._print('"test262/error " + e.name + " " + e.message') +
46+
'else ' + this._print('"test262/error " + e') +
47+
'}\n';
48+
}
49+
50+
}
51+
52+
JSShellRunner.prototype = Object.create(ConsoleRunner.prototype);
53+
54+
JSShellRunner.prototype.executeBatch = function(batch, batchDone) {
55+
var runner = this;
56+
var baseFile = '__tmp' + counter++ + '_';
57+
var script = '';
58+
59+
// write test files to disk
60+
batch.forEach(function(test, i) {
61+
script += this._print('"test262/test-start"')
62+
+ this.batchTestStart
63+
+ 'evalcx(' + JSON.stringify(test.contents) + ', env);\n'
64+
+ this.batchTestEnd
65+
+ this._print('"test262/test-end"');
66+
}, this);
67+
68+
var scriptFile = baseFile + 'main.js';
69+
70+
fs.writeFileSync(scriptFile, script);
71+
72+
cp.exec(this.command + " " + scriptFile, function(err, stdout, stderr) {
73+
var results = { log: [] };
74+
var lines = stdout.split(/\r?\n/);
75+
var index = 0;
76+
77+
lines.forEach(function(line) {
78+
switch(line) {
79+
case 'test262/test-start':
80+
break;
81+
case 'test262/test-end':
82+
var test = batch[index++];
83+
runner.validateResult(test, results);
84+
results = { log: [] };
85+
86+
break;
87+
default:
88+
results.log.push(line);
89+
}
90+
})
91+
92+
fs.unlink(scriptFile);
93+
batchDone();
94+
});
95+
}
96+

0 commit comments

Comments
 (0)