OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // Test the StreamSubscription.cancel return Future. |
| 6 library stream_subscription_cancel; |
| 7 |
| 8 import 'dart:async'; |
| 9 import 'package:unittest/unittest.dart'; |
| 10 |
| 11 void main() { |
| 12 test('subscription.cancel', () { |
| 13 var completer = new Completer(); |
| 14 StreamController controller = new StreamController( |
| 15 onCancel: () => completer.future); |
| 16 |
| 17 bool done = false; |
| 18 expect(controller.stream.listen(null).cancel().then((_) => done), |
| 19 completion(equals(true))); |
| 20 |
| 21 Timer.run(() { |
| 22 done = true; |
| 23 completer.complete(); |
| 24 }); |
| 25 }); |
| 26 |
| 27 test('subscription.cancel after close', () { |
| 28 var completer = new Completer(); |
| 29 StreamController controller = new StreamController( |
| 30 onCancel: () { |
| 31 completer.complete(); |
| 32 return completer.future; |
| 33 }); |
| 34 |
| 35 controller.close(); |
| 36 |
| 37 var completer2 = new Completer(); |
| 38 var sub; |
| 39 void onDone() { |
| 40 sub.cancel().then(completer2.complete); |
| 41 } |
| 42 |
| 43 sub = controller.stream.listen(null, onDone: onDone); |
| 44 expect(completer.future, completes); |
| 45 expect(completer2.future, completes); |
| 46 }); |
| 47 |
| 48 test('subscription.cancel after error', () { |
| 49 var completer = new Completer(); |
| 50 StreamController controller = new StreamController( |
| 51 onCancel: () => completer.future); |
| 52 |
| 53 controller.addError("error"); |
| 54 |
| 55 bool done = false; |
| 56 |
| 57 var subscription; |
| 58 var doneCompleter = new Completer(); |
| 59 void onError(e) { |
| 60 subscription.cancel().then((_) => doneCompleter.complete(done)); |
| 61 done = true; |
| 62 completer.complete(); |
| 63 } |
| 64 subscription = controller.stream.listen(null, onError: onError); |
| 65 expect(doneCompleter.future, completion(equals(true))); |
| 66 }); |
| 67 |
| 68 test('subscription.cancel after error (cancelOnError)', () { |
| 69 bool called = false; |
| 70 StreamController controller = new StreamController( |
| 71 onCancel: () { |
| 72 called = true; |
| 73 }); |
| 74 |
| 75 controller.addError("error"); |
| 76 |
| 77 var doneCompleter = new Completer(); |
| 78 void onError(e) { |
| 79 expect(called, equals(true)); |
| 80 doneCompleter.complete(true); |
| 81 } |
| 82 controller.stream.listen(null, onError: onError, cancelOnError: true); |
| 83 expect(doneCompleter.future, completion(equals(true))); |
| 84 }); |
| 85 |
| 86 test('subscription.cancel before error (cancelOnError)', () { |
| 87 var doneCompleter = new Completer(); |
| 88 StreamController controller = new StreamController( |
| 89 onCancel: () { |
| 90 doneCompleter.complete(true); |
| 91 }); |
| 92 |
| 93 controller.addError("error"); |
| 94 |
| 95 void onError(e) { |
| 96 fail("onError is unexpected"); |
| 97 } |
| 98 controller.stream |
| 99 .listen(null, onError: onError, cancelOnError: true) |
| 100 .cancel(); |
| 101 expect(doneCompleter.future, completion(equals(true))); |
| 102 }); |
| 103 |
| 104 test('subscription.cancel on error (cancelOnError)', () { |
| 105 bool called = false; |
| 106 StreamController controller = new StreamController( |
| 107 onCancel: () { |
| 108 expect(called, isFalse); |
| 109 called = true; |
| 110 }); |
| 111 |
| 112 controller.addError("error"); |
| 113 |
| 114 var doneCompleter = new Completer(); |
| 115 var sub; |
| 116 void onError(e) { |
| 117 expect(called, equals(true)); |
| 118 sub.cancel(); |
| 119 doneCompleter.complete(true); |
| 120 } |
| 121 sub = controller.stream |
| 122 .listen(null, onError: onError, cancelOnError: true); |
| 123 expect(doneCompleter.future, completion(equals(true))); |
| 124 }); |
| 125 |
| 126 test('subscription.cancel before done', () { |
| 127 var doneCompleter = new Completer(); |
| 128 StreamController controller = new StreamController( |
| 129 onCancel: () { |
| 130 doneCompleter.complete(true); |
| 131 }); |
| 132 |
| 133 controller.close(); |
| 134 |
| 135 void onDone() { |
| 136 fail("onDone is unexpected"); |
| 137 } |
| 138 controller.stream |
| 139 .listen(null, onDone: onDone) |
| 140 .cancel(); |
| 141 expect(doneCompleter.future, completion(equals(true))); |
| 142 }); |
| 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 } |
OLD | NEW |