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

Unified Diff: pkg/scheduled_test/test/utils.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/test/substitute_future_test.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/scheduled_test/test/utils.dart
diff --git a/pkg/scheduled_test/test/utils.dart b/pkg/scheduled_test/test/utils.dart
index 3b15dd511c6c25c14e8773b8ade96ae5621c3dd0..bd87edf3414b50bb22cf6713a3729c3966b6f896 100644
--- a/pkg/scheduled_test/test/utils.dart
+++ b/pkg/scheduled_test/test/utils.dart
@@ -7,6 +7,9 @@ library test_utils;
import 'dart:async';
import 'package:scheduled_test/src/utils.dart';
+import 'package:scheduled_test/src/mock_clock.dart' as mock_clock;
+
+export 'package:scheduled_test/src/utils.dart';
/// Wraps [input] to provide a timeout. If [input] completes before
/// [milliseconds] have passed, then the return value completes in the same way.
@@ -36,52 +39,11 @@ Future timeout(Future input, int milliseconds, onTimeout()) {
return completer.future;
}
-/// Returns a [Future] that completes after pumping the event queue [times]
-/// times. By default, this should pump the event queue enough times to allow
-/// any code to run, as long as it's not waiting on some external event.
-Future pumpEventQueue([int times=200]) {
- if (times == 0) return new Future.immediate(null);
- return new Future.immediate(null).then((_) => pumpEventQueue(times - 1));
-}
-
-/// A class for controlling the relative order of multiple [Future]s. This is an
-/// alternative to using [Timer]s to ensure that callbacks run in a given order.
-///
-/// [Future]s are generated by calling [wait] with a given position. The
-/// [Future]s will complete in position order, pumping the event queue
-/// thoroughly between each completion and before the first completion.
-class Waiter {
- /// The completers for [Future]s returned by [wait]. Conceptually, this is
- /// really a priority queue, but Dart doesn't have such a data type built in.
- /// Since the number of calls to [wait] will be very small, a [Map] is fine.
- final _completers = new Map<int, Completer>();
-
- /// Whether [_wait] is currently running.
- var _waiting = false;
-
- /// Returns a [Future] that will complete in [position] order relative to
- /// other [Future]s returned by [wait]. The order of calling [wait] doesn't
- /// matter.
- Future wait(int position) {
- if (_completers.containsKey(position)) {
- throw new StateError("Waiter already has a wait at position $position");
- }
-
- var completer = new Completer();
- _completers[position] = completer;
- _wait();
- return completer.future;
- }
-
- /// Pumps the event queue, then completes the [Future] at the lowest position.
- void _wait() {
- if (_waiting) return;
- _waiting = true;
- pumpEventQueue().then((_) {
- _waiting = false;
- var completer = _completers.remove(_completers.keys.min());
- if (!_completers.isEmpty) _wait();
- completer.complete();
- });
- }
+/// Returns a [Future] that will complete in [milliseconds].
+Future sleep(int milliseconds) {
+ var completer = new Completer();
+ mock_clock.newTimer(new Duration(milliseconds: milliseconds), (_) {
+ completer.complete();
+ });
+ return completer.future;
}
« no previous file with comments | « pkg/scheduled_test/test/substitute_future_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698