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

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

Issue 14753009: Make StreamSubscription be the active part of a stream. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Made tests run (mostly) Created 7 years, 7 months 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
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /// A library that wraps [Timer] in a way that can be mocked out in test code. 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]. 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 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. 8 /// they're controllable by a returned [Clock] object.
9 library mock_clock; 9 library mock_clock;
10 10
(...skipping 27 matching lines...) Expand all
38 38
39 /// A clock that controls when mocked [Timer]s move forward in time. It starts 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 40 /// at time 0 and advances forward millisecond-by-millisecond, broadcasting each
41 /// tick on the [onTick] stream. 41 /// tick on the [onTick] stream.
42 class Clock { 42 class Clock {
43 /// The current time of the clock, in milliseconds. Starts at 0. 43 /// The current time of the clock, in milliseconds. Starts at 0.
44 int get time => _time; 44 int get time => _time;
45 int _time = 0; 45 int _time = 0;
46 46
47 /// The stream of millisecond ticks of the clock. 47 /// The stream of millisecond ticks of the clock.
48 Stream<int> get onTick { 48 Stream<int> get onTick => _onTickControllerStream;
49 if (_onTickControllerStream == null) { 49
50 _onTickControllerStream = _onTickController.stream.asBroadcastStream(); 50 StreamController<int> _onTickController;
51 } 51 Stream<int> _onTickControllerStream;
52 return _onTickControllerStream; 52
53 Clock._() {
54 _initStream();
53 } 55 }
54 56
55 final _onTickController = new StreamController<int>(); 57 /// Creates a new stream controller and its stream as broadcast stream.
floitsch 2013/05/22 16:26:29 The behavior is not the same anymore. When there a
56 Stream<int> _onTickControllerStream; 58 /// When a stream controller is cancelled, a new controller is created
57 59 /// to give the perception of a stream that can be listened to more than
58 Clock._(); 60 /// once.
61 void _initStream() {
62 _onTickController = new StreamController<int>(onCancel: _initStream);
63 _onTickControllerStream = _onTickController.stream.asBroadcastStream();
64 }
nweiz 2013/05/22 18:42:41 I don't understand these changes. Why are we creat
Lasse Reichstein Nielsen 2013/05/24 06:02:49 The stream-controller only received the cancel whe
59 65
60 /// Advances the clock forward by [milliseconds]. This works like synchronous 66 /// Advances the clock forward by [milliseconds]. This works like synchronous
61 /// code that takes [milliseconds] to execute; any [Timer]s that are scheduled 67 /// code that takes [milliseconds] to execute; any [Timer]s that are scheduled
62 /// to fire during the interval will do so asynchronously once control returns 68 /// to fire during the interval will do so asynchronously once control returns
63 /// to the event loop. 69 /// to the event loop.
64 void tick([int milliseconds=1]) { 70 void tick([int milliseconds=1]) {
65 for (var i = 0; i < milliseconds; i++) { 71 for (var i = 0; i < milliseconds; i++) {
66 var tickTime = ++_time; 72 var tickTime = ++_time;
67 new Future.value().then((_) => _onTickController.add(tickTime)); 73 runAsync(() => _onTickController.add(tickTime));
68 } 74 }
69 } 75 }
70 76
71 /// Automatically progresses forward in time as long as there are still 77 /// Automatically progresses forward in time as long as there are still
72 /// subscribers to [onTick] (that is, [Timer]s waiting to fire). After each 78 /// subscribers to [onTick] (that is, [Timer]s waiting to fire). After each
73 /// tick, this pumps the event loop repeatedly so that all non-clock-dependent 79 /// tick, this pumps the event loop repeatedly so that all non-clock-dependent
74 /// code runs before the next tick. 80 /// code runs before the next tick.
75 void run() { 81 void run() {
76 pumpEventQueue().then((_) { 82 pumpEventQueue().then((_) {
77 if (!_onTickController.hasListener) return; 83 if (_onTickController == null) return;
floitsch 2013/05/22 16:26:29 the _onTickController can never be null. I think t
Lasse Reichstein Nielsen 2013/05/24 06:02:49 Good point.
78 tick(); 84 tick();
79 return run(); 85 return run();
80 }); 86 });
81 } 87 }
82 } 88 }
83 89
84 /// A mock implementation of [Timer] that uses [Clock] to keep time, rather than 90 /// A mock implementation of [Timer] that uses [Clock] to keep time, rather than
85 /// the system clock. 91 /// the system clock.
86 class _MockTimer implements Timer { 92 class _MockTimer implements Timer {
87 /// The time at which the timer should fire. 93 /// The time at which the timer should fire.
88 final int _time; 94 final int _time;
89 95
90 /// The callback to run when the timer fires. 96 /// The callback to run when the timer fires.
91 final TimerCallback _callback; 97 final TimerCallback _callback;
92 98
93 /// The subscription to the [Clock.onTick] stream. 99 /// The subscription to the [Clock.onTick] stream.
94 StreamSubscription _subscription; 100 StreamSubscription _subscription;
95 101
96 _MockTimer(Duration duration, this._callback) 102 _MockTimer(Duration duration, this._callback)
97 : _time = _clock.time + duration.inMilliseconds { 103 : _time = _clock.time + duration.inMilliseconds {
98 _subscription = _clock.onTick.listen((time) { 104 _subscription = _clock.onTick.listen((time) {
99 if (time < _time) return; 105 if (time < _time) return;
100 _subscription.cancel(); 106 _subscription.cancel();
101 _callback(); 107 _callback();
102 }); 108 });
103 } 109 }
104 110
105 void cancel() => _subscription.cancel(); 111 void cancel() => _subscription.cancel();
106 } 112 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698