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