Skip to content
This repository has been archived by the owner on Sep 19, 2018. It is now read-only.

Commit

Permalink
fixup! Add after_timeout function
Browse files Browse the repository at this point in the history
  • Loading branch information
jgraham committed May 8, 2015
1 parent c73c853 commit fd3d4b8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
27 changes: 27 additions & 0 deletions docs/api.md
Expand Up @@ -243,6 +243,33 @@ the test result is known. For example
assert_true(false);
});

## Timeouts in Tests ##

In general the use of timeouts in tests is discouraged because this is
an observed source of instability in real tests when run on CI
infrastructure. In particular if a test should fail when something
doesn't happen it is good practice to simply let the test run to the
full timeout rather than trying to guess an appropriate shorter
timeout to use.

In other cases it may be necessary to use a timeout (e.g. for a test
that only passes if some event is *not* fired. In this case it is
*not* permitted to use the standard `setTimeout` function. Instead one
must use the `step_timeout` function:

var t = async_test("Some test that does something after a timeout");

t.step_timeout(function() {assert_true(true); this.done()}, 2000);

The difference between `setTimeout` and `step_timeout` is that the
latter takes account of the timeout multiplier when computing the
delay e.g. in the above case a timeout multiplier of 2 would cause a
pause of 4000ms before calling the callback. This makes it less likely
to produce unstable results in slow configurations.

For single-page tests, `step_timeout` is also available as a global
function.

## Harness Timeout ##

The overall harness admits two timeout values `"normal"` (the
Expand Down
12 changes: 10 additions & 2 deletions testharness.js
Expand Up @@ -603,7 +603,7 @@ policies and contribution forms [3].
object.addEventListener(event, callback, false);
}

function after_timeout(f, t) {
function step_timeout(f, t) {
return setTimeout(f, t * tests.timeout_multiplier);
}

Expand All @@ -615,7 +615,7 @@ policies and contribution forms [3].
expose(setup, 'setup');
expose(done, 'done');
expose(on_event, 'on_event');
expose(after_timeout, 'after_timeout');
expose(step_timeout, 'step_timeout');

/*
* Return a string truncated to the given length, with ... added at the end
Expand Down Expand Up @@ -1372,6 +1372,14 @@ policies and contribution forms [3].
});
};

Test.prototype.step_timeout = function(f, timeout) {
var test_this = this;

return setTimeout(this.step_func(function() {
return f.call(test_this);
}, timeout * tests.timeout_multiplier));
}

Test.prototype.add_cleanup = function(callback) {
this.cleanup_callbacks.push(callback);
};
Expand Down

0 comments on commit fd3d4b8

Please sign in to comment.