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