Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Unified Diff: pkg/async_helper/lib/async_helper.dart

Issue 23512008: Call asyncEnd only on success. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Updated cf. comments. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/compiler/dart2js/analyze_all_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/async_helper/lib/async_helper.dart
diff --git a/pkg/async_helper/lib/async_helper.dart b/pkg/async_helper/lib/async_helper.dart
index 84df557a811d6ce40f3643430a3477d7f420260a..808e19875b4f3fce41b4b5c54167560de2a642e5 100644
--- a/pkg/async_helper/lib/async_helper.dart
+++ b/pkg/async_helper/lib/async_helper.dart
@@ -8,13 +8,18 @@
/// 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
+/// This library provides four methods
/// - asyncStart(): Needs to be called before an asynchronous operation is
/// scheduled.
/// - asyncEnd(): Needs to be called as soon as the asynchronous operation
/// ended.
+/// - asyncSuccess(_): Variant of asyncEnd useful together with Future.then.
+/// - asyncTest(f()): Helper method that wraps a computation that returns a
+/// Future with matching calls to asyncStart() and
+/// asyncSuccess(_).
/// After the last asyncStart() called was matched with a corresponding
-/// asyncEnd() call, the testing driver will be notified that the tests is done.
+/// asyncEnd() or asyncSuccess(_) call, the testing driver will be notified that
+/// the tests is done.
library async_helper;
@@ -32,6 +37,7 @@ Exception _buildException(String msg) {
return new Exception('Fatal: $msg. This is most likely a bug in your test.');
}
+/// Call this method before an asynchronous test is created.
void asyncStart() {
if (_initialized && _asyncLevel == 0) {
throw _buildException('asyncStart() was called even though we are done '
@@ -45,6 +51,7 @@ void asyncStart() {
_asyncLevel++;
}
+/// Call this after an asynchronous test has ended successfully.
void asyncEnd() {
if (_asyncLevel <= 0) {
if (!_initialized) {
@@ -62,7 +69,26 @@ void asyncEnd() {
}
}
+/**
+ * Call this after an asynchronous test has ended successfully. This is a helper
+ * for calling [asyncEnd].
+ *
+ * This method intentionally has a signature that matches [:Future.then:] as a
+ * convenience for calling [asyncEnd] when a [:Future:] completes without error,
+ * like this:
+ *
+ * asyncStart();
+ * Future result = test();
+ * result.then(asyncSuccess);
+ */
+void asyncSuccess(_) => asyncEnd();
+
+/**
+ * Helper method for performing asynchronous tests involving [:Future:].
+ *
+ * [f] must return a [:Future:] for the test computation.
+ */
void asyncTest(f()) {
asyncStart();
- f().whenComplete(() => asyncEnd());
+ f().then(asyncSuccess);
}
« no previous file with comments | « no previous file | tests/compiler/dart2js/analyze_all_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698