Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix: run all timers in passage of time in a single fakeAsync's tick call
Browse files Browse the repository at this point in the history
Closes #454
  • Loading branch information
kasiditi authored and mhevery committed Oct 1, 2016
1 parent b52cf02 commit a85db4c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/zone-spec/fake-async-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,24 @@
}

tick(millis: number = 0): void {
this._currentTime += millis;
while (this._schedulerQueue.length > 0) {
let finalTime = this._currentTime + millis;
while (this._schedulerQueue.length > 0) {
let current = this._schedulerQueue[0];
if (this._currentTime < current.endTime) {
if (finalTime < current.endTime) {
// Done processing the queue since it's sorted by endTime.
break;
} else {
// Time to run scheduled function. Remove it from the head of queue.
let current = this._schedulerQueue.shift();
this._currentTime = current.endTime;
let retval = current.func.apply(global, current.args);
if (!retval) {
// Uncaught exception in the current scheduled function. Stop processing the queue.
break;
}
}
}
this._currentTime = finalTime;
}
}

Expand Down
26 changes: 26 additions & 0 deletions test/zone-spec/fake-async-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,29 @@ describe('FakeAsyncTestZoneSpec', () => {
});
});

it('should run queued timer created by timer callback', () => {
fakeAsyncTestZone.run(() => {
let counter = 0;
const startCounterLoop = () => {
counter++;
setTimeout(startCounterLoop, 10);
}

startCounterLoop();

expect(counter).toEqual(1);

testZoneSpec.tick(10);
expect(counter).toEqual(2);

testZoneSpec.tick(10);
expect(counter).toEqual(3);

testZoneSpec.tick(30);
expect(counter).toEqual(6);
});
});

it('should run queued timer only once', () => {
fakeAsyncTestZone.run(() => {
let cycles = 0;
Expand Down Expand Up @@ -160,6 +183,9 @@ describe('FakeAsyncTestZoneSpec', () => {

testZoneSpec.tick(10);
expect(cycles).toEqual(3);

testZoneSpec.tick(30);
expect(cycles).toEqual(6);
});
});

Expand Down

0 comments on commit a85db4c

Please sign in to comment.