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 // Test the basic StreamController and StreamController.broadcast. |
| 6 library stream_controller_async_test; |
| 7 |
| 8 import 'dart:async'; |
| 9 import "package:expect/expect.dart"; |
| 10 import 'package:unittest/unittest.dart'; |
| 11 import 'event_helper.dart'; |
| 12 import 'stream_state_helper.dart'; |
| 13 |
| 14 void cancelSub(StreamSubscription sub) { sub.cancel(); } |
| 15 |
| 16 testController() { |
| 17 // Test fold |
| 18 test("StreamController.fold", () { |
| 19 StreamController c = new StreamController(); |
| 20 Stream stream = c.stream.asBroadcastStream(onCancel: cancelSub); |
| 21 stream.fold(0, (a,b) => a + b) |
| 22 .then(expectAsync((int v) { |
| 23 Expect.equals(42, v); |
| 24 })); |
| 25 c.add(10); |
| 26 c.add(32); |
| 27 c.close(); |
| 28 }); |
| 29 |
| 30 test("StreamController.fold throws", () { |
| 31 StreamController c = new StreamController(); |
| 32 Stream stream = c.stream.asBroadcastStream(onCancel: cancelSub); |
| 33 stream.fold(0, (a,b) { throw "Fnyf!"; }) |
| 34 .catchError(expectAsync((error) { Expect.equals("Fnyf!", error); })); |
| 35 c.add(42); |
| 36 }); |
| 37 } |
| 38 |
| 39 testSingleController() { |
| 40 test("Single-subscription StreamController.fold", () { |
| 41 StreamController c = new StreamController(); |
| 42 Stream stream = c.stream; |
| 43 stream.fold(0, (a,b) => a + b) |
| 44 .then(expectAsync((int v) { Expect.equals(42, v); })); |
| 45 c.add(10); |
| 46 c.add(32); |
| 47 c.close(); |
| 48 }); |
| 49 |
| 50 test("Single-subscription StreamController.fold throws", () { |
| 51 StreamController c = new StreamController(); |
| 52 Stream stream = c.stream; |
| 53 stream.fold(0, (a,b) { throw "Fnyf!"; }) |
| 54 .catchError(expectAsync((e) { Expect.equals("Fnyf!", e); })); |
| 55 c.add(42); |
| 56 }); |
| 57 |
| 58 test("Single-subscription StreamController events are buffered when" |
| 59 " there is no subscriber", |
| 60 () { |
| 61 StreamController c = new StreamController(); |
| 62 EventSink sink = c.sink; |
| 63 Stream stream = c.stream; |
| 64 int counter = 0; |
| 65 sink.add(1); |
| 66 sink.add(2); |
| 67 sink.close(); |
| 68 stream.listen( |
| 69 (data) { |
| 70 counter += data; |
| 71 }, |
| 72 onDone: expectAsync(() { |
| 73 Expect.equals(3, counter); |
| 74 })); |
| 75 }); |
| 76 } |
| 77 |
| 78 testExtraMethods() { |
| 79 Events sentEvents = new Events()..add(7)..add(9)..add(13)..add(87)..close(); |
| 80 |
| 81 test("forEach", () { |
| 82 StreamController c = new StreamController(); |
| 83 Events actualEvents = new Events(); |
| 84 Future f = c.stream.forEach(actualEvents.add); |
| 85 f.then(expectAsync((_) { |
| 86 actualEvents.close(); |
| 87 Expect.listEquals(sentEvents.events, actualEvents.events); |
| 88 })); |
| 89 sentEvents.replay(c); |
| 90 }); |
| 91 |
| 92 test("forEachError", () { |
| 93 Events sentEvents = new Events()..add(7)..error("bad")..add(87)..close(); |
| 94 StreamController c = new StreamController(); |
| 95 Events actualEvents = new Events(); |
| 96 Future f = c.stream.forEach(actualEvents.add); |
| 97 f.catchError(expectAsync((error) { |
| 98 Expect.equals("bad", error); |
| 99 Expect.listEquals((new Events()..add(7)).events, actualEvents.events); |
| 100 })); |
| 101 sentEvents.replay(c); |
| 102 }); |
| 103 |
| 104 test("forEachError2", () { |
| 105 Events sentEvents = new Events()..add(7)..add(9)..add(87)..close(); |
| 106 StreamController c = new StreamController(); |
| 107 Events actualEvents = new Events(); |
| 108 Future f = c.stream.forEach((x) { |
| 109 if (x == 9) throw "bad"; |
| 110 actualEvents.add(x); |
| 111 }); |
| 112 f.catchError(expectAsync((error) { |
| 113 Expect.equals("bad", error); |
| 114 Expect.listEquals((new Events()..add(7)).events, actualEvents.events); |
| 115 })); |
| 116 sentEvents.replay(c); |
| 117 }); |
| 118 |
| 119 test("firstWhere", () { |
| 120 StreamController c = new StreamController(); |
| 121 Future f = c.stream.firstWhere((x) => (x % 3) == 0); |
| 122 f.then(expectAsync((v) { Expect.equals(9, v); })); |
| 123 sentEvents.replay(c); |
| 124 }); |
| 125 |
| 126 test("firstWhere 2", () { |
| 127 StreamController c = new StreamController(); |
| 128 Future f = c.stream.firstWhere((x) => (x % 4) == 0); |
| 129 f.catchError(expectAsync((e) {})); |
| 130 sentEvents.replay(c); |
| 131 }); |
| 132 |
| 133 test("firstWhere 3", () { |
| 134 StreamController c = new StreamController(); |
| 135 Future f = c.stream.firstWhere((x) => (x % 4) == 0, defaultValue: () => 999)
; |
| 136 f.then(expectAsync((v) { Expect.equals(999, v); })); |
| 137 sentEvents.replay(c); |
| 138 }); |
| 139 |
| 140 |
| 141 test("lastWhere", () { |
| 142 StreamController c = new StreamController(); |
| 143 Future f = c.stream.lastWhere((x) => (x % 3) == 0); |
| 144 f.then(expectAsync((v) { Expect.equals(87, v); })); |
| 145 sentEvents.replay(c); |
| 146 }); |
| 147 |
| 148 test("lastWhere 2", () { |
| 149 StreamController c = new StreamController(); |
| 150 Future f = c.stream.lastWhere((x) => (x % 4) == 0); |
| 151 f.catchError(expectAsync((e) {})); |
| 152 sentEvents.replay(c); |
| 153 }); |
| 154 |
| 155 test("lastWhere 3", () { |
| 156 StreamController c = new StreamController(); |
| 157 Future f = c.stream.lastWhere((x) => (x % 4) == 0, defaultValue: () => 999); |
| 158 f.then(expectAsync((v) { Expect.equals(999, v); })); |
| 159 sentEvents.replay(c); |
| 160 }); |
| 161 |
| 162 test("singleWhere", () { |
| 163 StreamController c = new StreamController(); |
| 164 Future f = c.stream.singleWhere((x) => (x % 9) == 0); |
| 165 f.then(expectAsync((v) { Expect.equals(9, v); })); |
| 166 sentEvents.replay(c); |
| 167 }); |
| 168 |
| 169 test("singleWhere 2", () { |
| 170 StreamController c = new StreamController(); |
| 171 Future f = c.stream.singleWhere((x) => (x % 3) == 0); // Matches 9 and 87.. |
| 172 f.catchError(expectAsync((error) { Expect.isTrue(error is StateError); })); |
| 173 sentEvents.replay(c); |
| 174 }); |
| 175 |
| 176 test("first", () { |
| 177 StreamController c = new StreamController(); |
| 178 Future f = c.stream.first; |
| 179 f.then(expectAsync((v) { Expect.equals(7, v);})); |
| 180 sentEvents.replay(c); |
| 181 }); |
| 182 |
| 183 test("first empty", () { |
| 184 StreamController c = new StreamController(); |
| 185 Future f = c.stream.first; |
| 186 f.catchError(expectAsync((error) { Expect.isTrue(error is StateError); })); |
| 187 Events emptyEvents = new Events()..close(); |
| 188 emptyEvents.replay(c); |
| 189 }); |
| 190 |
| 191 test("first error", () { |
| 192 StreamController c = new StreamController(); |
| 193 Future f = c.stream.first; |
| 194 f.catchError(expectAsync((error) { Expect.equals("error", error); })); |
| 195 Events errorEvents = new Events()..error("error")..close(); |
| 196 errorEvents.replay(c); |
| 197 }); |
| 198 |
| 199 test("first error 2", () { |
| 200 StreamController c = new StreamController(); |
| 201 Future f = c.stream.first; |
| 202 f.catchError(expectAsync((error) { Expect.equals("error", error); })); |
| 203 Events errorEvents = new Events()..error("error")..error("error2")..close(); |
| 204 errorEvents.replay(c); |
| 205 }); |
| 206 |
| 207 test("last", () { |
| 208 StreamController c = new StreamController(); |
| 209 Future f = c.stream.last; |
| 210 f.then(expectAsync((v) { Expect.equals(87, v);})); |
| 211 sentEvents.replay(c); |
| 212 }); |
| 213 |
| 214 test("last empty", () { |
| 215 StreamController c = new StreamController(); |
| 216 Future f = c.stream.last; |
| 217 f.catchError(expectAsync((error) { Expect.isTrue(error is StateError); })); |
| 218 Events emptyEvents = new Events()..close(); |
| 219 emptyEvents.replay(c); |
| 220 }); |
| 221 |
| 222 test("last error", () { |
| 223 StreamController c = new StreamController(); |
| 224 Future f = c.stream.last; |
| 225 f.catchError(expectAsync((error) { Expect.equals("error", error); })); |
| 226 Events errorEvents = new Events()..error("error")..close(); |
| 227 errorEvents.replay(c); |
| 228 }); |
| 229 |
| 230 test("last error 2", () { |
| 231 StreamController c = new StreamController(); |
| 232 Future f = c.stream.last; |
| 233 f.catchError(expectAsync((error) { Expect.equals("error", error); })); |
| 234 Events errorEvents = new Events()..error("error")..error("error2")..close(); |
| 235 errorEvents.replay(c); |
| 236 }); |
| 237 |
| 238 test("elementAt", () { |
| 239 StreamController c = new StreamController(); |
| 240 Future f = c.stream.elementAt(2); |
| 241 f.then(expectAsync((v) { Expect.equals(13, v);})); |
| 242 sentEvents.replay(c); |
| 243 }); |
| 244 |
| 245 test("elementAt 2", () { |
| 246 StreamController c = new StreamController(); |
| 247 Future f = c.stream.elementAt(20); |
| 248 f.catchError(expectAsync((error) { Expect.isTrue(error is RangeError); })); |
| 249 sentEvents.replay(c); |
| 250 }); |
| 251 |
| 252 test("drain", () { |
| 253 StreamController c = new StreamController(); |
| 254 Future f = c.stream.drain(); |
| 255 f.then(expectAsync((v) { Expect.equals(null, v);})); |
| 256 sentEvents.replay(c); |
| 257 }); |
| 258 |
| 259 test("drain error", () { |
| 260 StreamController c = new StreamController(); |
| 261 Future f = c.stream.drain(); |
| 262 f.catchError(expectAsync((error) { Expect.equals("error", error); })); |
| 263 Events errorEvents = new Events()..error("error")..error("error2")..close(); |
| 264 errorEvents.replay(c); |
| 265 }); |
| 266 |
| 267 } |
| 268 |
| 269 testPause() { |
| 270 test("pause event-unpause", () { |
| 271 |
| 272 StreamProtocolTest test = new StreamProtocolTest(); |
| 273 Completer completer = new Completer(); |
| 274 test..expectListen() |
| 275 ..expectData(42, () { test.pause(completer.future); }) |
| 276 ..expectPause(() { |
| 277 completer.complete(null); |
| 278 }) |
| 279 ..expectData(43) |
| 280 ..expectData(44) |
| 281 ..expectCancel() |
| 282 ..expectDone(test.terminate); |
| 283 test.listen(); |
| 284 test.add(42); |
| 285 test.add(43); |
| 286 test.add(44); |
| 287 test.close(); |
| 288 }); |
| 289 |
| 290 test("pause twice event-unpause", () { |
| 291 StreamProtocolTest test = new StreamProtocolTest(); |
| 292 Completer completer = new Completer(); |
| 293 Completer completer2 = new Completer(); |
| 294 test..expectListen() |
| 295 ..expectData(42, () { |
| 296 test.pause(completer.future); |
| 297 test.pause(completer2.future); |
| 298 }) |
| 299 ..expectPause(() { |
| 300 completer.future.then(completer2.complete); |
| 301 completer.complete(null); |
| 302 }) |
| 303 ..expectData(43) |
| 304 ..expectData(44) |
| 305 ..expectCancel() |
| 306 ..expectDone(test.terminate); |
| 307 test..listen() |
| 308 ..add(42) |
| 309 ..add(43) |
| 310 ..add(44) |
| 311 ..close(); |
| 312 }); |
| 313 |
| 314 test("pause twice direct-unpause", () { |
| 315 StreamProtocolTest test = new StreamProtocolTest(); |
| 316 test..expectListen() |
| 317 ..expectData(42, () { |
| 318 test.pause(); |
| 319 test.pause(); |
| 320 }) |
| 321 ..expectPause(() { |
| 322 test.resume(); |
| 323 test.resume(); |
| 324 }) |
| 325 ..expectData(43) |
| 326 ..expectData(44) |
| 327 ..expectCancel() |
| 328 ..expectDone(test.terminate); |
| 329 test..listen() |
| 330 ..add(42) |
| 331 ..add(43) |
| 332 ..add(44) |
| 333 ..close(); |
| 334 }); |
| 335 |
| 336 test("pause twice direct-event-unpause", () { |
| 337 StreamProtocolTest test = new StreamProtocolTest(); |
| 338 Completer completer = new Completer(); |
| 339 test..expectListen() |
| 340 ..expectData(42, () { |
| 341 test.pause(); |
| 342 test.pause(completer.future); |
| 343 test.add(43); |
| 344 test.add(44); |
| 345 test.close(); |
| 346 }) |
| 347 ..expectPause(() { |
| 348 completer.future.then((v) => test.resume()); |
| 349 completer.complete(null); |
| 350 }) |
| 351 ..expectData(43) |
| 352 ..expectData(44) |
| 353 ..expectCancel() |
| 354 ..expectDone(test.terminate); |
| 355 test..listen() |
| 356 ..add(42); |
| 357 }); |
| 358 } |
| 359 |
| 360 class TestError { const TestError(); } |
| 361 |
| 362 testRethrow() { |
| 363 TestError error = const TestError(); |
| 364 |
| 365 testStream(name, streamValueTransform) { |
| 366 test("rethrow-$name-value", () { |
| 367 StreamController c = new StreamController(); |
| 368 Stream s = streamValueTransform(c.stream, (v) { throw error; }); |
| 369 s.listen((_) { Expect.fail("unexpected value"); }, onError: expectAsync( |
| 370 (e) { Expect.identical(error, e); })); |
| 371 c.add(null); |
| 372 c.close(); |
| 373 }); |
| 374 } |
| 375 |
| 376 testStreamError(name, streamErrorTransform) { |
| 377 test("rethrow-$name-error", () { |
| 378 StreamController c = new StreamController(); |
| 379 Stream s = streamErrorTransform(c.stream, (e) { throw error; }); |
| 380 s.listen((_) { Expect.fail("unexpected value"); }, onError: expectAsync( |
| 381 (e) { Expect.identical(error, e); })); |
| 382 c.addError("SOME ERROR"); |
| 383 c.close(); |
| 384 }); |
| 385 } |
| 386 |
| 387 testFuture(name, streamValueTransform) { |
| 388 test("rethrow-$name-value", () { |
| 389 StreamController c = new StreamController(); |
| 390 Future f = streamValueTransform(c.stream, (v) { throw error; }); |
| 391 f.then((v) { Expect.fail("unreachable"); }, |
| 392 onError: expectAsync((e) { Expect.identical(error, e); })); |
| 393 // Need two values to trigger compare for reduce. |
| 394 c.add(0); |
| 395 c.add(1); |
| 396 c.close(); |
| 397 }); |
| 398 } |
| 399 |
| 400 testStream("where", (s, act) => s.where(act)); |
| 401 testStream("map", (s, act) => s.map(act)); |
| 402 testStream("expand", (s, act) => s.expand(act)); |
| 403 testStream("where", (s, act) => s.where(act)); |
| 404 testStreamError("handleError", (s, act) => s.handleError(act)); |
| 405 testStreamError("handleTest", (s, act) => s.handleError((v) {}, test: act)); |
| 406 testFuture("forEach", (s, act) => s.forEach(act)); |
| 407 testFuture("every", (s, act) => s.every(act)); |
| 408 testFuture("any", (s, act) => s.any(act)); |
| 409 testFuture("reduce", (s, act) => s.reduce((a,b) => act(b))); |
| 410 testFuture("fold", (s, act) => s.fold(0, (a,b) => act(b))); |
| 411 testFuture("drain", (s, act) => s.drain().then(act)); |
| 412 } |
| 413 |
| 414 void testBroadcastController() { |
| 415 test("broadcast-controller-basic", () { |
| 416 StreamProtocolTest test = new StreamProtocolTest.broadcast(); |
| 417 test..expectListen() |
| 418 ..expectData(42) |
| 419 ..expectCancel() |
| 420 ..expectDone(test.terminate); |
| 421 test..listen() |
| 422 ..add(42) |
| 423 ..close(); |
| 424 }); |
| 425 |
| 426 test("broadcast-controller-listen-twice", () { |
| 427 StreamProtocolTest test = new StreamProtocolTest.broadcast(); |
| 428 test..expectListen() |
| 429 ..expectData(42, () { |
| 430 test.listen(); |
| 431 test.add(37); |
| 432 test.close(); |
| 433 }) |
| 434 // Order is not guaranteed between subscriptions if not sync. |
| 435 ..expectData(37) |
| 436 ..expectData(37) |
| 437 ..expectDone() |
| 438 ..expectCancel() |
| 439 ..expectDone(test.terminate); |
| 440 test.listen(); |
| 441 test.add(42); |
| 442 }); |
| 443 |
| 444 test("broadcast-controller-listen-twice-non-overlap", () { |
| 445 StreamProtocolTest test = new StreamProtocolTest.broadcast(); |
| 446 test |
| 447 ..expectListen(() { |
| 448 test.add(42); |
| 449 }) |
| 450 ..expectData(42, () { |
| 451 test.cancel(); |
| 452 }) |
| 453 ..expectCancel(() { |
| 454 test.listen(); |
| 455 })..expectListen(() { |
| 456 test.add(37); |
| 457 }) |
| 458 ..expectData(37, () { |
| 459 test.close(); |
| 460 }) |
| 461 ..expectCancel() |
| 462 ..expectDone(test.terminate); |
| 463 test.listen(); |
| 464 }); |
| 465 |
| 466 test("broadcast-controller-individual-pause", () { |
| 467 StreamProtocolTest test = new StreamProtocolTest.broadcast(); |
| 468 var sub1; |
| 469 test..expectListen() |
| 470 ..expectData(42) |
| 471 ..expectData(42, () { sub1.pause(); }) |
| 472 ..expectData(43, () { |
| 473 sub1.cancel(); |
| 474 test.listen(); |
| 475 test.add(44); |
| 476 test.expectData(44); |
| 477 test.expectData(44, test.terminate); |
| 478 }); |
| 479 sub1 = test.listen(); |
| 480 test.listen(); |
| 481 test.add(42); |
| 482 test.add(43); |
| 483 }); |
| 484 |
| 485 test("broadcast-controller-add-in-callback", () { |
| 486 StreamProtocolTest test = new StreamProtocolTest.broadcast(); |
| 487 test.expectListen(); |
| 488 var sub = test.listen(); |
| 489 test.add(42); |
| 490 sub.expectData(42, () { |
| 491 test.add(87); |
| 492 sub.cancel(); |
| 493 }); |
| 494 test.expectCancel(() { |
| 495 test.add(37); |
| 496 test.terminate(); |
| 497 }); |
| 498 }); |
| 499 } |
| 500 |
| 501 void testAsBroadcast() { |
| 502 test("asBroadcast-not-canceled", () { |
| 503 StreamProtocolTest test = new StreamProtocolTest.asBroadcast(); |
| 504 var sub; |
| 505 test..expectListen() |
| 506 ..expectBroadcastListen((_) { |
| 507 test.add(42); |
| 508 }) |
| 509 ..expectData(42, () { |
| 510 sub.cancel(); |
| 511 }) |
| 512 ..expectBroadcastCancel((_) { |
| 513 sub = test.listen(); |
| 514 }) |
| 515 ..expectBroadcastListen((_) { |
| 516 test.terminate(); |
| 517 }); |
| 518 sub = test.listen(); |
| 519 }); |
| 520 |
| 521 test("asBroadcast-canceled", () { |
| 522 StreamProtocolTest test = new StreamProtocolTest.asBroadcast(); |
| 523 var sub; |
| 524 test..expectListen() |
| 525 ..expectBroadcastListen((_) { |
| 526 test.add(42); |
| 527 }) |
| 528 ..expectData(42, () { |
| 529 sub.cancel(); |
| 530 }) |
| 531 ..expectBroadcastCancel((originalSub) { |
| 532 originalSub.cancel(); |
| 533 }) |
| 534 ..expectCancel(test.terminate); |
| 535 sub = test.listen(); |
| 536 }); |
| 537 |
| 538 test("asBroadcast-pause-original", () { |
| 539 StreamProtocolTest test = new StreamProtocolTest.asBroadcast(); |
| 540 var sub; |
| 541 test..expectListen() |
| 542 ..expectBroadcastListen((_) { |
| 543 test.add(42); |
| 544 test.add(43); |
| 545 }) |
| 546 ..expectData(42, () { |
| 547 sub.cancel(); |
| 548 }) |
| 549 ..expectBroadcastCancel((originalSub) { |
| 550 originalSub.pause(); // Pause before sending 43 from original sub. |
| 551 }) |
| 552 ..expectPause(() { |
| 553 sub = test.listen(); |
| 554 }) |
| 555 ..expectBroadcastListen((originalSub) { |
| 556 originalSub.resume(); |
| 557 }) |
| 558 ..expectData(43) |
| 559 ..expectResume(() { |
| 560 test.close(); |
| 561 }) |
| 562 ..expectCancel() |
| 563 ..expectDone() |
| 564 ..expectBroadcastCancel((_) => test.terminate()); |
| 565 sub = test.listen(); |
| 566 }); |
| 567 } |
| 568 |
| 569 void testSink({bool sync, bool broadcast, bool asBroadcast}) { |
| 570 String type = "${sync?"S":"A"}${broadcast?"B":"S"}${asBroadcast?"aB":""}"; |
| 571 test("$type-controller-sink", () { |
| 572 var done = expectAsync((){}); |
| 573 var c = broadcast ? new StreamController.broadcast(sync: sync) |
| 574 : new StreamController(sync: sync); |
| 575 var expected = new Events() |
| 576 ..add(42)..error("error") |
| 577 ..add(1)..add(2)..add(3)..add(4)..add(5) |
| 578 ..add(43)..close(); |
| 579 var actual = new Events.capture(asBroadcast ? c.stream.asBroadcastStream() |
| 580 : c.stream); |
| 581 var sink = c.sink; |
| 582 sink.add(42); |
| 583 sink.addError("error"); |
| 584 sink.addStream(new Stream.fromIterable([1, 2, 3, 4, 5])) |
| 585 .then((_) { |
| 586 sink.add(43); |
| 587 return sink.close(); |
| 588 }) |
| 589 .then((_) { |
| 590 Expect.listEquals(expected.events, actual.events); |
| 591 done(); |
| 592 }); |
| 593 }); |
| 594 |
| 595 test("$type-controller-sink-canceled", () { |
| 596 var done = expectAsync((){}); |
| 597 var c = broadcast ? new StreamController.broadcast(sync: sync) |
| 598 : new StreamController(sync: sync); |
| 599 var expected = new Events() |
| 600 ..add(42)..error("error") |
| 601 ..add(1)..add(2)..add(3); |
| 602 var stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; |
| 603 var actual = new Events(); |
| 604 var sub; |
| 605 // Cancel subscription after receiving "3" event. |
| 606 sub = stream.listen((v) { |
| 607 if (v == 3) sub.cancel(); |
| 608 actual.add(v); |
| 609 }, onError: actual.error); |
| 610 var sink = c.sink; |
| 611 sink.add(42); |
| 612 sink.addError("error"); |
| 613 sink.addStream(new Stream.fromIterable([1, 2, 3, 4, 5])) |
| 614 .then((_) { |
| 615 Expect.listEquals(expected.events, actual.events); |
| 616 // Close controller as well. It has no listener. If it is a broadcast |
| 617 // stream, it will still be open, and we read the "done" future before |
| 618 // closing. A normal stream is already done when its listener cancels. |
| 619 Future doneFuture = sink.done; |
| 620 sink.close(); |
| 621 return doneFuture; |
| 622 }) |
| 623 .then((_) { |
| 624 // No change in events. |
| 625 Expect.listEquals(expected.events, actual.events); |
| 626 done(); |
| 627 }); |
| 628 }); |
| 629 |
| 630 test("$type-controller-sink-paused", () { |
| 631 var done = expectAsync((){}); |
| 632 var c = broadcast ? new StreamController.broadcast(sync: sync) |
| 633 : new StreamController(sync: sync); |
| 634 var expected = new Events() |
| 635 ..add(42)..error("error") |
| 636 ..add(1)..add(2)..add(3) |
| 637 ..add(4)..add(5)..add(43)..close(); |
| 638 var stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; |
| 639 var actual = new Events(); |
| 640 var sub; |
| 641 var pauseIsDone = false; |
| 642 sub = stream.listen( |
| 643 (v) { |
| 644 if (v == 3) { |
| 645 sub.pause(new Future.delayed(const Duration(milliseconds: 15), |
| 646 () { pauseIsDone = true; })); |
| 647 } |
| 648 actual.add(v); |
| 649 }, |
| 650 onError: actual.error, |
| 651 onDone: actual.close); |
| 652 var sink = c.sink; |
| 653 sink.add(42); |
| 654 sink.addError("error"); |
| 655 sink.addStream(new Stream.fromIterable([1, 2, 3, 4, 5])) |
| 656 .then((_) { |
| 657 sink.add(43); |
| 658 return sink.close(); |
| 659 }) |
| 660 .then((_) { |
| 661 if (asBroadcast || broadcast) { |
| 662 // The done-future of the sink completes when it passes |
| 663 // the done event to the asBroadcastStream controller, which is |
| 664 // before the final listener gets the event. |
| 665 // Wait for the done event to be *delivered* before testing the |
| 666 // events. |
| 667 actual.onDone(() { |
| 668 Expect.listEquals(expected.events, actual.events); |
| 669 done(); |
| 670 }); |
| 671 } else { |
| 672 Expect.listEquals(expected.events, actual.events); |
| 673 done(); |
| 674 } |
| 675 }); |
| 676 }); |
| 677 |
| 678 test("$type-controller-addstream-error-stop", () { |
| 679 // Check that addStream defaults to ending after the first error. |
| 680 var done = expectAsync((){}); |
| 681 StreamController c = broadcast |
| 682 ? new StreamController.broadcast(sync: sync) |
| 683 : new StreamController(sync: sync); |
| 684 Stream stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; |
| 685 var actual = new Events.capture(stream); |
| 686 |
| 687 var source = new Events(); |
| 688 source..add(1)..add(2)..error("BAD")..add(3)..error("FAIL")..close(); |
| 689 |
| 690 var expected = new Events()..add(1)..add(2)..error("BAD")..close(); |
| 691 StreamController sourceController = new StreamController(); |
| 692 c.addStream(sourceController.stream).then((_) { |
| 693 c.close().then((_) { |
| 694 Expect.listEquals(expected.events, actual.events); |
| 695 done(); |
| 696 }); |
| 697 }); |
| 698 |
| 699 source.replay(sourceController); |
| 700 }); |
| 701 |
| 702 test("$type-controller-addstream-error-forward", () { |
| 703 // Check that addStream with cancelOnError:false passes all data and errors |
| 704 // to the controller. |
| 705 var done = expectAsync((){}); |
| 706 StreamController c = broadcast |
| 707 ? new StreamController.broadcast(sync: sync) |
| 708 : new StreamController(sync: sync); |
| 709 Stream stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; |
| 710 var actual = new Events.capture(stream); |
| 711 |
| 712 var source = new Events(); |
| 713 source..add(1)..add(2)..addError("BAD")..add(3)..addError("FAIL")..close(); |
| 714 |
| 715 StreamController sourceController = new StreamController(); |
| 716 c.addStream(sourceController.stream, cancelOnError: false).then((_) { |
| 717 c.close().then((_) { |
| 718 Expect.listEquals(source.events, actual.events); |
| 719 done(); |
| 720 }); |
| 721 }); |
| 722 |
| 723 source.replay(sourceController); |
| 724 }); |
| 725 |
| 726 test("$type-controller-addstream-twice", () { |
| 727 // Using addStream twice on the same stream |
| 728 var done = expectAsync((){}); |
| 729 StreamController c = broadcast |
| 730 ? new StreamController.broadcast(sync: sync) |
| 731 : new StreamController(sync: sync); |
| 732 Stream stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; |
| 733 var actual = new Events.capture(stream); |
| 734 |
| 735 // Streams of five events, throws on 3. |
| 736 Stream s1 = new Stream.fromIterable([1,2,3,4,5]) |
| 737 .map((x) => (x == 3 ? throw x : x)); |
| 738 Stream s2 = new Stream.fromIterable([1,2,3,4,5]) |
| 739 .map((x) => (x == 3 ? throw x : x)); |
| 740 |
| 741 Events expected = new Events(); |
| 742 expected..add(1)..add(2)..error(3); |
| 743 expected..add(1)..add(2)..error(3)..add(4)..add(5); |
| 744 expected..close(); |
| 745 |
| 746 c.addStream(s1).then((_) { |
| 747 c.addStream(s2, cancelOnError: false).then((_) { |
| 748 c.close().then((_) { |
| 749 Expect.listEquals(expected.events, actual.events); |
| 750 done(); |
| 751 }); |
| 752 }); |
| 753 }); |
| 754 }); |
| 755 } |
| 756 |
| 757 main() { |
| 758 testController(); |
| 759 testSingleController(); |
| 760 testExtraMethods(); |
| 761 testPause(); |
| 762 testRethrow(); |
| 763 testBroadcastController(); |
| 764 testAsBroadcast(); |
| 765 testSink(sync: true, broadcast: false, asBroadcast: false); |
| 766 testSink(sync: true, broadcast: false, asBroadcast: true); |
| 767 testSink(sync: true, broadcast: true, asBroadcast: false); |
| 768 testSink(sync: false, broadcast: false, asBroadcast: false); |
| 769 testSink(sync: false, broadcast: false, asBroadcast: true); |
| 770 testSink(sync: false, broadcast: true, asBroadcast: false); |
| 771 } |
OLD | NEW |