| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 /// This library is used for testing asynchronous tests. | 5 /// This library is used for testing asynchronous tests. |
| 6 /// If a test is asynchronous, it needs to notify the testing driver | 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() | 7 /// about this (otherwise tests may get reported as passing [after main() |
| 8 /// finished] even if the asynchronous operations fail). | 8 /// finished] even if the asynchronous operations fail). |
| 9 /// Tests which can't use the unittest framework should use the helper functions | 9 /// Tests which can't use the unittest framework should use the helper functions |
| 10 /// in this library. | 10 /// in this library. |
| 11 /// This library provides four methods | 11 /// This library provides four methods |
| 12 /// - asyncStart(): Needs to be called before an asynchronous operation is | 12 /// - asyncStart(): Needs to be called before an asynchronous operation is |
| 13 /// scheduled. | 13 /// scheduled. |
| 14 /// - asyncEnd(): Needs to be called as soon as the asynchronous operation | 14 /// - asyncEnd(): Needs to be called as soon as the asynchronous operation |
| 15 /// ended. | 15 /// ended. |
| 16 /// - asyncSuccess(_): Variant of asyncEnd useful together with Future.then. | 16 /// - asyncSuccess(_): Variant of asyncEnd useful together with Future.then. |
| 17 /// - asyncTest(f()): Helper method that wraps a computation that returns a | 17 /// - asyncTest(f()): Helper method that wraps a computation that returns a |
| 18 /// Future with matching calls to asyncStart() and | 18 /// Future with matching calls to asyncStart() and |
| 19 /// asyncSuccess(_). | 19 /// asyncSuccess(_). |
| 20 /// After the last asyncStart() called was matched with a corresponding | 20 /// After the last asyncStart() called was matched with a corresponding |
| 21 /// asyncEnd() or asyncSuccess(_) call, the testing driver will be notified that | 21 /// asyncEnd() or asyncSuccess(_) call, the testing driver will be notified that |
| 22 /// the tests is done. | 22 /// the tests is done. |
| 23 | 23 |
| 24 library async_helper; | 24 library async_helper; |
| 25 | 25 |
| 26 // TODO(kustermann): This is problematic because we rely on a working | 26 bool _initialized = false; |
| 27 // 'dart:isolate' (i.e. it is in particular problematic with dart2js). | |
| 28 // It would be nice if we could use a different mechanism for different | |
| 29 // runtimes. | |
| 30 import 'dart:isolate'; | |
| 31 | 27 |
| 32 bool _initialized = false; | 28 typedef void _Action0(); |
| 33 ReceivePort _port = null; | 29 _Action0 _onAsyncEnd; |
| 30 |
| 34 int _asyncLevel = 0; | 31 int _asyncLevel = 0; |
| 35 | 32 |
| 36 Exception _buildException(String msg) { | 33 Exception _buildException(String msg) { |
| 37 return new Exception('Fatal: $msg. This is most likely a bug in your test.'); | 34 return new Exception('Fatal: $msg. This is most likely a bug in your test.'); |
| 38 } | 35 } |
| 39 | 36 |
| 37 /// Implementation method called from language_tests.js. |
| 38 /// Registers the callback that will be used complete the test. |
| 39 void asyncTestInitialize(_Action0 callback) { |
| 40 _asyncLevel = 0; |
| 41 _initialized = false; |
| 42 _onAsyncEnd = callback; |
| 43 } |
| 44 |
| 45 /// Implementation method called from language_tests.js. |
| 46 /// Returns true if an asyncTest was started. |
| 47 bool get asyncTestStarted => _initialized; |
| 48 |
| 40 /// Call this method before an asynchronous test is created. | 49 /// Call this method before an asynchronous test is created. |
| 41 void asyncStart() { | 50 void asyncStart() { |
| 42 if (_initialized && _asyncLevel == 0) { | 51 if (_initialized && _asyncLevel == 0) { |
| 43 throw _buildException('asyncStart() was called even though we are done ' | 52 throw _buildException('asyncStart() was called even though we are done ' |
| 44 'with testing.'); | 53 'with testing.'); |
| 45 } | 54 } |
| 46 if (!_initialized) { | 55 if (!_initialized) { |
| 56 if (_onAsyncEnd == null) { |
| 57 throw _buildException( |
| 58 'asyncStart() was called before asyncTestInitialize()'); |
| 59 } |
| 60 |
| 47 print('unittest-suite-wait-for-done'); | 61 print('unittest-suite-wait-for-done'); |
| 48 _initialized = true; | 62 _initialized = true; |
| 49 _port = new ReceivePort(); | 63 |
| 50 } | 64 } |
| 51 _asyncLevel++; | 65 _asyncLevel++; |
| 52 } | 66 } |
| 53 | 67 |
| 54 /// Call this after an asynchronous test has ended successfully. | 68 /// Call this after an asynchronous test has ended successfully. |
| 55 void asyncEnd() { | 69 void asyncEnd() { |
| 56 if (_asyncLevel <= 0) { | 70 if (_asyncLevel <= 0) { |
| 57 if (!_initialized) { | 71 if (!_initialized) { |
| 58 throw _buildException('asyncEnd() was called before asyncStart().'); | 72 throw _buildException('asyncEnd() was called before asyncStart().'); |
| 59 } else { | 73 } else { |
| 60 throw _buildException('asyncEnd() was called more often than ' | 74 throw _buildException('asyncEnd() was called more often than ' |
| 61 'asyncStart().'); | 75 'asyncStart().'); |
| 62 } | 76 } |
| 63 } | 77 } |
| 64 _asyncLevel--; | 78 _asyncLevel--; |
| 65 if (_asyncLevel == 0) { | 79 if (_asyncLevel == 0) { |
| 66 _port.close(); | 80 var callback = _onAsyncEnd; |
| 67 _port = null; | 81 _onAsyncEnd = null; |
| 82 callback(); |
| 68 print('unittest-suite-success'); | 83 print('unittest-suite-success'); |
| 69 } | 84 } |
| 70 } | 85 } |
| 71 | 86 |
| 72 /** | 87 /** |
| 73 * Call this after an asynchronous test has ended successfully. This is a helper | 88 * Call this after an asynchronous test has ended successfully. This is a helper |
| 74 * for calling [asyncEnd]. | 89 * for calling [asyncEnd]. |
| 75 * | 90 * |
| 76 * This method intentionally has a signature that matches [:Future.then:] as a | 91 * This method intentionally has a signature that matches [:Future.then:] as a |
| 77 * convenience for calling [asyncEnd] when a [:Future:] completes without error, | 92 * convenience for calling [asyncEnd] when a [:Future:] completes without error, |
| 78 * like this: | 93 * like this: |
| 79 * | 94 * |
| 80 * asyncStart(); | 95 * asyncStart(); |
| 81 * Future result = test(); | 96 * Future result = test(); |
| 82 * result.then(asyncSuccess); | 97 * result.then(asyncSuccess); |
| 83 */ | 98 */ |
| 84 void asyncSuccess(_) => asyncEnd(); | 99 void asyncSuccess(_) => asyncEnd(); |
| 85 | 100 |
| 86 /** | 101 /** |
| 87 * Helper method for performing asynchronous tests involving [:Future:]. | 102 * Helper method for performing asynchronous tests involving [:Future:]. |
| 88 * | 103 * |
| 89 * [f] must return a [:Future:] for the test computation. | 104 * [f] must return a [:Future:] for the test computation. |
| 90 */ | 105 */ |
| 91 void asyncTest(f()) { | 106 void asyncTest(f()) { |
| 92 asyncStart(); | 107 asyncStart(); |
| 93 f().then(asyncSuccess); | 108 f().then(asyncSuccess); |
| 94 } | 109 } |
| OLD | NEW |