Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit a53f33d

Browse files
yjbanovjbdeboer
authored andcommitted
feat(mock): Add timer queue checks in mock zone
Closes #1157
1 parent f30d51a commit a53f33d

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/mock/zone.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@ microLeap() {
6868
*/
6969
isAsyncQueueEmpty() => _asyncQueue.isEmpty;
7070

71+
/**
72+
* Returns whether there are outstanding timers.
73+
*/
74+
isTimerQueueEmpty() => _timerQueue.isEmpty;
75+
76+
/**
77+
* Returns whether there are outstanding non-periodic timers.
78+
*/
79+
isNonPeriodicTimerQueueEmpty() => _timerQueue
80+
.where((_TimerSpec spec) => !spec.periodic)
81+
.isEmpty;
82+
83+
/**
84+
* Returns whether there are outstanding periodic timers.
85+
*/
86+
isPeriodicTimerQueueEmpty() => _timerQueue
87+
.where((_TimerSpec spec) => spec.periodic)
88+
.isEmpty;
89+
7190
/**
7291
* Simulates a clock tick by running any scheduled timers. Can only be used
7392
* in [async] tests.Clock tick will call [microLeap] to process the microtask

test/mock/zone_spec.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,28 @@ void main() {
332332
(_) => dump("i never run"));
333333
})).toThrow('1 active timer(s) are still in the queue.');
334334
});
335+
336+
it('should report no timers when there are none', async(() {
337+
expect(isTimerQueueEmpty()).toBe(true);
338+
expect(isNonPeriodicTimerQueueEmpty()).toBe(true);
339+
expect(isPeriodicTimerQueueEmpty()).toBe(true);
340+
}));
341+
342+
it('should report remaining non-periodic timers', async(() {
343+
new Future(() => null);
344+
expect(isTimerQueueEmpty()).toBe(false);
345+
expect(isNonPeriodicTimerQueueEmpty()).toBe(false);
346+
expect(isPeriodicTimerQueueEmpty()).toBe(true);
347+
clockTick();
348+
}));
349+
350+
it('should report remaining periodic timers', async(() {
351+
var t = new Timer.periodic(new Duration(seconds: 1), (_) => null);
352+
expect(isTimerQueueEmpty()).toBe(false);
353+
expect(isNonPeriodicTimerQueueEmpty()).toBe(true);
354+
expect(isPeriodicTimerQueueEmpty()).toBe(false);
355+
t.cancel();
356+
}));
335357
});
336358
});
337359
});

0 commit comments

Comments
 (0)