Chromium Code Reviews| 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 'dart:async'; | 8 import 'dart:async'; |
| 9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
| 10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
| (...skipping 621 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 632 var done = expectAsync0((){}); | 632 var done = expectAsync0((){}); |
| 633 var c = broadcast ? new StreamController.broadcast(sync: sync) | 633 var c = broadcast ? new StreamController.broadcast(sync: sync) |
| 634 : new StreamController(sync: sync); | 634 : new StreamController(sync: sync); |
| 635 var expected = new Events() | 635 var expected = new Events() |
| 636 ..add(42)..error("error") | 636 ..add(42)..error("error") |
| 637 ..add(1)..add(2)..add(3) | 637 ..add(1)..add(2)..add(3) |
| 638 ..add(4)..add(5)..add(43)..close(); | 638 ..add(4)..add(5)..add(43)..close(); |
| 639 var stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; | 639 var stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; |
| 640 var actual = new Events(); | 640 var actual = new Events(); |
| 641 var sub; | 641 var sub; |
| 642 var pauseIsDone = false; | 642 var pauseIsDone = false; |
|
floitsch
2013/10/16 11:42:44
variable can be removed now.
| |
| 643 sub = stream.listen( | 643 sub = stream.listen( |
| 644 (v) { | 644 (v) { |
| 645 if (v == 3) { | 645 if (v == 3) { |
| 646 sub.pause(new Future.delayed(const Duration(milliseconds: 15), | 646 sub.pause(new Future.delayed(const Duration(milliseconds: 15), |
| 647 () { pauseIsDone = true; })); | 647 () { pauseIsDone = true; })); |
| 648 } | 648 } |
| 649 actual.add(v); | 649 actual.add(v); |
| 650 }, | 650 }, |
| 651 onError: actual.error, | 651 onError: actual.error, |
| 652 onDone: actual.close); | 652 onDone: actual.close); |
| 653 var sink = c.sink; | 653 var sink = c.sink; |
| 654 sink.add(42); | 654 sink.add(42); |
| 655 sink.addError("error"); | 655 sink.addError("error"); |
| 656 sink.addStream(new Stream.fromIterable([1, 2, 3, 4, 5])) | 656 sink.addStream(new Stream.fromIterable([1, 2, 3, 4, 5])) |
| 657 .then((_) { | 657 .then((_) { |
| 658 sink.add(43); | 658 sink.add(43); |
| 659 return sink.close(); | 659 return sink.close(); |
| 660 }) | 660 }) |
| 661 .then((_) { | 661 .then((_) { |
| 662 if (asBroadcast || broadcast) { | 662 if (asBroadcast || broadcast) { |
| 663 // The done-future of the sink completes when it passes | 663 // The done-future of the sink completes when it passes |
| 664 // the done event to the asBroadcastStream controller, which is | 664 // the done event to the asBroadcastStream controller, which is |
| 665 // before the final listener gets the event. | 665 // before the final listener gets the event. |
| 666 // Wait for the pause to end before testing the events. | 666 // Wait for the done event to be *delivered* before testing the |
| 667 dynamic executeWhenPauseIsDone(Function f) { | 667 // events. |
| 668 if (!pauseIsDone) { | 668 actual.onDone(() { |
| 669 return new Future.delayed(const Duration(milliseconds: 50), () { | |
| 670 return executeWhenPauseIsDone(f); | |
| 671 }); | |
| 672 } | |
| 673 return f(); | |
| 674 } | |
| 675 return executeWhenPauseIsDone(() { | |
| 676 Expect.listEquals(expected.events, actual.events); | 669 Expect.listEquals(expected.events, actual.events); |
| 677 done(); | 670 done(); |
| 678 }); | 671 }); |
| 679 } else { | 672 } else { |
| 680 Expect.listEquals(expected.events, actual.events); | 673 Expect.listEquals(expected.events, actual.events); |
| 681 done(); | 674 done(); |
| 682 } | 675 } |
| 683 }); | 676 }); |
| 684 }); | 677 }); |
| 685 } | 678 } |
| 686 | 679 |
| 687 main() { | 680 main() { |
| 688 testController(); | 681 testController(); |
| 689 testSingleController(); | 682 testSingleController(); |
| 690 testExtraMethods(); | 683 testExtraMethods(); |
| 691 testPause(); | 684 testPause(); |
| 692 testRethrow(); | 685 testRethrow(); |
| 693 testBroadcastController(); | 686 testBroadcastController(); |
| 694 testAsBroadcast(); | 687 testAsBroadcast(); |
| 695 testSink(sync: true, broadcast: false, asBroadcast: false); | 688 testSink(sync: true, broadcast: false, asBroadcast: false); |
| 696 testSink(sync: true, broadcast: false, asBroadcast: true); | 689 testSink(sync: true, broadcast: false, asBroadcast: true); |
| 697 testSink(sync: true, broadcast: true, asBroadcast: false); | 690 testSink(sync: true, broadcast: true, asBroadcast: false); |
| 698 testSink(sync: false, broadcast: false, asBroadcast: false); | 691 testSink(sync: false, broadcast: false, asBroadcast: false); |
| 699 testSink(sync: false, broadcast: false, asBroadcast: true); | 692 testSink(sync: false, broadcast: false, asBroadcast: true); |
| 700 testSink(sync: false, broadcast: true, asBroadcast: false); | 693 testSink(sync: false, broadcast: true, asBroadcast: false); |
| 701 } | 694 } |
| OLD | NEW |