| 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:io'; | 7 import 'dart:io'; |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 | 9 |
| 10 import 'package:scheduled_test/scheduled_test.dart'; | 10 import 'package:scheduled_test/scheduled_test.dart'; |
| 11 import 'package:scheduled_test/src/utils.dart'; | 11 import 'package:scheduled_test/src/utils.dart'; |
| 12 import 'package:scheduled_test/src/mock_clock.dart' as mock_clock; | 12 import 'package:scheduled_test/src/mock_clock.dart' as mock_clock; |
| 13 | 13 |
| 14 import 'metatest.dart'; | 14 import 'metatest.dart'; |
| 15 | 15 |
| 16 export 'package:scheduled_test/src/utils.dart'; | 16 export 'package:scheduled_test/src/utils.dart'; |
| 17 | 17 |
| 18 /// Wraps [input] to provide a timeout. If [input] completes before | 18 /// Wraps [input] to provide a timeout. If [input] completes before |
| 19 /// [milliseconds] have passed, then the return value completes in the same way. | 19 /// [milliseconds] have passed, then the return value completes in the same way. |
| 20 /// However, if [milliseconds] pass before [input] has completed, [onTimeout] is | 20 /// However, if [milliseconds] pass before [input] has completed, [onTimeout] is |
| 21 /// run and its result is passed to [input] (with chaining, if it returns a | 21 /// run and its result is passed to [input] (with chaining, if it returns a |
| 22 /// [Future]). | 22 /// [Future]). |
| 23 /// | 23 /// |
| 24 /// Note that timing out will not cancel the asynchronous operation behind | 24 /// Note that timing out will not cancel the asynchronous operation behind |
| 25 /// [input]. | 25 /// [input]. |
| 26 Future timeout(Future input, int milliseconds, onTimeout()) { | 26 Future timeout(Future input, int milliseconds, onTimeout()) { |
| 27 var completer = new Completer(); | 27 var completer = new Completer(); |
| 28 var timer = new Timer(new Duration(milliseconds: milliseconds), () { | 28 var timer = new Timer(new Duration(milliseconds: milliseconds), () { |
| 29 chainToCompleter(new Future.of(onTimeout), completer); | 29 chainToCompleter(new Future.sync(onTimeout), completer); |
| 30 }); | 30 }); |
| 31 input.then((value) { | 31 input.then((value) { |
| 32 if (completer.isCompleted) return; | 32 if (completer.isCompleted) return; |
| 33 timer.cancel(); | 33 timer.cancel(); |
| 34 completer.complete(value); | 34 completer.complete(value); |
| 35 }).catchError((error) { | 35 }).catchError((error) { |
| 36 if (completer.isCompleted) return; | 36 if (completer.isCompleted) return; |
| 37 timer.cancel(); | 37 timer.cancel(); |
| 38 completer.completeError(error); | 38 completer.completeError(error); |
| 39 }); | 39 }); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 52 /// Sets up a timeout for every metatest in this file. | 52 /// Sets up a timeout for every metatest in this file. |
| 53 void setUpTimeout() { | 53 void setUpTimeout() { |
| 54 metaSetUp(() { | 54 metaSetUp(() { |
| 55 // TODO(nweiz): We used to only increase the timeout to 10s for the Windows | 55 // TODO(nweiz): We used to only increase the timeout to 10s for the Windows |
| 56 // bots, but the Linux and Mac bots have started taking upwards of 5s when | 56 // bots, but the Linux and Mac bots have started taking upwards of 5s when |
| 57 // running pumpEventQueue, so we're increasing the timeout across the board | 57 // running pumpEventQueue, so we're increasing the timeout across the board |
| 58 // (see issue 9248). | 58 // (see issue 9248). |
| 59 currentSchedule.timeout = new Duration(seconds: 10); | 59 currentSchedule.timeout = new Duration(seconds: 10); |
| 60 }); | 60 }); |
| 61 } | 61 } |
| OLD | NEW |