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

Side by Side Diff: packages/quiver/test/async/metronome_test.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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
OLDNEW
(Empty)
1 // Copyright 2014 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 library quiver.time.clock_test;
16
17 import 'package:quiver/testing/async.dart';
18 import 'package:quiver/async.dart';
19 import 'package:quiver/time.dart';
20 import 'package:test/test.dart';
21
22 main() {
23 group("Metronome", () {
24 test("delivers events as expected", () {
25 new FakeAsync().run((async) {
26 int callbacks = 0;
27 DateTime lastTime;
28 var sub = new Metronome.epoch(aMinute,
29 clock: async.getClock(DateTime.parse("2014-05-05 20:00:30")))
30 .listen((d) {
31 callbacks++;
32 lastTime = d;
33 });
34 expect(callbacks, 0, reason: "Should be no callbacks at start");
35 async.elapse(aSecond * 15);
36 expect(callbacks, 0, reason: "Should be no callbacks before trigger");
37 async.elapse(aSecond * 15);
38 expect(callbacks, 1, reason: "Calledback on rollover");
39 expect(lastTime, DateTime.parse("2014-05-05 20:01:00"),
40 reason: "And that time was correct");
41 async.elapse(aMinute * 1);
42 expect(callbacks, 2, reason: "Callback is repeated");
43 expect(lastTime, DateTime.parse("2014-05-05 20:02:00"),
44 reason: "And that time was correct");
45 sub.cancel();
46 async.elapse(aMinute * 2);
47 expect(callbacks, 2, reason: "No callbacks after subscription cancel");
48 });
49 });
50
51 test("can be re-listened to", () {
52 new FakeAsync().run((async) {
53 int callbacks = 0;
54 var clock = new Metronome.epoch(aMinute,
55 clock: async.getClock(DateTime.parse("2014-05-05 20:00:30")));
56 var sub = clock.listen((d) {
57 callbacks++;
58 });
59 async.elapse(aMinute);
60 expect(callbacks, 1);
61 sub.cancel();
62 async.elapse(aMinute);
63 expect(callbacks, 1);
64 sub = clock.listen((d) {
65 callbacks++;
66 });
67 async.elapse(aMinute);
68 expect(callbacks, 2);
69 });
70 });
71
72 test("supports multiple listeners joining and leaving", () {
73 new FakeAsync().run((async) {
74 List<int> callbacks = [0, 0];
75 var clock = new Metronome.epoch(aMinute,
76 clock: async.getClock(DateTime.parse("2014-05-05 20:00:30")));
77 List subs = [
78 clock.listen((d) {
79 callbacks[0]++;
80 }),
81 clock.listen((d) {
82 callbacks[1]++;
83 })
84 ];
85
86 async.elapse(aMinute);
87 expect(callbacks, [1, 1]);
88 subs[0].cancel();
89 async.elapse(aMinute);
90 expect(callbacks, [1, 2]);
91 });
92 });
93
94 test("can be anchored at any time", () {
95 new FakeAsync().run((async) {
96 List<DateTime> times = [];
97 DateTime start = DateTime.parse("2014-05-05 20:06:00");
98 Clock clock = async.getClock(start);
99 new Metronome.periodic(aMinute * 10,
100 clock: clock, anchor: clock.minutesAgo(59)).listen((d) {
101 times.add(d);
102 });
103 async.elapse(anHour);
104 expect(times, [
105 DateTime.parse("2014-05-05 20:07:00"),
106 DateTime.parse("2014-05-05 20:17:00"),
107 DateTime.parse("2014-05-05 20:27:00"),
108 DateTime.parse("2014-05-05 20:37:00"),
109 DateTime.parse("2014-05-05 20:47:00"),
110 DateTime.parse("2014-05-05 20:57:00"),
111 ]);
112 });
113 });
114
115 test("can be anchored in the future", () {
116 new FakeAsync().run((async) {
117 List<DateTime> times = [];
118 DateTime start = DateTime.parse("2014-05-05 20:06:00");
119 Clock clock = async.getClock(start);
120 new Metronome.periodic(aMinute * 10,
121 clock: clock, anchor: clock.minutesFromNow(61)).listen((d) {
122 times.add(d);
123 });
124 async.elapse(anHour);
125 expect(times, [
126 DateTime.parse("2014-05-05 20:07:00"),
127 DateTime.parse("2014-05-05 20:17:00"),
128 DateTime.parse("2014-05-05 20:27:00"),
129 DateTime.parse("2014-05-05 20:37:00"),
130 DateTime.parse("2014-05-05 20:47:00"),
131 DateTime.parse("2014-05-05 20:57:00"),
132 ]);
133 });
134 });
135
136 test("can be a periodic timer", () {
137 new FakeAsync().run((async) {
138 List<DateTime> times = [];
139 DateTime start = DateTime.parse("2014-05-05 20:06:00.004");
140 new Metronome.periodic(aMillisecond * 100,
141 clock: async.getClock(start), anchor: start).listen((d) {
142 times.add(d);
143 });
144 async.elapse(aMillisecond * 304);
145 expect(times, [
146 DateTime.parse("2014-05-05 20:06:00.104"),
147 DateTime.parse("2014-05-05 20:06:00.204"),
148 DateTime.parse("2014-05-05 20:06:00.304"),
149 ]);
150 });
151 });
152
153 test("resyncs when workers taking some time", () {
154 new FakeAsync().run((async) {
155 List<DateTime> times = [];
156 DateTime start = DateTime.parse("2014-05-05 20:06:00.004");
157 new Metronome.periodic(aMillisecond * 100,
158 clock: async.getClock(start), anchor: start).listen((d) {
159 times.add(d);
160 async.elapseBlocking(const Duration(milliseconds: 80));
161 });
162 async.elapse(aMillisecond * 304);
163 expect(times, [
164 DateTime.parse("2014-05-05 20:06:00.104"),
165 DateTime.parse("2014-05-05 20:06:00.204"),
166 DateTime.parse("2014-05-05 20:06:00.304"),
167 ]);
168 });
169 });
170
171 test("drops time when workers taking longer than interval", () {
172 new FakeAsync().run((async) {
173 List<DateTime> times = [];
174 DateTime start = DateTime.parse("2014-05-05 20:06:00.004");
175 new Metronome.periodic(aMillisecond * 100,
176 clock: async.getClock(start), anchor: start).listen((d) {
177 times.add(d);
178 async.elapseBlocking(const Duration(milliseconds: 105));
179 });
180 async.elapse(aMillisecond * 504);
181 expect(times, [
182 DateTime.parse("2014-05-05 20:06:00.104"),
183 DateTime.parse("2014-05-05 20:06:00.304"),
184 DateTime.parse("2014-05-05 20:06:00.504"),
185 ]);
186 });
187 });
188 });
189 }
OLDNEW
« no previous file with comments | « packages/quiver/test/async/iteration_test.dart ('k') | packages/quiver/test/async/stream_router_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698