Skip to content

Commit

Permalink
feat(mock): Add timer queue checks in mock zone
Browse files Browse the repository at this point in the history
  • Loading branch information
yjbanov authored and Diana Salsbury committed Jul 16, 2014
1 parent b370afd commit 8aee8f2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/mock/zone.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ microLeap() {
*/
isAsyncQueueEmpty() => _asyncQueue.isEmpty;

/**
* Returns whether there are outstanding timers.
*/
isTimerQueueEmpty() => _timerQueue.isEmpty;

/**
* Returns whether there are outstanding non-periodic timers.
*/
isNonPeriodicTimerQueueEmpty() => _timerQueue
.where((_TimerSpec spec) => !spec.periodic)
.isEmpty;

/**
* Returns whether there are outstanding periodic timers.
*/
isPeriodicTimerQueueEmpty() => _timerQueue
.where((_TimerSpec spec) => spec.periodic)
.isEmpty;

/**
* Simulates a clock tick by running any scheduled timers. Can only be used
* in [async] tests.Clock tick will call [microLeap] to process the microtask
Expand Down
22 changes: 22 additions & 0 deletions test/mock/zone_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,28 @@ void main() {
(_) => dump("i never run"));
})).toThrow('1 active timer(s) are still in the queue.');
});

it('should report no timers when there are none', async(() {
expect(isTimerQueueEmpty()).toBe(true);
expect(isNonPeriodicTimerQueueEmpty()).toBe(true);
expect(isPeriodicTimerQueueEmpty()).toBe(true);
}));

it('should report remaining non-periodic timers', async(() {
new Future(() => null);
expect(isTimerQueueEmpty()).toBe(false);
expect(isNonPeriodicTimerQueueEmpty()).toBe(false);
expect(isPeriodicTimerQueueEmpty()).toBe(true);
clockTick();
}));

it('should report remaining periodic timers', async(() {
var t = new Timer.periodic(new Duration(seconds: 1), (_) => null);
expect(isTimerQueueEmpty()).toBe(false);
expect(isNonPeriodicTimerQueueEmpty()).toBe(true);
expect(isPeriodicTimerQueueEmpty()).toBe(false);
t.cancel();
}));
});
});
});
Expand Down

0 comments on commit 8aee8f2

Please sign in to comment.