OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // Test the StreamSubscription.cancel return Future. | 5 // Test the StreamSubscription.cancel return Future. |
6 library stream_subscription_cancel; | 6 library stream_subscription_cancel; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
10 | 10 |
11 void main() { | 11 void main() { |
12 test('subscription.cancel', () { | 12 test('subscription.cancel', () { |
13 var completer = new Completer(); | 13 var completer = new Completer(); |
14 StreamController controller = new StreamController( | 14 StreamController controller = new StreamController( |
15 onCancel: () => completer.future); | 15 onCancel: () => completer.future); |
16 | 16 |
17 bool done = false; | 17 bool done = false; |
18 expect(controller.stream.listen(null).cancel().then((_) => done), | 18 expect(controller.stream.listen(null).cancel().then((_) => done), |
19 completion(equals(true))); | 19 completion(equals(true))); |
20 | 20 |
21 Timer.run(() { | 21 Timer.run(() { |
22 done = true; | 22 done = true; |
23 completer.complete(); | 23 completer.complete(); |
24 }); | 24 }); |
25 }); | 25 }); |
26 | 26 |
27 test('subscription.cancel after close', () { | 27 test('subscription.cancel after close', () { |
28 var completer = new Completer(); | 28 var completer = new Completer(); |
29 StreamController controller = new StreamController( | 29 StreamController controller = new StreamController( |
30 onCancel: () { | 30 onCancel: completer.complete); |
31 completer.complete(); | |
32 return completer.future; | |
33 }); | |
34 | 31 |
35 controller.close(); | 32 controller.close(); |
36 | 33 |
37 var completer2 = new Completer(); | |
38 var sub; | 34 var sub; |
39 void onDone() { | 35 void onDone() { |
40 sub.cancel().then(completer2.complete); | 36 expect(sub.cancel(), isNull); |
41 } | 37 } |
42 | 38 |
43 sub = controller.stream.listen(null, onDone: onDone); | 39 sub = controller.stream.listen(null, onDone: onDone); |
44 expect(completer.future, completes); | 40 expect(completer.future, completes); |
45 expect(completer2.future, completes); | |
46 }); | 41 }); |
47 | 42 |
48 test('subscription.cancel after error', () { | 43 test('subscription.cancel after error', () { |
49 var completer = new Completer(); | 44 var completer = new Completer(); |
50 StreamController controller = new StreamController( | 45 StreamController controller = new StreamController( |
51 onCancel: () => completer.future); | 46 onCancel: () => completer.future); |
52 | 47 |
53 controller.addError("error"); | 48 controller.addError("error"); |
54 | 49 |
55 bool done = false; | 50 bool done = false; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 controller.close(); | 128 controller.close(); |
134 | 129 |
135 void onDone() { | 130 void onDone() { |
136 fail("onDone is unexpected"); | 131 fail("onDone is unexpected"); |
137 } | 132 } |
138 controller.stream | 133 controller.stream |
139 .listen(null, onDone: onDone) | 134 .listen(null, onDone: onDone) |
140 .cancel(); | 135 .cancel(); |
141 expect(doneCompleter.future, completion(equals(true))); | 136 expect(doneCompleter.future, completion(equals(true))); |
142 }); | 137 }); |
143 | |
144 test('subscription.cancel through map', () { | |
145 var completer = new Completer(); | |
146 StreamController controller = new StreamController( | |
147 onCancel: () => completer.future); | |
148 | |
149 bool done = false; | |
150 var future = controller.stream.map((x) => x).listen(null).cancel(); | |
151 | |
152 expect(future.then((_) => done = true), completion(equals(true))); | |
153 | |
154 Timer.run(() { | |
155 expect(done, isFalse); | |
156 completer.complete(); | |
157 }); | |
158 }); | |
159 | |
160 test('subscription.cancel through asyncMap', () { | |
161 var completer = new Completer(); | |
162 StreamController controller = new StreamController( | |
163 onCancel: () => completer.future); | |
164 | |
165 bool done = false; | |
166 var future = controller.stream.asyncMap((x) => x).listen(null).cancel(); | |
167 | |
168 expect(future.then((_) => done = true), completion(equals(true))); | |
169 | |
170 Timer.run(() { | |
171 expect(done, isFalse); | |
172 completer.complete(); | |
173 }); | |
174 }); | |
175 | |
176 test('subscription.cancel through asyncExpand', () { | |
177 var completer = new Completer(); | |
178 StreamController controller = new StreamController( | |
179 onCancel: () => completer.future); | |
180 | |
181 bool done = false; | |
182 var future = controller.stream.asyncExpand((x) => x).listen(null).cancel(); | |
183 | |
184 expect(future.then((_) => done = true), completion(equals(true))); | |
185 | |
186 Timer.run(() { | |
187 expect(done, isFalse); | |
188 completer.complete(); | |
189 }); | |
190 }); | |
191 | |
192 test('subscription.cancel through handleError', () { | |
193 var completer = new Completer(); | |
194 StreamController controller = new StreamController( | |
195 onCancel: () => completer.future); | |
196 | |
197 bool done = false; | |
198 var future = controller.stream.handleError((x) => x).listen(null).cancel(); | |
199 | |
200 expect(future.then((_) => done = true), completion(equals(true))); | |
201 | |
202 Timer.run(() { | |
203 expect(done, isFalse); | |
204 completer.complete(); | |
205 }); | |
206 }); | |
207 | |
208 test('subscription.cancel through skip', () { | |
209 var completer = new Completer(); | |
210 StreamController controller = new StreamController( | |
211 onCancel: () => completer.future); | |
212 | |
213 bool done = false; | |
214 var future = controller.stream.skip(1).listen(null).cancel(); | |
215 | |
216 expect(future.then((_) => done = true), completion(equals(true))); | |
217 | |
218 Timer.run(() { | |
219 expect(done, isFalse); | |
220 completer.complete(); | |
221 }); | |
222 }); | |
223 | |
224 test('subscription.cancel through take', () { | |
225 var completer = new Completer(); | |
226 StreamController controller = new StreamController( | |
227 onCancel: () => completer.future); | |
228 | |
229 bool done = false; | |
230 var future = controller.stream.take(1).listen(null).cancel(); | |
231 | |
232 expect(future.then((_) => done = true), completion(equals(true))); | |
233 | |
234 Timer.run(() { | |
235 expect(done, isFalse); | |
236 completer.complete(); | |
237 }); | |
238 }); | |
239 | |
240 test('subscription.cancel through skipWhile', () { | |
241 var completer = new Completer(); | |
242 StreamController controller = new StreamController( | |
243 onCancel: () => completer.future); | |
244 | |
245 bool done = false; | |
246 var future = controller.stream.skipWhile((x) => true).listen(null).cancel(); | |
247 | |
248 expect(future.then((_) => done = true), completion(equals(true))); | |
249 | |
250 Timer.run(() { | |
251 expect(done, isFalse); | |
252 completer.complete(); | |
253 }); | |
254 }); | |
255 | |
256 test('subscription.cancel through takeWhile', () { | |
257 var completer = new Completer(); | |
258 StreamController controller = new StreamController( | |
259 onCancel: () => completer.future); | |
260 | |
261 bool done = false; | |
262 var future = controller.stream.takeWhile((x) => true).listen(null).cancel(); | |
263 | |
264 expect(future.then((_) => done = true), completion(equals(true))); | |
265 | |
266 Timer.run(() { | |
267 expect(done, isFalse); | |
268 completer.complete(); | |
269 }); | |
270 }); | |
271 | |
272 test('subscription.cancel through timeOut', () { | |
273 var completer = new Completer(); | |
274 StreamController controller = new StreamController( | |
275 onCancel: () => completer.future); | |
276 | |
277 bool done = false; | |
278 var duration = const Duration(hours: 5); | |
279 var future = controller.stream.timeout(duration).listen(null).cancel(); | |
280 | |
281 expect(future.then((_) => done = true), completion(equals(true))); | |
282 | |
283 Timer.run(() { | |
284 expect(done, isFalse); | |
285 completer.complete(); | |
286 }); | |
287 }); | |
288 | |
289 test('subscription.cancel through transform', () { | |
290 var completer = new Completer(); | |
291 StreamController controller = new StreamController( | |
292 onCancel: () => completer.future); | |
293 | |
294 bool done = false; | |
295 var transformer = | |
296 new StreamTransformer.fromHandlers(handleData: (x, y) {}); | |
297 var future = controller.stream.transform(transformer).listen(null).cancel(); | |
298 | |
299 expect(future.then((_) => done = true), completion(equals(true))); | |
300 | |
301 Timer.run(() { | |
302 expect(done, isFalse); | |
303 completer.complete(); | |
304 }); | |
305 }); | |
306 | |
307 test('subscription.cancel through where', () { | |
308 var completer = new Completer(); | |
309 StreamController controller = new StreamController( | |
310 onCancel: () => completer.future); | |
311 | |
312 bool done = false; | |
313 var future = controller.stream.where((x) => true).listen(null).cancel(); | |
314 | |
315 expect(future.then((_) => done = true), completion(equals(true))); | |
316 | |
317 Timer.run(() { | |
318 expect(done, isFalse); | |
319 completer.complete(); | |
320 }); | |
321 }); | |
322 } | 138 } |
OLD | NEW |