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

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

Issue 12209073: Add a scheduled test library. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Make everything play nicely with the outside world. 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
Index: pkg/scheduled_test/lib/src/utils.dart
diff --git a/pkg/scheduled_test/lib/src/utils.dart b/pkg/scheduled_test/lib/src/utils.dart
new file mode 100644
index 0000000000000000000000000000000000000000..94be7ef7dd6d5a9bf0748c42f482be75272d4eab
--- /dev/null
+++ b/pkg/scheduled_test/lib/src/utils.dart
@@ -0,0 +1,53 @@
+// 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 utils;
+
+import 'dart:async';
+
+/// Configures [future] so that its result (success or exception) is passed on
+/// to [completer].
+void chainToCompleter(Future future, Completer completer) {
+ future.then((value) => completer.complete(value),
+ onError: (e) => completer.completeError(e.error, e.stackTrace));
+}
+
+/// Prepends each line in [text] with [prefix].
+String prefixLines(String text, {String prefix: '| '}) =>
+ text.split('\n').map((line) => '$prefix$line').join('\n');
+
+/// Returns a [Future] that completes in [milliseconds].
+Future sleep(int milliseconds) {
+ var completer = new Completer();
+ new Timer(milliseconds, (_) => completer.complete());
+ return completer.future;
+}
+
+/// Wraps [input] to provide a timeout. If [input] completes before
+/// [milliseconds] have passed, then the return value completes in the same way.
+/// However, if [milliseconds] pass before [input] has completed, [onTimeout] is
+/// run and its result is passed to [input] (with chaining, if it returns a
+/// [Future]).
+///
+/// Note that timing out will not cancel the asynchronous operation behind
+/// [input].
+Future timeout(Future input, int milliseconds, onTimeout()) {
+ bool completed = false;
+ var completer = new Completer();
+ var timer = new Timer(milliseconds, (_) {
+ completed = true;
+ chainToCompleter(new Future.immediate(null).then((_) => onTimeout()),
+ completer);
+ });
+ input.then((value) {
+ if (completed) return;
+ timer.cancel();
+ completer.complete(value);
+ }).catchError((e) {
+ if (completed) return;
+ timer.cancel();
+ completer.completeError(e.error, e.stackTrace);
+ });
+ return completer.future;
+}

Powered by Google App Engine
This is Rietveld 408576698