Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(236)

Side by Side Diff: pkg/scheduled_test/lib/src/mock_clock.dart

Issue 812253002: Delete a bunch of packages that are now on GitHub. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Un-delete http Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/scheduled_test/lib/src/future_group.dart ('k') | pkg/scheduled_test/lib/src/schedule.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 /// A library that wraps [Timer] in a way that can be mocked out in test code.
6 /// Application code only needs to use [newTimer] to get an instance of [Timer].
7 /// Then test code can call [mock] to mock out all new [Timer] instances so that
8 /// they're controllable by a returned [Clock] object.
9 library mock_clock;
10
11 import 'dart:async';
12
13 import 'utils.dart';
14
15 /// The mock clock object. New [Timer]s will be mocked if and only if this is
16 /// non-`null`.
17 Clock _clock;
18
19 /// Whether or not mocking is active.
20 bool get _mocked => _clock != null;
21
22 /// Causes all future calls to [newTimer] to return mock [Timer] objects that
23 /// are controlled by the returned [Clock]. This may only be called once.
24 Clock mock() {
25 if (_mocked) {
26 throw new StateError("mock_clock.mock() has already been called.");
27 }
28
29 _clock = new Clock._();
30 return _clock;
31 }
32
33 typedef void TimerCallback();
34
35 /// Returns a new (possibly mocked) [Timer]. Works the same as `new Timer`.
36 Timer newTimer(Duration duration, TimerCallback callback) =>
37 _mocked ? new _MockTimer(duration, callback) : new Timer(duration, callback);
38
39 /// A clock that controls when mocked [Timer]s move forward in time. It starts
40 /// at time 0 and advances forward millisecond-by-millisecond, broadcasting each
41 /// tick on the [onTick] stream.
42 class Clock {
43 /// The current time of the clock, in milliseconds. Starts at 0.
44 int get time => _time;
45 int _time = 0;
46
47 /// Controller providing streams for listening.
48 StreamController<int> _broadcastController =
49 new StreamController<int>.broadcast(sync: true);
50
51 Clock._();
52
53 /// The stream of millisecond ticks of the clock.
54 Stream<int> get onTick => _broadcastController.stream;
55
56 /// Advances the clock forward by [milliseconds]. This works like synchronous
57 /// code that takes [milliseconds] to execute; any [Timer]s that are scheduled
58 /// to fire during the interval will do so asynchronously once control returns
59 /// to the event loop.
60 void tick([int milliseconds = 1]) {
61 for (var i = 0; i < milliseconds; i++) {
62 var tickTime = ++_time;
63 scheduleMicrotask(() {
64 _broadcastController.add(tickTime);
65 });
66 }
67 }
68
69 /// Automatically progresses forward in time as long as there are still
70 /// subscribers to [onTick] (that is, [Timer]s waiting to fire). After each
71 /// tick, this pumps the event loop repeatedly so that all non-clock-dependent
72 /// code runs before the next tick.
73 void run() {
74 pumpEventQueue().then((_) {
75 if (!_broadcastController.hasListener) return null;
76 tick();
77 return run();
78 });
79 }
80 }
81
82 /// A mock implementation of [Timer] that uses [Clock] to keep time, rather than
83 /// the system clock.
84 class _MockTimer implements Timer {
85 /// The time at which the timer should fire.
86 final int _time;
87
88 /// The callback to run when the timer fires.
89 final TimerCallback _callback;
90
91 /// The subscription to the [Clock.onTick] stream.
92 StreamSubscription _subscription;
93
94 _MockTimer(Duration duration, this._callback)
95 : _time = _clock.time + duration.inMilliseconds {
96 _subscription = _clock.onTick.listen((time) {
97 if (time < _time) return;
98 _subscription.cancel();
99 _callback();
100 });
101 }
102
103 bool get isActive => _subscription != null;
104
105 void cancel() {
106 _subscription.cancel();
107 _subscription = null;
108 }
109 }
OLDNEW
« no previous file with comments | « pkg/scheduled_test/lib/src/future_group.dart ('k') | pkg/scheduled_test/lib/src/schedule.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698