| 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 // Test the basic StreamController and StreamController.singleSubscription. | 5 // Test the basic StreamController and StreamController.singleSubscription. |
| 6 library stream_controller_async_test; | 6 library stream_controller_async_test; |
| 7 | 7 |
| 8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
| 9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
| (...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 ..expectData(37, () { | 459 ..expectData(37, () { |
| 460 test.close(); | 460 test.close(); |
| 461 }) | 461 }) |
| 462 ..expectDone() | 462 ..expectDone() |
| 463 ..expectCancel(test.terminate); | 463 ..expectCancel(test.terminate); |
| 464 test.listen(); | 464 test.listen(); |
| 465 }); | 465 }); |
| 466 | 466 |
| 467 test("broadcast-controller-individual-pause", () { | 467 test("broadcast-controller-individual-pause", () { |
| 468 StreamProtocolTest test = new StreamProtocolTest.broadcast(); | 468 StreamProtocolTest test = new StreamProtocolTest.broadcast(); |
| 469 test.trace = true; | |
| 470 var sub1; | 469 var sub1; |
| 471 test..expectListen() | 470 test..expectListen() |
| 472 ..expectData(42) | 471 ..expectData(42) |
| 473 ..expectData(42, () { sub1.pause(); }) | 472 ..expectData(42, () { sub1.pause(); }) |
| 474 ..expectData(43, () { | 473 ..expectData(43, () { |
| 475 sub1.cancel(); | 474 sub1.cancel(); |
| 476 test.listen(); | 475 test.listen(); |
| 477 test.add(44); | 476 test.add(44); |
| 478 test.expectData(44); | 477 test.expectData(44); |
| 479 test.expectData(44, test.terminate); | 478 test.expectData(44, test.terminate); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 ..expectResume(() { | 560 ..expectResume(() { |
| 562 test.close(); | 561 test.close(); |
| 563 }) | 562 }) |
| 564 ..expectDone() | 563 ..expectDone() |
| 565 ..expectBroadcastCancel() | 564 ..expectBroadcastCancel() |
| 566 ..expectCancel(test.terminate); | 565 ..expectCancel(test.terminate); |
| 567 sub = test.listen(); | 566 sub = test.listen(); |
| 568 }); | 567 }); |
| 569 } | 568 } |
| 570 | 569 |
| 570 void testSink({bool sync, bool broadcast, bool asBroadcast}) { |
| 571 String type = "${sync?"S":"A"}${broadcast?"B":"S"}${asBroadcast?"aB":""}"; |
| 572 test("$type-controller-sink", () { |
| 573 var done = expectAsync0((){}); |
| 574 var c = broadcast ? new StreamController.broadcast(sync: sync) |
| 575 : new StreamController(sync: sync); |
| 576 var expected = new Events() |
| 577 ..add(42)..error("error") |
| 578 ..add(1)..add(2)..add(3)..add(4)..add(5) |
| 579 ..add(43)..close(); |
| 580 var actual = new Events.capture(asBroadcast ? c.stream.asBroadcastStream() |
| 581 : c.stream); |
| 582 var sink = c.sink; |
| 583 sink.add(42); |
| 584 sink.addError("error"); |
| 585 sink.addStream(new Stream.fromIterable([1, 2, 3, 4, 5])) |
| 586 .then((_) { |
| 587 sink.add(43); |
| 588 return sink.close(); |
| 589 }) |
| 590 .then((_) { |
| 591 Expect.listEquals(expected.events, actual.events); |
| 592 done(); |
| 593 }); |
| 594 }); |
| 595 |
| 596 test("$type-controller-sink-canceled", () { |
| 597 var done = expectAsync0((){}); |
| 598 var c = broadcast ? new StreamController.broadcast(sync: sync) |
| 599 : new StreamController(sync: sync); |
| 600 var expected = new Events() |
| 601 ..add(42)..error("error") |
| 602 ..add(1)..add(2)..add(3); |
| 603 var stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; |
| 604 var actual = new Events(); |
| 605 var sub; |
| 606 // Cancel subscription after receiving "3" event. |
| 607 sub = stream.listen((v) { |
| 608 if (v == 3) sub.cancel(); |
| 609 actual.add(v); |
| 610 }, onError: actual.error); |
| 611 var sink = c.sink; |
| 612 sink.add(42); |
| 613 sink.addError("error"); |
| 614 sink.addStream(new Stream.fromIterable([1, 2, 3, 4, 5])) |
| 615 .then((_) { |
| 616 Expect.listEquals(expected.events, actual.events); |
| 617 // Close controller as well. It has no listener. If it is a broadcast |
| 618 // stream, it will still be open, and we read the "done" future before |
| 619 // closing. A normal stream is already done when its listener cancels. |
| 620 Future doneFuture = sink.done; |
| 621 sink.close(); |
| 622 return doneFuture; |
| 623 }) |
| 624 .then((_) { |
| 625 // No change in events. |
| 626 Expect.listEquals(expected.events, actual.events); |
| 627 done(); |
| 628 }); |
| 629 }); |
| 630 |
| 631 test("$type-controller-sink-paused", () { |
| 632 var done = expectAsync0((){}); |
| 633 var c = broadcast ? new StreamController.broadcast(sync: sync) |
| 634 : new StreamController(sync: sync); |
| 635 var expected = new Events() |
| 636 ..add(42)..error("error") |
| 637 ..add(1)..add(2)..add(3) |
| 638 ..add(4)..add(5)..add(43)..close(); |
| 639 var stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; |
| 640 var actual = new Events(); |
| 641 var sub; |
| 642 sub = stream.listen( |
| 643 (v) { |
| 644 if (v == 3) { |
| 645 sub.pause(new Future.delayed(const Duration(milliseconds: 15), |
| 646 () => null)); |
| 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) { |
| 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 pause to end before testing the events. |
| 666 return new Future.delayed(const Duration(milliseconds: 50), () { |
| 667 Expect.listEquals(expected.events, actual.events); |
| 668 done(); |
| 669 }); |
| 670 } else { |
| 671 Expect.listEquals(expected.events, actual.events); |
| 672 done(); |
| 673 } |
| 674 }); |
| 675 }); |
| 676 } |
| 677 |
| 571 main() { | 678 main() { |
| 572 testController(); | 679 testController(); |
| 573 testSingleController(); | 680 testSingleController(); |
| 574 testExtraMethods(); | 681 testExtraMethods(); |
| 575 testPause(); | 682 testPause(); |
| 576 testRethrow(); | 683 testRethrow(); |
| 577 testBroadcastController(); | 684 testBroadcastController(); |
| 578 testAsBroadcast(); | 685 testAsBroadcast(); |
| 686 testSink(sync: true, broadcast: false, asBroadcast: false); |
| 687 testSink(sync: true, broadcast: false, asBroadcast: true); |
| 688 testSink(sync: true, broadcast: true, asBroadcast: false); |
| 689 testSink(sync: false, broadcast: false, asBroadcast: false); |
| 690 testSink(sync: false, broadcast: false, asBroadcast: true); |
| 691 testSink(sync: false, broadcast: true, asBroadcast: false); |
| 579 } | 692 } |
| OLD | NEW |