-
Notifications
You must be signed in to change notification settings - Fork 15
/
fake_async_test.dart
646 lines (570 loc) · 20.1 KB
/
fake_async_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'dart:async';
import 'package:clock/clock.dart';
import 'package:fake_async/fake_async.dart';
import 'package:test/test.dart';
void main() {
final initialTime = DateTime(2000);
final elapseBy = const Duration(days: 1);
test('should set initial time', () {
expect(FakeAsync().getClock(initialTime).now(), initialTime);
});
group('elapseBlocking', () {
test('should elapse time without calling timers', () {
Timer(elapseBy ~/ 2, neverCalled);
FakeAsync().elapseBlocking(elapseBy);
});
test('should elapse time by the specified amount', () {
final async = FakeAsync()..elapseBlocking(elapseBy);
expect(async.elapsed, elapseBy);
});
test('should throw when called with a negative duration', () {
expect(() => FakeAsync().elapseBlocking(const Duration(days: -1)),
throwsArgumentError);
});
});
group('elapse', () {
test('should elapse time by the specified amount', () {
FakeAsync().run((async) {
async.elapse(elapseBy);
expect(async.elapsed, elapseBy);
});
});
test('should throw ArgumentError when called with a negative duration', () {
expect(() => FakeAsync().elapse(const Duration(days: -1)),
throwsArgumentError);
});
test('should throw when called before previous call is complete', () {
FakeAsync().run((async) {
Timer(elapseBy ~/ 2, expectAsync0(() {
expect(() => async.elapse(elapseBy), throwsStateError);
}));
async.elapse(elapseBy);
});
});
group('when creating timers', () {
test('should call timers expiring before or at end time', () {
FakeAsync().run((async) {
Timer(elapseBy ~/ 2, expectAsync0(() {}));
Timer(elapseBy, expectAsync0(() {}));
async.elapse(elapseBy);
});
});
test('should call timers expiring due to elapseBlocking', () {
FakeAsync().run((async) {
Timer(elapseBy, () => async.elapseBlocking(elapseBy));
Timer(elapseBy * 2, expectAsync0(() {}));
async.elapse(elapseBy);
expect(async.elapsed, elapseBy * 2);
});
});
test('should call timers at their scheduled time', () {
FakeAsync().run((async) {
Timer(elapseBy ~/ 2, expectAsync0(() {
expect(async.elapsed, elapseBy ~/ 2);
}));
final periodicCalledAt = <Duration>[];
Timer.periodic(
elapseBy ~/ 2, (_) => periodicCalledAt.add(async.elapsed));
async.elapse(elapseBy);
expect(periodicCalledAt, [elapseBy ~/ 2, elapseBy]);
});
});
test('should not call timers expiring after end time', () {
FakeAsync().run((async) {
Timer(elapseBy * 2, neverCalled);
async.elapse(elapseBy);
});
});
test('should not call canceled timers', () {
FakeAsync().run((async) {
Timer(elapseBy ~/ 2, neverCalled).cancel();
async.elapse(elapseBy);
});
});
test('should call periodic timers each time the duration elapses', () {
FakeAsync().run((async) {
Timer.periodic(elapseBy ~/ 10, expectAsync1((_) {}, count: 10));
async.elapse(elapseBy);
});
});
test('should call timers occurring at the same time in FIFO order', () {
FakeAsync().run((async) {
final log = <String>[];
Timer(elapseBy ~/ 2, () => log.add('1'));
Timer(elapseBy ~/ 2, () => log.add('2'));
async.elapse(elapseBy);
expect(log, ['1', '2']);
});
});
test('should maintain FIFO order even with periodic timers', () {
FakeAsync().run((async) {
final log = <String>[];
Timer.periodic(elapseBy ~/ 2, (_) => log.add('periodic 1'));
Timer(elapseBy ~/ 2, () => log.add('delayed 1'));
Timer(elapseBy, () => log.add('delayed 2'));
Timer.periodic(elapseBy, (_) => log.add('periodic 2'));
async.elapse(elapseBy);
expect(log, [
'periodic 1',
'delayed 1',
'periodic 1',
'delayed 2',
'periodic 2'
]);
});
});
test('should process microtasks surrounding each timer', () {
FakeAsync().run((async) {
var microtaskCalls = 0;
var timerCalls = 0;
void scheduleMicrotasks() {
for (var i = 0; i < 5; i++) {
scheduleMicrotask(() => microtaskCalls++);
}
}
scheduleMicrotasks();
Timer.periodic(elapseBy ~/ 5, (_) {
timerCalls++;
expect(microtaskCalls, 5 * timerCalls);
scheduleMicrotasks();
});
async.elapse(elapseBy);
expect(timerCalls, 5);
expect(microtaskCalls, 5 * (timerCalls + 1));
});
});
test('should pass the periodic timer itself to callbacks', () {
FakeAsync().run((async) {
late Timer constructed;
constructed = Timer.periodic(elapseBy, expectAsync1((passed) {
expect(passed, same(constructed));
}));
async.elapse(elapseBy);
});
});
test('should call microtasks before advancing time', () {
FakeAsync().run((async) {
scheduleMicrotask(expectAsync0(() {
expect(async.elapsed, Duration.zero);
}));
async.elapse(const Duration(minutes: 1));
});
});
test('should add event before advancing time', () {
FakeAsync().run((async) {
final controller = StreamController<void>();
expect(controller.stream.first.then((_) {
expect(async.elapsed, Duration.zero);
}), completes);
controller.add(null);
async.elapse(const Duration(minutes: 1));
});
});
test('should increase negative duration timers to zero duration', () {
FakeAsync().run((async) {
final negativeDuration = const Duration(days: -1);
Timer(negativeDuration, expectAsync0(() {
expect(async.elapsed, Duration.zero);
}));
async.elapse(const Duration(minutes: 1));
});
});
test('should not be additive with elapseBlocking', () {
FakeAsync().run((async) {
Timer(Duration.zero, () => async.elapseBlocking(elapseBy * 5));
async.elapse(elapseBy);
expect(async.elapsed, elapseBy * 5);
});
});
group('isActive', () {
test('should be false after timer is run', () {
FakeAsync().run((async) {
final timer = Timer(elapseBy ~/ 2, () {});
async.elapse(elapseBy);
expect(timer.isActive, isFalse);
});
});
test('should be true after periodic timer is run', () {
FakeAsync().run((async) {
final timer = Timer.periodic(elapseBy ~/ 2, (_) {});
async.elapse(elapseBy);
expect(timer.isActive, isTrue);
});
});
test('should be false after timer is canceled', () {
FakeAsync().run((async) {
final timer = Timer(elapseBy ~/ 2, () {})..cancel();
expect(timer.isActive, isFalse);
});
});
});
test('should work with new Future()', () {
FakeAsync().run((async) {
Future(expectAsync0(() {}));
async.elapse(Duration.zero);
});
});
test('should work with Future.delayed', () {
FakeAsync().run((async) {
Future.delayed(elapseBy, expectAsync0(() {}));
async.elapse(elapseBy);
});
});
test('should work with Future.timeout', () {
FakeAsync().run((async) {
final completer = Completer<void>();
expect(completer.future.timeout(elapseBy ~/ 2),
throwsA(const TypeMatcher<TimeoutException>()));
async.elapse(elapseBy);
completer.complete();
});
});
// TODO: Pausing and resuming the timeout Stream doesn't work since
// it uses `new Stopwatch()`.
//
// See https://code.google.com/p/dart/issues/detail?id=18149
test('should work with Stream.periodic', () {
FakeAsync().run((async) {
expect(Stream.periodic(const Duration(minutes: 1), (i) => i),
emitsInOrder([0, 1, 2]));
async.elapse(const Duration(minutes: 3));
});
});
test('should work with Stream.timeout', () {
FakeAsync().run((async) {
final controller = StreamController<int>();
final timed = controller.stream.timeout(const Duration(minutes: 2));
final events = <int>[];
final errors = <Object>[];
timed.listen(events.add, onError: errors.add);
controller.add(0);
async.elapse(const Duration(minutes: 1));
expect(events, [0]);
async.elapse(const Duration(minutes: 1));
expect(errors, hasLength(1));
expect(errors.first, const TypeMatcher<TimeoutException>());
});
});
});
});
group('flushMicrotasks', () {
test('should flush a microtask', () {
FakeAsync().run((async) {
Future.microtask(expectAsync0(() {}));
async.flushMicrotasks();
});
});
test('should flush microtasks scheduled by microtasks in order', () {
FakeAsync().run((async) {
final log = <int>[];
scheduleMicrotask(() {
log.add(1);
scheduleMicrotask(() => log.add(3));
});
scheduleMicrotask(() => log.add(2));
async.flushMicrotasks();
expect(log, [1, 2, 3]);
});
});
test('should not run timers', () {
FakeAsync().run((async) {
final log = <int>[];
scheduleMicrotask(() => log.add(1));
Timer.run(() => log.add(2));
Timer.periodic(const Duration(seconds: 1), (_) => log.add(2));
async.flushMicrotasks();
expect(log, [1]);
});
});
});
group('flushTimers', () {
test('should flush timers in FIFO order', () {
FakeAsync().run((async) {
final log = <int>[];
Timer.run(() {
log.add(1);
Timer(elapseBy, () => log.add(3));
});
Timer.run(() => log.add(2));
async.flushTimers(timeout: elapseBy * 2);
expect(log, [1, 2, 3]);
expect(async.elapsed, elapseBy);
});
});
test(
'should run collateral periodic timers with non-periodic first if '
'scheduled first', () {
FakeAsync().run((async) {
final log = <String>[];
Timer(const Duration(seconds: 2), () => log.add('delayed'));
Timer.periodic(const Duration(seconds: 1), (_) => log.add('periodic'));
async.flushTimers(flushPeriodicTimers: false);
expect(log, ['periodic', 'delayed', 'periodic']);
});
});
test(
'should run collateral periodic timers with periodic first '
'if scheduled first', () {
FakeAsync().run((async) {
final log = <String>[];
Timer.periodic(const Duration(seconds: 1), (_) => log.add('periodic'));
Timer(const Duration(seconds: 2), () => log.add('delayed'));
async.flushTimers(flushPeriodicTimers: false);
expect(log, ['periodic', 'periodic', 'delayed']);
});
});
test('should time out', () {
FakeAsync().run((async) {
// Schedule 3 timers. All but the last one should fire.
for (var delay in [30, 60, 90]) {
Timer(Duration(minutes: delay),
expectAsync0(() {}, count: delay == 90 ? 0 : 1));
}
expect(() => async.flushTimers(), throwsStateError);
});
});
test('should time out a chain of timers', () {
FakeAsync().run((async) {
var count = 0;
void createTimer() {
Timer(const Duration(minutes: 30), () {
count++;
createTimer();
});
}
createTimer();
expect(() => async.flushTimers(timeout: const Duration(hours: 2)),
throwsStateError);
expect(count, 4);
});
});
test('should time out periodic timers', () {
FakeAsync().run((async) {
Timer.periodic(
const Duration(minutes: 30), expectAsync1((_) {}, count: 2));
expect(() => async.flushTimers(timeout: const Duration(hours: 1)),
throwsStateError);
});
});
test('should flush periodic timers', () {
FakeAsync().run((async) {
var count = 0;
Timer.periodic(const Duration(minutes: 30), (timer) {
if (count == 3) timer.cancel();
count++;
});
async.flushTimers(timeout: const Duration(hours: 20));
expect(count, 4);
});
});
test('should compute absolute timeout as elapsed + timeout', () {
FakeAsync().run((async) {
var count = 0;
void createTimer() {
Timer(const Duration(minutes: 30), () {
count++;
if (count < 4) createTimer();
});
}
createTimer();
async
..elapse(const Duration(hours: 1))
..flushTimers(timeout: const Duration(hours: 1));
expect(count, 4);
});
});
});
group('stats', () {
test('should report the number of pending microtasks', () {
FakeAsync().run((async) {
expect(async.microtaskCount, 0);
scheduleMicrotask(() {});
expect(async.microtaskCount, 1);
scheduleMicrotask(() {});
expect(async.microtaskCount, 2);
async.flushMicrotasks();
expect(async.microtaskCount, 0);
});
});
test('it should report the number of pending periodic timers', () {
FakeAsync().run((async) {
expect(async.periodicTimerCount, 0);
final timer = Timer.periodic(const Duration(minutes: 30), (_) {});
expect(async.periodicTimerCount, 1);
Timer.periodic(const Duration(minutes: 20), (_) {});
expect(async.periodicTimerCount, 2);
async.elapse(const Duration(minutes: 20));
expect(async.periodicTimerCount, 2);
timer.cancel();
expect(async.periodicTimerCount, 1);
});
});
test('it should report the number of pending non periodic timers', () {
FakeAsync().run((async) {
expect(async.nonPeriodicTimerCount, 0);
final timer = Timer(const Duration(minutes: 30), () {});
expect(async.nonPeriodicTimerCount, 1);
Timer(const Duration(minutes: 20), () {});
expect(async.nonPeriodicTimerCount, 2);
async.elapse(const Duration(minutes: 25));
expect(async.nonPeriodicTimerCount, 1);
timer.cancel();
expect(async.nonPeriodicTimerCount, 0);
});
});
test('should report debugging information of pending timers', () {
FakeAsync().run((fakeAsync) {
expect(fakeAsync.pendingTimers, isEmpty);
final nonPeriodic =
Timer(const Duration(seconds: 1), () {}) as FakeTimer;
final periodic =
Timer.periodic(const Duration(seconds: 2), (Timer timer) {})
as FakeTimer;
final debugInfo = fakeAsync.pendingTimers;
expect(debugInfo.length, 2);
expect(
debugInfo,
containsAll([
nonPeriodic,
periodic,
]),
);
const thisFileName = 'fake_async_test.dart';
expect(nonPeriodic.debugString, contains(':01.0'));
expect(nonPeriodic.debugString, contains('periodic: false'));
expect(nonPeriodic.debugString, contains(thisFileName));
expect(periodic.debugString, contains(':02.0'));
expect(periodic.debugString, contains('periodic: true'));
expect(periodic.debugString, contains(thisFileName));
});
});
test(
'should report debugging information of pending timers excluding '
'stack traces', () {
FakeAsync(includeTimerStackTrace: false).run((fakeAsync) {
expect(fakeAsync.pendingTimers, isEmpty);
final nonPeriodic =
Timer(const Duration(seconds: 1), () {}) as FakeTimer;
final periodic =
Timer.periodic(const Duration(seconds: 2), (Timer timer) {})
as FakeTimer;
final debugInfo = fakeAsync.pendingTimers;
expect(debugInfo.length, 2);
expect(
debugInfo,
containsAll([
nonPeriodic,
periodic,
]),
);
const thisFileName = 'fake_async_test.dart';
expect(nonPeriodic.debugString, contains(':01.0'));
expect(nonPeriodic.debugString, contains('periodic: false'));
expect(nonPeriodic.debugString, isNot(contains(thisFileName)));
expect(periodic.debugString, contains(':02.0'));
expect(periodic.debugString, contains('periodic: true'));
expect(periodic.debugString, isNot(contains(thisFileName)));
});
});
});
group('timers', () {
test("should become inactive as soon as they're invoked", () {
return FakeAsync().run((async) {
late Timer timer;
timer = Timer(elapseBy, expectAsync0(() {
expect(timer.isActive, isFalse);
}));
expect(timer.isActive, isTrue);
async.elapse(elapseBy);
expect(timer.isActive, isFalse);
});
});
test('should increment tick in a non-periodic timer', () {
return FakeAsync().run((async) {
late Timer timer;
timer = Timer(elapseBy, expectAsync0(() {
expect(timer.tick, 1);
}));
expect(timer.tick, 0);
async.elapse(elapseBy);
});
});
test('should increment tick in a periodic timer', () {
return FakeAsync().run((async) {
final ticks = <int>[];
Timer.periodic(
elapseBy,
expectAsync1((timer) {
ticks.add(timer.tick);
}, count: 2));
async
..elapse(elapseBy)
..elapse(elapseBy);
expect(ticks, [1, 2]);
});
});
});
group('clock', () {
test('updates following elapse()', () {
FakeAsync().run((async) {
final before = clock.now();
async.elapse(elapseBy);
expect(clock.now(), before.add(elapseBy));
});
});
test('updates following elapseBlocking()', () {
FakeAsync().run((async) {
final before = clock.now();
async.elapseBlocking(elapseBy);
expect(clock.now(), before.add(elapseBy));
});
});
group('starts at', () {
test('the time at which the FakeAsync was created', () {
final start = DateTime.now();
FakeAsync().run((async) {
expect(clock.now(), _closeToTime(start));
async.elapse(elapseBy);
expect(clock.now(), _closeToTime(start.add(elapseBy)));
});
});
test('the value of clock.now()', () {
final start = DateTime(1990, 8, 11);
withClock(Clock.fixed(start), () {
FakeAsync().run((async) {
expect(clock.now(), start);
async.elapse(elapseBy);
expect(clock.now(), start.add(elapseBy));
});
});
});
test('an explicit value', () {
final start = DateTime(1990, 8, 11);
FakeAsync(initialTime: start).run((async) {
expect(clock.now(), start);
async.elapse(elapseBy);
expect(clock.now(), start.add(elapseBy));
});
});
});
});
}
/// Returns a matcher that asserts that a [DateTime] is within 100ms of
/// [expected].
Matcher _closeToTime(DateTime expected) => predicate(
(actual) =>
expected.difference(actual as DateTime).inMilliseconds.abs() < 100,
'is close to $expected');