Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, 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 async_helper; | |
| 6 | |
| 7 // This library is used for testing asynchronous tests. | |
|
ahe
2013/06/17 13:51:11
This should be a documentation comment and go befo
kustermann
2013/06/25 07:14:21
Done.
| |
| 8 // If a test is asynchronous, it needs to notify the testing driver | |
| 9 // about this (otherwise tests may get reported as passing [after main() | |
| 10 // finished] even if the asynchronous operations fail). | |
| 11 // Tests which can't use the unittest framework should use the helper functions | |
| 12 // in this library. | |
| 13 // This library provides two methods | |
| 14 // - asyncStart(): Needs to be called before an asynchronous operation is | |
| 15 // scheduled. | |
| 16 // - asyncEnd(): Needs to be called as soon as the asynchronous operation | |
| 17 // ended. | |
| 18 // After the last asyncStart() called was matched with a corresponding | |
| 19 // asyncEnd() call, the testing driver will be notified that the tests is done. | |
| 20 | |
| 21 | |
| 22 bool _initialized = false; | |
| 23 int _asyncLevel = 0; | |
| 24 | |
| 25 void _throwError(String msg) { | |
|
ahe
2013/06/17 13:51:11
This method is will create annoying stack traces.
kustermann
2013/06/25 07:14:21
Done.
| |
| 26 throw new Exception('Fatal: $msg. This is most likely a bug in your test'); | |
| 27 } | |
| 28 | |
| 29 void asyncStart() { | |
| 30 if (_initialized && _asyncLevel == 0) { | |
| 31 _throwError('asyncStart() was called even though we are done with ' | |
| 32 'testing.'); | |
|
kustermann
2013/06/13 14:40:51
I put these three assertions in, so we can catch i
| |
| 33 } | |
| 34 if (!_initialized) { | |
| 35 print('unittest-suite-wait-for-done'); | |
| 36 _initialized = true; | |
| 37 } | |
| 38 _asyncLevel++; | |
| 39 } | |
| 40 | |
| 41 void asyncEnd() { | |
| 42 if (_asyncLevel <= 0) { | |
| 43 if (_initialized) { | |
| 44 _throwError('asyncEnd() was called before asyncStart().'); | |
| 45 } else { | |
| 46 _throwError('asyncEnd() was called more often than asyncStart().'); | |
| 47 } | |
| 48 } | |
| 49 _asyncLevel--; | |
| 50 if (_asyncLevel == 0) { | |
| 51 print('unittest-suite-done'); | |
| 52 } | |
| 53 } | |
| 54 | |
|
ahe
2013/06/17 13:51:11
Extra line at end of file.
kustermann
2013/06/25 07:14:21
Done.
| |
| OLD | NEW |