Permalink
Browse files

Added test timeout support

  • Loading branch information...
1 parent 61d29c1 commit 1f9cc1c2942992f95a2733deb9f4b9b22e23dde3 @davglass committed Apr 25, 2012
Showing with 76 additions and 4 deletions.
  1. +19 −0 bin/grover.js
  2. +9 −0 lib/wrapper.js
  3. +25 −0 tests/html/timeout.html
  4. +23 −4 tests/test.js
View
@@ -21,6 +21,13 @@ var util = require(path.join(__dirname, '../lib/'));
while (args.length > 0) {
var v = args.shift();
switch (v) {
+ case "-t":
+ case "--timeout":
+ var t = parseInt(args.shift());
+ if (!isNaN(t)) {
+ options.timeout = t;
+ }
+ break;
case "-i":
case "--import":
options.import = args.shift();
@@ -48,6 +55,7 @@ while (args.length > 0) {
console.log(' -s, --silent Print no output, only use exit code');
console.log(' -q, --quiet Only print errors and use exit code');
console.log(' -f, --fail Fail on first error');
+ console.log(' -t, --timeout Specify a timeout (in seconds) for a test file to be considered as failed.');
console.log(' -i, --import <path to js file> - Require this file and use the exports (array)');
console.log(' as the list of files to process.');
console.log(' -o, --outfile <path to export file>');
@@ -89,6 +97,10 @@ while (args.length > 0) {
}
}
+if (options.timeout <=0 ) {
+ options.timeout = null;
+}
+
if (options.import) {
if (!path.existsSync(options.import) || path.existsSync(path.join(process.cwd(), options.import))) {
options.import = path.join(process.cwd(), options.import);
@@ -120,7 +132,11 @@ check(function(version) {
if (!options.silent && !options.quiet) {
util.log('Starting Grover on ' + options.paths.length + ' files with PhantomJS@' + version);
+ if (options.timeout) {
+ util.log(' Using a ' + options.timeout + ' second timeout per test.');
+ }
}
+
if (options.exitOnFail) {
util.log('--will exit on first test error');
}
@@ -154,6 +170,9 @@ var run = function() {
process.exit(1);
}
var cmd = 'phantomjs ' + wrapper + ' ' + file;
+ if (options.timeout) {
+ cmd += ' ' + options.timeout;
+ }
exec(cmd, function(err, stdout) {
var results = JSON.parse(stdout);
testResults.push(results);
View
@@ -152,6 +152,15 @@ if (!phantom.args.length) {
}
var file = phantom.args[0];
+var timeout = parseInt(phantom.args[1]);
+var timer;
+
+if (isNaN(timeout)) {
+ timeout = 60; //Default to one minute before failing the test
+}
+timer = setTimeout(function() {
+ throwError('Script Timeout');
+}, (timeout * 1000));
var fs = require('fs');
View
@@ -0,0 +1,25 @@
+<!doctype html>
+<html>
+<head>
+ <title>Test One</title>
+</head>
+<body>
+<script src="http://yui.yahooapis.com/3.5.0/build/yui/yui-min.js"></script>
+<script>
+YUI().use('test', function(Y) {
+ var suite = new Y.Test.Suite('Timeout Test');
+
+ for (var i = 1; i <= 5000; i++) { //Set this high so the timeout can fire
+ suite.add(new Y.Test.Case({
+ name: "Case #" + i,
+ 'test: bogus test - ': function() {
+ Y.Assert.isTrue(true);
+ }
+ }));
+ }
+ Y.Test.Runner.add(suite);
+ Y.Test.Runner.run();
+});
+</script>
+</body>
+</html>
View
@@ -21,8 +21,12 @@ var Assert = YUITest.Assert;
var wrapper = path.join(__dirname, '../lib/wrapper.js');
-var runTest = function(file, cb) {
- var cmd = 'phantomjs ' + wrapper + ' ' + path.join(__dirname, file);
+var runTest = function(file, timeout, cb) {
+ if (!cb) {
+ cb = timeout;
+ timeout = '';
+ }
+ var cmd = 'phantomjs ' + wrapper + ' ' + path.join(__dirname, file) + ' ' + timeout;
//console.log('Executing: ', cmd);
exec(cmd, cb);
};
@@ -66,8 +70,8 @@ suite.add(new YUITest.TestCase({
}));
suite.add(new YUITest.TestCase({
- name: 'Script Error',
- 'Error Tests': function() {
+ name: 'Errors',
+ 'Should throw script error': function() {
var test = this;
runTest('./html/error.html', function(err, stdout) {
@@ -80,6 +84,21 @@ suite.add(new YUITest.TestCase({
});
this.wait();
+ },
+ 'Should Throw Timeout': function() {
+ var test = this;
+
+ runTest('./html/timeout.html', 1, function(err, stdout) {
+ test.resume(function() {
+ var json = JSON.parse(stdout);
+ Assert.areEqual(0, json.passed, 'A test failed');
+ Assert.areEqual(1, json.failed, 'A test failed');
+ Assert.isNotUndefined(json.error, 'Error message was not passed along');
+ Assert.areEqual('Script Timeout', json.error);
+ });
+ });
+
+ this.wait();
}
}));

0 comments on commit 1f9cc1c

Please sign in to comment.