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

Commit

Permalink
fix: make fakeAsync test spec timer id global (#1207)
Browse files Browse the repository at this point in the history
There is a possibility that a single test gets timer id-s from two
separate fakeAsync test zones and the id-s could clash.

This changes makes it such that the next timer id is global and the can
never clash.
  • Loading branch information
vikerman authored Mar 21, 2019
1 parent 2a8415d commit d32e79b
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/zone-spec/fake-async-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const timers = {

class Scheduler {
// Next scheduler id.
public nextId: number = 1;
public static nextId: number = 1;

// Scheduler queue with the tuple of end time and callback function - sorted by end time.
private _schedulerQueue: ScheduledFunction[] = [];
Expand All @@ -90,7 +90,7 @@ class Scheduler {
scheduleFunction(
cb: Function, delay: number, args: any[] = [], isPeriodic: boolean = false,
isRequestAnimationFrame: boolean = false, id: number = -1): number {
let currentId: number = id < 0 ? this.nextId++ : id;
let currentId: number = id < 0 ? Scheduler.nextId++ : id;
let endTime = this._currentTime + delay;

// Insert so that scheduler queue remains sorted by end time.
Expand Down Expand Up @@ -292,7 +292,7 @@ class FakeAsyncTestZoneSpec implements ZoneSpec {
}

private _setTimeout(fn: Function, delay: number, args: any[], isTimer = true): number {
let removeTimerFn = this._dequeueTimer(this._scheduler.nextId);
let removeTimerFn = this._dequeueTimer(Scheduler.nextId);
// Queue the callback and dequeue the timer on success and error.
let cb = this._fnAndFlush(fn, {onSuccess: removeTimerFn, onError: removeTimerFn});
let id = this._scheduler.scheduleFunction(cb, delay, args, false, !isTimer);
Expand All @@ -308,7 +308,7 @@ class FakeAsyncTestZoneSpec implements ZoneSpec {
}

private _setInterval(fn: Function, interval: number, args: any[]): number {
let id = this._scheduler.nextId;
let id = Scheduler.nextId;
let completers = {onSuccess: null as any, onError: this._dequeuePeriodicTimer(id)};
let cb = this._fnAndFlush(fn, completers);

Expand Down

0 comments on commit d32e79b

Please sign in to comment.