Chromium Code Reviews| Index: tests/async_helper.dart |
| diff --git a/tests/async_helper.dart b/tests/async_helper.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f52d8b580c47c9254a63bc0a87b9aa8f8d3efe9e |
| --- /dev/null |
| +++ b/tests/async_helper.dart |
| @@ -0,0 +1,54 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library async_helper; |
| + |
| +// 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.
|
| +// If a test is asynchronous, it needs to notify the testing driver |
| +// about this (otherwise tests may get reported as passing [after main() |
| +// finished] even if the asynchronous operations fail). |
| +// Tests which can't use the unittest framework should use the helper functions |
| +// in this library. |
| +// This library provides two methods |
| +// - asyncStart(): Needs to be called before an asynchronous operation is |
| +// scheduled. |
| +// - asyncEnd(): Needs to be called as soon as the asynchronous operation |
| +// ended. |
| +// After the last asyncStart() called was matched with a corresponding |
| +// asyncEnd() call, the testing driver will be notified that the tests is done. |
| + |
| + |
| +bool _initialized = false; |
| +int _asyncLevel = 0; |
| + |
| +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.
|
| + throw new Exception('Fatal: $msg. This is most likely a bug in your test'); |
| +} |
| + |
| +void asyncStart() { |
| + if (_initialized && _asyncLevel == 0) { |
| + _throwError('asyncStart() was called even though we are done with ' |
| + 'testing.'); |
|
kustermann
2013/06/13 14:40:51
I put these three assertions in, so we can catch i
|
| + } |
| + if (!_initialized) { |
| + print('unittest-suite-wait-for-done'); |
| + _initialized = true; |
| + } |
| + _asyncLevel++; |
| +} |
| + |
| +void asyncEnd() { |
| + if (_asyncLevel <= 0) { |
| + if (_initialized) { |
| + _throwError('asyncEnd() was called before asyncStart().'); |
| + } else { |
| + _throwError('asyncEnd() was called more often than asyncStart().'); |
| + } |
| + } |
| + _asyncLevel--; |
| + if (_asyncLevel == 0) { |
| + print('unittest-suite-done'); |
| + } |
| +} |
| + |
|
ahe
2013/06/17 13:51:11
Extra line at end of file.
kustermann
2013/06/25 07:14:21
Done.
|