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

Unified Diff: pkg/scheduled_test/lib/src/substitute_future.dart

Issue 12251012: Add built-in timeouts to scheduled_test. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 7 years, 10 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 | « pkg/scheduled_test/lib/src/schedule_error.dart ('k') | pkg/scheduled_test/lib/src/utils.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/scheduled_test/lib/src/substitute_future.dart
diff --git a/pkg/scheduled_test/lib/src/substitute_future.dart b/pkg/scheduled_test/lib/src/substitute_future.dart
new file mode 100644
index 0000000000000000000000000000000000000000..4122a3ddc277492a5d994256a103064b76081750
--- /dev/null
+++ b/pkg/scheduled_test/lib/src/substitute_future.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 substitute_future;
+
+import 'dart:async';
+
+/// A wrapper for [Future] that allows other [Future]s to be substituted in as
+/// the wrapped [Future]. This is used for injecting timeout errors into
+/// long-running [Future]s.
+class SubstituteFuture<T> implements Future<T> {
+ /// The wrapped [Future].
+ Future<T> _inner;
+
+ /// The completer that corresponds to [this]'s result.
+ final Completer<T> _completer = new Completer<T>();
+
+ /// Whether or not [this] has been completed yet.
+ bool _complete = false;
+
+ SubstituteFuture(Future wrapped) {
+ substitute(wrapped);
+ }
+
+ Stream<T> asStream() => _completer.future.asStream();
+ Future catchError(onError(AsyncError asyncError), {bool test(error)}) =>
+ _completer.future.catchError(onError, test: test);
+ Future then(onValue(T value), {onError(AsyncError asyncError)}) =>
+ _completer.future.then(onValue, onError: onError);
+ Future<T> whenComplete(action()) => _completer.future.whenComplete(action);
+
+ /// Substitutes [newFuture] for the currently wrapped [Future], which is
+ /// returned.
+ Future<T> substitute(Future<T> newFuture) {
+ if (_complete) {
+ throw new StateError("You may not call substitute on a SubstituteFuture "
+ "that's already complete.");
+ }
+
+ var oldFuture = _inner;
+ _inner = newFuture;
+ _inner.then((value) {
+ if (_inner != newFuture) return;
+ _completer.complete(value);
+ _complete = true;
+ }).catchError((error) {
+ if (_inner != newFuture) return;
+ _completer.completeError(error);
+ _complete = true;
+ });
+ return oldFuture;
+ }
+}
« no previous file with comments | « pkg/scheduled_test/lib/src/schedule_error.dart ('k') | pkg/scheduled_test/lib/src/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698