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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: packages/quiver/test/async/metronome_test.dart
diff --git a/packages/quiver/test/async/metronome_test.dart b/packages/quiver/test/async/metronome_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..4391cb5345260f7b3eb9770e72852c75be8b5f34
--- /dev/null
+++ b/packages/quiver/test/async/metronome_test.dart
@@ -0,0 +1,189 @@
+// 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.
+
+library quiver.time.clock_test;
+
+import 'package:quiver/testing/async.dart';
+import 'package:quiver/async.dart';
+import 'package:quiver/time.dart';
+import 'package:test/test.dart';
+
+main() {
+ group("Metronome", () {
+ test("delivers events as expected", () {
+ new FakeAsync().run((async) {
+ int callbacks = 0;
+ DateTime lastTime;
+ var sub = new Metronome.epoch(aMinute,
+ clock: async.getClock(DateTime.parse("2014-05-05 20:00:30")))
+ .listen((d) {
+ callbacks++;
+ lastTime = d;
+ });
+ expect(callbacks, 0, reason: "Should be no callbacks at start");
+ async.elapse(aSecond * 15);
+ expect(callbacks, 0, reason: "Should be no callbacks before trigger");
+ async.elapse(aSecond * 15);
+ expect(callbacks, 1, reason: "Calledback on rollover");
+ expect(lastTime, DateTime.parse("2014-05-05 20:01:00"),
+ reason: "And that time was correct");
+ async.elapse(aMinute * 1);
+ expect(callbacks, 2, reason: "Callback is repeated");
+ expect(lastTime, DateTime.parse("2014-05-05 20:02:00"),
+ reason: "And that time was correct");
+ sub.cancel();
+ async.elapse(aMinute * 2);
+ expect(callbacks, 2, reason: "No callbacks after subscription cancel");
+ });
+ });
+
+ test("can be re-listened to", () {
+ new FakeAsync().run((async) {
+ int callbacks = 0;
+ var clock = new Metronome.epoch(aMinute,
+ clock: async.getClock(DateTime.parse("2014-05-05 20:00:30")));
+ var sub = clock.listen((d) {
+ callbacks++;
+ });
+ async.elapse(aMinute);
+ expect(callbacks, 1);
+ sub.cancel();
+ async.elapse(aMinute);
+ expect(callbacks, 1);
+ sub = clock.listen((d) {
+ callbacks++;
+ });
+ async.elapse(aMinute);
+ expect(callbacks, 2);
+ });
+ });
+
+ test("supports multiple listeners joining and leaving", () {
+ new FakeAsync().run((async) {
+ List<int> callbacks = [0, 0];
+ var clock = new Metronome.epoch(aMinute,
+ clock: async.getClock(DateTime.parse("2014-05-05 20:00:30")));
+ List subs = [
+ clock.listen((d) {
+ callbacks[0]++;
+ }),
+ clock.listen((d) {
+ callbacks[1]++;
+ })
+ ];
+
+ async.elapse(aMinute);
+ expect(callbacks, [1, 1]);
+ subs[0].cancel();
+ async.elapse(aMinute);
+ expect(callbacks, [1, 2]);
+ });
+ });
+
+ test("can be anchored at any time", () {
+ new FakeAsync().run((async) {
+ List<DateTime> times = [];
+ DateTime start = DateTime.parse("2014-05-05 20:06:00");
+ Clock clock = async.getClock(start);
+ new Metronome.periodic(aMinute * 10,
+ clock: clock, anchor: clock.minutesAgo(59)).listen((d) {
+ times.add(d);
+ });
+ async.elapse(anHour);
+ expect(times, [
+ DateTime.parse("2014-05-05 20:07:00"),
+ DateTime.parse("2014-05-05 20:17:00"),
+ DateTime.parse("2014-05-05 20:27:00"),
+ DateTime.parse("2014-05-05 20:37:00"),
+ DateTime.parse("2014-05-05 20:47:00"),
+ DateTime.parse("2014-05-05 20:57:00"),
+ ]);
+ });
+ });
+
+ test("can be anchored in the future", () {
+ new FakeAsync().run((async) {
+ List<DateTime> times = [];
+ DateTime start = DateTime.parse("2014-05-05 20:06:00");
+ Clock clock = async.getClock(start);
+ new Metronome.periodic(aMinute * 10,
+ clock: clock, anchor: clock.minutesFromNow(61)).listen((d) {
+ times.add(d);
+ });
+ async.elapse(anHour);
+ expect(times, [
+ DateTime.parse("2014-05-05 20:07:00"),
+ DateTime.parse("2014-05-05 20:17:00"),
+ DateTime.parse("2014-05-05 20:27:00"),
+ DateTime.parse("2014-05-05 20:37:00"),
+ DateTime.parse("2014-05-05 20:47:00"),
+ DateTime.parse("2014-05-05 20:57:00"),
+ ]);
+ });
+ });
+
+ test("can be a periodic timer", () {
+ new FakeAsync().run((async) {
+ List<DateTime> times = [];
+ DateTime start = DateTime.parse("2014-05-05 20:06:00.004");
+ new Metronome.periodic(aMillisecond * 100,
+ clock: async.getClock(start), anchor: start).listen((d) {
+ times.add(d);
+ });
+ async.elapse(aMillisecond * 304);
+ expect(times, [
+ DateTime.parse("2014-05-05 20:06:00.104"),
+ DateTime.parse("2014-05-05 20:06:00.204"),
+ DateTime.parse("2014-05-05 20:06:00.304"),
+ ]);
+ });
+ });
+
+ test("resyncs when workers taking some time", () {
+ new FakeAsync().run((async) {
+ List<DateTime> times = [];
+ DateTime start = DateTime.parse("2014-05-05 20:06:00.004");
+ new Metronome.periodic(aMillisecond * 100,
+ clock: async.getClock(start), anchor: start).listen((d) {
+ times.add(d);
+ async.elapseBlocking(const Duration(milliseconds: 80));
+ });
+ async.elapse(aMillisecond * 304);
+ expect(times, [
+ DateTime.parse("2014-05-05 20:06:00.104"),
+ DateTime.parse("2014-05-05 20:06:00.204"),
+ DateTime.parse("2014-05-05 20:06:00.304"),
+ ]);
+ });
+ });
+
+ test("drops time when workers taking longer than interval", () {
+ new FakeAsync().run((async) {
+ List<DateTime> times = [];
+ DateTime start = DateTime.parse("2014-05-05 20:06:00.004");
+ new Metronome.periodic(aMillisecond * 100,
+ clock: async.getClock(start), anchor: start).listen((d) {
+ times.add(d);
+ async.elapseBlocking(const Duration(milliseconds: 105));
+ });
+ async.elapse(aMillisecond * 504);
+ expect(times, [
+ DateTime.parse("2014-05-05 20:06:00.104"),
+ DateTime.parse("2014-05-05 20:06:00.304"),
+ DateTime.parse("2014-05-05 20:06:00.504"),
+ ]);
+ });
+ });
+ });
+}
« 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