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