OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library unittest.breath_test; | 5 library unittest.breath_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
10 | 10 |
11 void main() { | 11 void main() { |
12 // Test the sync test 'breath' feature of unittest. | 12 // Test the sync test 'breath' feature of unittest. |
13 | 13 |
| 14 // We use the testStartStopwatch to determine if the 'starve' |
| 15 // test was executed within a small enough time interval from |
| 16 // the first test that we are guaranteed the second test is |
| 17 // running in a microtask. If the second test is running as a |
| 18 // microtask we are guaranteed the timer scheduled in the |
| 19 // first test has not been run yet. |
| 20 var testStartStopwatch = new Stopwatch()..start(); |
| 21 |
14 group('breath', () { | 22 group('breath', () { |
15 var sentinel = 0; | 23 var sentinel = 0; |
16 var start; | |
17 | 24 |
18 test('initial', () { | 25 test('initial', () { |
19 Timer.run(() { | 26 Timer.run(() { |
20 sentinel = 1; | 27 sentinel = 1; |
21 }); | 28 }); |
22 }); | 29 }); |
23 | 30 |
24 test('starve', () { | 31 test('starve', () { |
25 start = new DateTime.now().millisecondsSinceEpoch; | 32 // If less than BREATH_INTERVAL time has passed since before |
26 var now; | 33 // we started the test group then the previous test's timer |
27 do { | 34 // has not been run (at least this is what we are testing). |
| 35 if (testStartStopwatch.elapsed.inMilliseconds <= BREATH_INTERVAL) { |
28 expect(sentinel, 0); | 36 expect(sentinel, 0); |
29 now = new DateTime.now().millisecondsSinceEpoch; | 37 } |
30 } while (now - start <= BREATH_INTERVAL); | 38 |
| 39 // Next we wait for at least BREATH_INTERVAL to guaranteed the |
| 40 // next (third) test is run using a timer which means it will |
| 41 // run after the timer scheduled in the first test and hence |
| 42 // the sentinel should have been set to 1. |
| 43 var sw = new Stopwatch()..start(); |
| 44 while (sw.elapsed.inMilliseconds < BREATH_INTERVAL); |
31 }); | 45 }); |
32 | 46 |
33 test('breathed', () { | 47 test('breathed', () { |
34 expect(sentinel, 1); | 48 expect(sentinel, 1); |
35 }); | 49 }); |
36 }); | 50 }); |
37 } | 51 } |
OLD | NEW |