OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 library timer_test; | |
6 | |
7 import 'dart:async'; | |
8 import 'package:expect/expect.dart'; | |
9 import 'package:async_helper/async_helper.dart'; | |
10 | |
11 const ms = const Duration(milliseconds: 1); | |
12 | |
13 expectGTE(min, actual, msg) { | |
14 if (actual >= min) return; | |
15 Expect._fail(msg.replaceAll('{0}', "$min").replaceAll('{1}', "$actual")); | |
16 } | |
17 | |
18 main() { | |
19 int interval = 20; | |
20 asyncStart(); | |
21 var sw = new Stopwatch()..start(); | |
22 int nextTick = 1; | |
23 new Timer.periodic(ms * interval, (t) { | |
24 expectGTE(nextTick, t.tick, "tick {1} before expect next tick {0}."); | |
25 int time = sw.elapsedMilliseconds; | |
26 int minTime = interval * t.tick; | |
27 expectGTE(minTime, time, "Actual time {1} before {0} at tick ${t.tick}"); | |
28 while (sw.elapsedMilliseconds < time + 3 * interval) { | |
29 // idle. | |
30 } | |
31 nextTick = t.tick + 2; // At least increment by two, probably more. | |
32 if (t.tick > 20) { | |
33 t.cancel(); | |
34 asyncEnd(); | |
35 } | |
36 }); | |
37 } | |
OLD | NEW |