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

Side by Side Diff: pkg/scheduled_test/lib/src/utils.dart

Issue 12224128: Pump the event queue manually rather than using a timer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Get rid of one more sleep call 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/scheduled_test/test/metatest.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 library utils; 5 library utils;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 /// Configures [future] so that its result (success or exception) is passed on 9 /// Configures [future] so that its result (success or exception) is passed on
10 /// to [completer]. 10 /// to [completer].
11 void chainToCompleter(Future future, Completer completer) { 11 void chainToCompleter(Future future, Completer completer) {
12 future.then((value) => completer.complete(value), 12 future.then((value) => completer.complete(value),
13 onError: (e) => completer.completeError(e.error, e.stackTrace)); 13 onError: (e) => completer.completeError(e.error, e.stackTrace));
14 } 14 }
15 15
16 /// Prepends each line in [text] with [prefix]. 16 /// Prepends each line in [text] with [prefix].
17 String prefixLines(String text, {String prefix: '| '}) => 17 String prefixLines(String text, {String prefix: '| '}) =>
18 text.split('\n').map((line) => '$prefix$line').join('\n'); 18 text.split('\n').map((line) => '$prefix$line').join('\n');
19
20 /// Returns a [Future] that completes in at least [milliseconds].
21 ///
22 /// This pumps the event loop after every millisecond of sleeping. This should
23 /// ensure that even if the CPU is pegged and code is running much slower than
24 /// expected, the sleep will take longer than any non-sleep code that's running.
25 Future sleep(int milliseconds) {
26 if (milliseconds == 0) return new Future.immediate(null);
27 return _sleep1().then((_) => sleep(milliseconds - 1));
28 }
29
30 /// Returns a [Future] that completes in one millisecond.
31 Future _sleep1() {
32 var completer = new Completer();
33 new Timer(1, (_) => completer.complete());
34 return completer.future;
35 }
36
37 /// Wraps [input] to provide a timeout. If [input] completes before
38 /// [milliseconds] have passed, then the return value completes in the same way.
39 /// However, if [milliseconds] pass before [input] has completed, [onTimeout] is
40 /// run and its result is passed to [input] (with chaining, if it returns a
41 /// [Future]).
42 ///
43 /// Note that timing out will not cancel the asynchronous operation behind
44 /// [input].
45 Future timeout(Future input, int milliseconds, onTimeout()) {
46 bool completed = false;
47 var completer = new Completer();
48 var timer = new Timer(milliseconds, (_) {
49 completed = true;
50 chainToCompleter(new Future.immediate(null).then((_) => onTimeout()),
51 completer);
52 });
53 input.then((value) {
54 if (completed) return;
55 timer.cancel();
56 completer.complete(value);
57 }).catchError((e) {
58 if (completed) return;
59 timer.cancel();
60 completer.completeError(e.error, e.stackTrace);
61 });
62 return completer.future;
63 }
OLDNEW
« no previous file with comments | « no previous file | pkg/scheduled_test/test/metatest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698