Chromium Code Reviews| OLD | NEW | 
|---|---|
| 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 test_utils; | 5 library test_utils; | 
| 6 | 6 | 
| 7 import 'dart:async'; | 7 import 'dart:async'; | 
| 8 | 8 | 
| 9 import 'package:scheduled_test/src/utils.dart'; | 9 import 'package:scheduled_test/src/utils.dart'; | 
| 10 import 'package:scheduled_test/src/mock_clock.dart' as mock_clock; | 10 import 'package:scheduled_test/src/mock_clock.dart' as mock_clock; | 
| 11 | 11 | 
| 12 export 'package:scheduled_test/src/utils.dart'; | 12 export 'package:scheduled_test/src/utils.dart'; | 
| 13 | 13 | 
| 14 /// Wraps [input] to provide a timeout. If [input] completes before | 14 /// Wraps [input] to provide a timeout. If [input] completes before | 
| 15 /// [milliseconds] have passed, then the return value completes in the same way. | 15 /// [milliseconds] have passed, then the return value completes in the same way. | 
| 16 /// However, if [milliseconds] pass before [input] has completed, [onTimeout] is | 16 /// However, if [milliseconds] pass before [input] has completed, [onTimeout] is | 
| 17 /// run and its result is passed to [input] (with chaining, if it returns a | 17 /// run and its result is passed to [input] (with chaining, if it returns a | 
| 18 /// [Future]). | 18 /// [Future]). | 
| 19 /// | 19 /// | 
| 20 /// Note that timing out will not cancel the asynchronous operation behind | 20 /// Note that timing out will not cancel the asynchronous operation behind | 
| 21 /// [input]. | 21 /// [input]. | 
| 22 Future timeout(Future input, int milliseconds, onTimeout()) { | 22 Future timeout(Future input, int milliseconds, onTimeout()) { | 
| 
 
Lasse Reichstein Nielsen
2013/02/28 11:14:48
Not changing "int milliseconds" to "Duration durat
 
floitsch
2013/02/28 13:24:05
It's not really in my code. But I would like it to
 
 | |
| 23 bool completed = false; | 23 bool completed = false; | 
| 24 var completer = new Completer(); | 24 var completer = new Completer(); | 
| 25 var timer = new Timer(milliseconds, (_) { | 25 var timer = new Timer(new Duration(milliseconds: milliseconds), () { | 
| 26 completed = true; | 26 completed = true; | 
| 27 chainToCompleter(new Future.immediate(null).then((_) => onTimeout()), | 27 chainToCompleter(new Future.immediate(null).then((_) => onTimeout()), | 
| 28 completer); | 28 completer); | 
| 29 }); | 29 }); | 
| 30 input.then((value) { | 30 input.then((value) { | 
| 31 if (completed) return; | 31 if (completed) return; | 
| 32 timer.cancel(); | 32 timer.cancel(); | 
| 33 completer.complete(value); | 33 completer.complete(value); | 
| 34 }).catchError((e) { | 34 }).catchError((e) { | 
| 35 if (completed) return; | 35 if (completed) return; | 
| 36 timer.cancel(); | 36 timer.cancel(); | 
| 37 completer.completeError(e.error, e.stackTrace); | 37 completer.completeError(e.error, e.stackTrace); | 
| 38 }); | 38 }); | 
| 39 return completer.future; | 39 return completer.future; | 
| 40 } | 40 } | 
| 41 | 41 | 
| 42 /// Returns a [Future] that will complete in [milliseconds]. | 42 /// Returns a [Future] that will complete in [milliseconds]. | 
| 43 Future sleep(int milliseconds) { | 43 Future sleep(int milliseconds) { | 
| 44 var completer = new Completer(); | 44 var completer = new Completer(); | 
| 45 mock_clock.newTimer(new Duration(milliseconds: milliseconds), (_) { | 45 mock_clock.newTimer(new Duration(milliseconds: milliseconds), () { | 
| 46 completer.complete(); | 46 completer.complete(); | 
| 47 }); | 47 }); | 
| 48 return completer.future; | 48 return completer.future; | 
| 49 } | 49 } | 
| OLD | NEW |