| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library test_utils; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:scheduled_test/scheduled_test.dart'; | |
| 10 import 'package:scheduled_test/src/utils.dart'; | |
| 11 import 'package:scheduled_test/src/mock_clock.dart' as mock_clock; | |
| 12 | |
| 13 import 'package:metatest/metatest.dart'; | |
| 14 | |
| 15 export 'package:scheduled_test/src/utils.dart'; | |
| 16 | |
| 17 /// Wraps [input] to provide a timeout. If [input] completes before | |
| 18 /// [milliseconds] have passed, then the return value completes in the same way. | |
| 19 /// However, if [milliseconds] pass before [input] has completed, [onTimeout] is | |
| 20 /// run and its result is passed to [input] (with chaining, if it returns a | |
| 21 /// [Future]). | |
| 22 /// | |
| 23 /// Note that timing out will not cancel the asynchronous operation behind | |
| 24 /// [input]. | |
| 25 Future timeout(Future input, int milliseconds, onTimeout()) { | |
| 26 var completer = new Completer(); | |
| 27 var timer = new Timer(new Duration(milliseconds: milliseconds), () { | |
| 28 chainToCompleter(syncFuture(onTimeout), completer); | |
| 29 }); | |
| 30 input.then((value) { | |
| 31 if (completer.isCompleted) return; | |
| 32 timer.cancel(); | |
| 33 completer.complete(value); | |
| 34 }).catchError((error, stackTrace) { | |
| 35 if (completer.isCompleted) return; | |
| 36 timer.cancel(); | |
| 37 completer.completeError(error, stackTrace); | |
| 38 }); | |
| 39 return completer.future; | |
| 40 } | |
| 41 | |
| 42 /// Returns a [Future] that will complete in [milliseconds]. | |
| 43 Future sleep(int milliseconds) { | |
| 44 var completer = new Completer(); | |
| 45 mock_clock.newTimer(new Duration(milliseconds: milliseconds), () { | |
| 46 completer.complete(); | |
| 47 }); | |
| 48 return completer.future; | |
| 49 } | |
| 50 | |
| 51 /// Sets up a timeout for every metatest in this file. | |
| 52 void setUpTimeout() { | |
| 53 metaSetUp(() { | |
| 54 // TODO(nweiz): We used to only increase the timeout to 10s for the Windows | |
| 55 // bots, but the Linux and Mac bots have started taking upwards of 5s when | |
| 56 // running pumpEventQueue, so we're increasing the timeout across the board | |
| 57 // (see issue 9248). | |
| 58 currentSchedule.timeout = new Duration(seconds: 10); | |
| 59 }); | |
| 60 } | |
| 61 | |
| 62 /// Creates a metatest with [body] and asserts that it passes. | |
| 63 /// | |
| 64 /// This is like [expectTestsPass], but the [test] is set up automatically. | |
| 65 void expectTestPasses(String description, body()) => | |
| 66 expectTestsPass(description, () => test('test', body)); | |
| 67 | |
| 68 /// Creates a metatest that runs [testBody], captures its schedule errors, and | |
| 69 /// passes them to [validator]. | |
| 70 /// | |
| 71 /// [testBody] is expected to produce an error, while [validator] is expected to | |
| 72 /// produce none. | |
| 73 void expectTestFails(String description, Future testBody(), | |
| 74 void validator(List<ScheduleError> errors)) { | |
| 75 expectTestsPass(description, () { | |
| 76 var errors; | |
| 77 test('test body', () { | |
| 78 currentSchedule.onException.schedule(() { | |
| 79 errors = currentSchedule.errors; | |
| 80 }); | |
| 81 | |
| 82 testBody(); | |
| 83 }); | |
| 84 | |
| 85 test('validate errors', () { | |
| 86 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 87 validator(errors); | |
| 88 }); | |
| 89 }, passing: ['validate errors']); | |
| 90 } | |
| OLD | NEW |