OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, 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 future_timeout_test; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'package:unittest/unittest.dart'; |
| 9 |
| 10 main() { |
| 11 test("timeoutNoComplete", () { |
| 12 Completer completer = new Completer(); |
| 13 Future timedOut = completer.future.timeout( |
| 14 const Duration(milliseconds: 5), onTimeout: () => 42); |
| 15 timedOut.then(expectAsync((v) { |
| 16 expect(v, 42); |
| 17 })); |
| 18 }); |
| 19 |
| 20 test("timeoutCompleteAfterTimeout", () { |
| 21 Completer completer = new Completer(); |
| 22 Future timedOut = completer.future.timeout( |
| 23 const Duration(milliseconds: 5), onTimeout: () => 42); |
| 24 Timer timer = new Timer(const Duration(seconds: 1), () { |
| 25 completer.complete(-1); |
| 26 }); |
| 27 timedOut.then(expectAsync((v) { |
| 28 expect(v, 42); |
| 29 })); |
| 30 }); |
| 31 |
| 32 test("timeoutCompleteBeforeTimeout", () { |
| 33 Completer completer = new Completer(); |
| 34 Timer timer = new Timer(const Duration(milliseconds: 5), () { |
| 35 completer.complete(42); |
| 36 }); |
| 37 Future timedOut = completer.future.timeout( |
| 38 const Duration(seconds: 1), onTimeout: () => -1); |
| 39 timedOut.then(expectAsync((v) { |
| 40 expect(v, 42); |
| 41 })); |
| 42 }); |
| 43 |
| 44 test("timeoutCompleteBeforeCreate", () { |
| 45 Completer completer = new Completer.sync(); |
| 46 completer.complete(42); |
| 47 Future timedOut = completer.future.timeout( |
| 48 const Duration(milliseconds: 5), onTimeout: () => -1); |
| 49 timedOut.then(expectAsync((v) { |
| 50 expect(v, 42); |
| 51 })); |
| 52 }); |
| 53 |
| 54 test("timeoutThrows", () { |
| 55 Completer completer = new Completer(); |
| 56 Future timedOut = completer.future.timeout( |
| 57 const Duration(milliseconds: 5), onTimeout: () { throw "EXN1"; }); |
| 58 timedOut.catchError(expectAsync((e, s) { |
| 59 expect(e, "EXN1"); |
| 60 })); |
| 61 }); |
| 62 |
| 63 test("timeoutThrowAfterTimeout", () { |
| 64 Completer completer = new Completer(); |
| 65 Future timedOut = completer.future.timeout( |
| 66 const Duration(milliseconds: 5), onTimeout: () => 42); |
| 67 Timer timer = new Timer(const Duration(seconds: 1), () { |
| 68 completer.completeError("EXN2"); |
| 69 }); |
| 70 timedOut.then(expectAsync((v) { |
| 71 expect(v, 42); |
| 72 })); |
| 73 }); |
| 74 |
| 75 test("timeoutThrowBeforeTimeout", () { |
| 76 Completer completer = new Completer(); |
| 77 Timer timer = new Timer(const Duration(milliseconds: 5), () { |
| 78 completer.completeError("EXN3"); |
| 79 }); |
| 80 Future timedOut = completer.future.timeout( |
| 81 const Duration(seconds: 1), onTimeout: () => -1); |
| 82 timedOut.catchError(expectAsync((e, s) { |
| 83 expect(e, "EXN3"); |
| 84 })); |
| 85 }); |
| 86 |
| 87 test("timeoutThrowBeforeCreate", () { |
| 88 // Prevent uncaught error when we create the error. |
| 89 Completer completer = new Completer.sync()..future.catchError((e){}); |
| 90 completer.completeError("EXN4"); |
| 91 Future timedOut = completer.future.timeout( |
| 92 const Duration(milliseconds: 5), onTimeout: () => -1); |
| 93 timedOut.catchError(expectAsync((e, s) { |
| 94 expect(e, "EXN4"); |
| 95 })); |
| 96 }); |
| 97 |
| 98 test("timeoutReturnFutureValue", () { |
| 99 Future result = new Future.value(42); |
| 100 Completer completer = new Completer(); |
| 101 Future timedOut = completer.future.timeout( |
| 102 const Duration(milliseconds: 5), onTimeout: () => result); |
| 103 timedOut.then(expectAsync((v) { |
| 104 expect(v, 42); |
| 105 })); |
| 106 }); |
| 107 |
| 108 test("timeoutReturnFutureError", () { |
| 109 Future result = new Future.error("EXN5")..catchError((e){}); |
| 110 Completer completer = new Completer(); |
| 111 Future timedOut = completer.future.timeout( |
| 112 const Duration(milliseconds: 5), onTimeout: () => result); |
| 113 timedOut.catchError(expectAsync((e, s) { |
| 114 expect(e, "EXN5"); |
| 115 })); |
| 116 }); |
| 117 |
| 118 test("timeoutReturnFutureValueLater", () { |
| 119 Completer result = new Completer(); |
| 120 Completer completer = new Completer(); |
| 121 Future timedOut = completer.future.timeout( |
| 122 const Duration(milliseconds: 5), onTimeout: () { |
| 123 result.complete(42); |
| 124 return result.future; |
| 125 }); |
| 126 timedOut.then(expectAsync((v) { |
| 127 expect(v, 42); |
| 128 })); |
| 129 }); |
| 130 |
| 131 test("timeoutReturnFutureErrorLater", () { |
| 132 Completer result = new Completer(); |
| 133 Completer completer = new Completer(); |
| 134 Future timedOut = completer.future.timeout( |
| 135 const Duration(milliseconds: 5), onTimeout: () { |
| 136 result.completeError("EXN6"); |
| 137 return result.future; |
| 138 }); |
| 139 timedOut.catchError(expectAsync((e, s) { |
| 140 expect(e, "EXN6"); |
| 141 })); |
| 142 }); |
| 143 |
| 144 test("timeoutZone", () { |
| 145 var initialZone = Zone.current; |
| 146 Zone forked; |
| 147 int registerCallDelta = 0; |
| 148 bool callbackCalled = false; |
| 149 Function callback = () { |
| 150 expect(callbackCalled, false); |
| 151 callbackCalled = true; |
| 152 expect(Zone.current, forked); |
| 153 return 42; |
| 154 }; |
| 155 forked = Zone.current.fork(specification: new ZoneSpecification( |
| 156 registerCallback: (Zone self, ZoneDelegate parent, Zone origin, f()) { |
| 157 if (!identical(f, callback)) return f; |
| 158 registerCallDelta++; // Increment calls to register. |
| 159 expect(origin, forked); |
| 160 expect(self, forked); |
| 161 return expectAsync(() { registerCallDelta--; return f(); }); |
| 162 } |
| 163 )); |
| 164 Completer completer = new Completer(); |
| 165 Future timedOut; |
| 166 forked.run(() { |
| 167 timedOut = completer.future.timeout(const Duration(milliseconds: 5), |
| 168 onTimeout: callback); |
| 169 }); |
| 170 timedOut.then(expectAsync((v) { |
| 171 expect(callbackCalled, true); |
| 172 expect(registerCallDelta, 0); |
| 173 expect(Zone.current, initialZone); |
| 174 expect(v, 42); |
| 175 })); |
| 176 }); |
| 177 |
| 178 test("timeoutNoFunction", () { |
| 179 Completer completer = new Completer(); |
| 180 Future timedOut = completer.future.timeout( |
| 181 const Duration(milliseconds: 5)); |
| 182 timedOut.catchError(expectAsync((e, s) { |
| 183 expect(e, new isInstanceOf<TimeoutException>()); |
| 184 expect(e.duration, const Duration(milliseconds: 5)); |
| 185 expect(s, null); |
| 186 })); |
| 187 }); |
| 188 |
| 189 test("timeoutType", () { |
| 190 Completer completer = new Completer<int>(); |
| 191 Future timedOut = completer.future.timeout( |
| 192 const Duration(milliseconds: 5)); |
| 193 expect(timedOut, new isInstanceOf<Future<int>>()); |
| 194 expect(timedOut, isNot(new isInstanceOf<Future<String>>())); |
| 195 timedOut.catchError((_) {}); |
| 196 completer.complete(499); |
| 197 }); |
| 198 } |
OLD | NEW |