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