| 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 /// This library is used for testing asynchronous tests. | |
| 6 /// If a test is asynchronous, it needs to notify the testing driver | |
| 7 /// about this (otherwise tests may get reported as passing [after main() | |
| 8 /// finished] even if the asynchronous operations fail). | |
| 9 /// Tests which can't use the unittest framework should use the helper functions | |
| 10 /// in this library. | |
| 11 /// This library provides two methods | |
| 12 /// - asyncStart(): Needs to be called before an asynchronous operation is | |
| 13 /// scheduled. | |
| 14 /// - asyncEnd(): Needs to be called as soon as the asynchronous operation | |
| 15 /// ended. | |
| 16 /// After the last asyncStart() called was matched with a corresponding | |
| 17 /// asyncEnd() call, the testing driver will be notified that the tests is done. | |
| 18 | |
| 19 library async_helper; | |
| 20 | |
| 21 // TODO(kustermann): This is problematic because we rely on a working | |
| 22 // 'dart:isolate' (i.e. it is in particular problematic with dart2js). | |
| 23 // It would be nice if we could use a different mechanism for different | |
| 24 // runtimes. | |
| 25 import 'dart:isolate'; | |
| 26 | |
| 27 bool _initialized = false; | |
| 28 ReceivePort _port = null; | |
| 29 int _asyncLevel = 0; | |
| 30 | |
| 31 Exception _buildException(String msg) { | |
| 32 return new Exception('Fatal: $msg. This is most likely a bug in your test.'); | |
| 33 } | |
| 34 | |
| 35 void asyncStart() { | |
| 36 if (_initialized && _asyncLevel == 0) { | |
| 37 throw _buildException('asyncStart() was called even though we are done ' | |
| 38 'with testing.'); | |
| 39 } | |
| 40 if (!_initialized) { | |
| 41 print('unittest-suite-wait-for-done'); | |
| 42 _initialized = true; | |
| 43 _port = new ReceivePort(); | |
| 44 } | |
| 45 _asyncLevel++; | |
| 46 } | |
| 47 | |
| 48 void asyncEnd() { | |
| 49 if (_asyncLevel <= 0) { | |
| 50 if (!_initialized) { | |
| 51 throw _buildException('asyncEnd() was called before asyncStart().'); | |
| 52 } else { | |
| 53 throw _buildException('asyncEnd() was called more often than ' | |
| 54 'asyncStart().'); | |
| 55 } | |
| 56 } | |
| 57 _asyncLevel--; | |
| 58 if (_asyncLevel == 0) { | |
| 59 _port.close(); | |
| 60 _port = null; | |
| 61 print('unittest-suite-success'); | |
| 62 } | |
| 63 } | |
| OLD | NEW |