OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library unittest.breath_test; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 import 'package:unittest/unittest.dart'; | |
10 | |
11 void main() { | |
12 // Test the sync test 'breath' feature of unittest. | |
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 | |
22 group('breath', () { | |
23 var sentinel = 0; | |
24 | |
25 test('initial', () { | |
26 Timer.run(() { | |
27 sentinel = 1; | |
28 }); | |
29 }); | |
30 | |
31 test('starve', () { | |
32 // If less than BREATH_INTERVAL time has passed since before | |
33 // we started the test group then the previous test's timer | |
34 // has not been run (at least this is what we are testing). | |
35 if (testStartStopwatch.elapsed.inMilliseconds <= BREATH_INTERVAL) { | |
36 expect(sentinel, 0); | |
37 } | |
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); | |
45 }); | |
46 | |
47 test('breathed', () { | |
48 expect(sentinel, 1); | |
49 }); | |
50 }); | |
51 } | |
OLD | NEW |