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 bool running = true; | |
24 var timer = new Timer.periodic(ms * interval, (t) { | |
25 expectGTE(nextTick, t.tick, "tick {1} before expect next tick {0}."); | |
26 nextTick += 1; | |
27 int time = sw.elapsedMilliseconds; | |
28 int minTime = interval * t.tick; | |
29 expectGTE(minTime, time, "Actual time {1} before {0} at tick ${t.tick}"); | |
30 if (t.tick > 20) { | |
31 running = false; | |
32 t.cancel(); | |
33 asyncEnd(); | |
34 } | |
35 }); | |
36 | |
37 /// Test that ticks still happen when the rest of the system is slow. | |
38 delay() { | |
39 int time = new DateTime.now().millisecondsSinceEpoch; | |
40 int limit = time + 3 * interval; | |
41 while (new DateTime.now().millisecondsSinceEpoch < limit) { | |
42 // Idle. | |
43 } | |
44 nextTick = timer.tick + 2; // At least increment by two, probably more. | |
45 if (running) Timer.run(delay); | |
46 } | |
47 | |
48 Timer.run(delay); | |
49 } | |
OLD | NEW |