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 group('breath', () { | 14 group('breath', () { |
15 var sentinel = 0; | 15 var sentinel = 0; |
16 var start; | 16 var start; |
17 | 17 |
18 test('initial', () { | 18 test('initial', () { |
19 start = new DateTime.now().millisecondsSinceEpoch; | |
19 Timer.run(() { | 20 Timer.run(() { |
20 sentinel = 1; | 21 sentinel = 1; |
21 }); | 22 }); |
22 }); | 23 }); |
23 | 24 |
24 test('starve', () { | 25 test('starve', () { |
25 start = new DateTime.now().millisecondsSinceEpoch; | 26 var now = new DateTime.now().millisecondsSinceEpoch; |
26 var now; | 27 while (now - start <= BREATH_INTERVAL) { |
27 do { | 28 // If less than BREATH_INTERVAL time has passed since |
29 // we scheduled the 'initial' timer it implies the | |
30 // current test is being run as a microtask which is | |
31 // again run before any timer callbacks. Hence the | |
32 // sentinel should not be 1. | |
28 expect(sentinel, 0); | 33 expect(sentinel, 0); |
29 now = new DateTime.now().millisecondsSinceEpoch; | 34 now = new DateTime.now().millisecondsSinceEpoch; |
30 } while (now - start <= BREATH_INTERVAL); | 35 }; |
36 // At this point we know at least BREATH_INTERVAL time has | |
37 // passed since we scheduled the Timer with the sentinel | |
38 // assignment. This means the next test should be scheduled | |
39 // using a timer as well. This ensures it will run after the | |
40 // timer scheduled in the 'initial' test. | |
kustermann
2014/11/17 12:32:02
This is not completely correct.
If test 'initial'
| |
31 }); | 41 }); |
32 | 42 |
33 test('breathed', () { | 43 test('breathed', () { |
34 expect(sentinel, 1); | 44 expect(sentinel, 1); |
35 }); | 45 }); |
36 }); | 46 }); |
37 } | 47 } |
OLD | NEW |