From a85db4cd98f8353a792615a7d014c0f39371baa1 Mon Sep 17 00:00:00 2001 From: Kasidit Iamthong Date: Sun, 18 Sep 2016 06:55:04 +0700 Subject: [PATCH] fix: run all timers in passage of time in a single fakeAsync's tick call Closes #454 --- lib/zone-spec/fake-async-test.ts | 8 +++++--- test/zone-spec/fake-async-test.spec.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/zone-spec/fake-async-test.ts b/lib/zone-spec/fake-async-test.ts index 9d2ce990e..3add4a68d 100644 --- a/lib/zone-spec/fake-async-test.ts +++ b/lib/zone-spec/fake-async-test.ts @@ -51,15 +51,16 @@ } 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. @@ -67,6 +68,7 @@ } } } + this._currentTime = finalTime; } } diff --git a/test/zone-spec/fake-async-test.spec.ts b/test/zone-spec/fake-async-test.spec.ts index 12639fb4f..f6fa38407 100644 --- a/test/zone-spec/fake-async-test.spec.ts +++ b/test/zone-spec/fake-async-test.spec.ts @@ -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; @@ -160,6 +183,9 @@ describe('FakeAsyncTestZoneSpec', () => { testZoneSpec.tick(10); expect(cycles).toEqual(3); + + testZoneSpec.tick(30); + expect(cycles).toEqual(6); }); });