Skip to content

Commit

Permalink
Make now configurable so time can freeze during backburner run loop
Browse files Browse the repository at this point in the history
This allow us to use fake timers, such as `sinon.useFakeTimers`, to
check the state of the app during the run loop.

For example: if a popup is supposed to be triggered in X ms from now and
it will show to the user during Y ms, then this will allow us to use
fake timers to pause between X and Y and check the state of the Ember
app at that exact time.

This is related with #263, #179 and #166.

If someone needs to have the backburner clock separate from the global
stubbed clock (as explained in #179), then `_platform.now` config should
be used to pass the native `Date.now` function.
  • Loading branch information
alias-mac committed Sep 21, 2017
1 parent 703a4cf commit a6fe19e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class Backburner {
platform.clearTimeout = _platform.clearTimeout || ((id) => clearTimeout(id));
platform.next = _platform.next || ((fn) => platform.setTimeout(fn, 0));
platform.clearNext = _platform.clearNext || platform.clearTimeout;
platform.now = _platform.now || Date.now;
platform.now = _platform.now || (() => Date.now());

this._platform = platform;

Expand Down
11 changes: 7 additions & 4 deletions tests/set-timeout-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,20 @@ QUnit.test('later', function(assert) {
}, 20);
});

QUnit.test('later can continue when `Date.now` is monkey-patched', function(assert) {
QUnit.test('later should rely on stubbed `Date.now`', function(assert) {
assert.expect(1);

let arbitraryTime = +new Date();
let bb = new Backburner(['one']);
let done = assert.async();
let globalNowWasUsed = false;

Date.now = function() { return arbitraryTime; };
Date.now = function() {
globalNowWasUsed = true;
return originalDateNow();
};

bb.later(() => {
assert.ok(true);
assert.ok(globalNowWasUsed);
done();
}, 1);
});
Expand Down

0 comments on commit a6fe19e

Please sign in to comment.