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 "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 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 457 ..expectData(37, () { | 457 ..expectData(37, () { |
| 458 test.close(); | 458 test.close(); |
| 459 }) | 459 }) |
| 460 ..expectDone() | 460 ..expectDone() |
| 461 ..expectCancel(test.terminate); | 461 ..expectCancel(test.terminate); |
| 462 test.listen(); | 462 test.listen(); |
| 463 }); | 463 }); |
| 464 | 464 |
| 465 test("broadcast-controller-individual-pause", () { | 465 test("broadcast-controller-individual-pause", () { |
| 466 StreamProtocolTest test = new StreamProtocolTest.broadcast(); | 466 StreamProtocolTest test = new StreamProtocolTest.broadcast(); |
| 467 test.trace = true; | |
| 468 var sub1; | 467 var sub1; |
| 469 test..expectListen() | 468 test..expectListen() |
| 470 ..expectData(42) | 469 ..expectData(42) |
| 471 ..expectData(42, () { sub1.pause(); }) | 470 ..expectData(42, () { sub1.pause(); }) |
| 472 ..expectData(43, () { | 471 ..expectData(43, () { |
| 473 sub1.cancel(); | 472 sub1.cancel(); |
| 474 test.listen(); | 473 test.listen(); |
| 475 test.add(44); | 474 test.add(44); |
| 476 test.expectData(44); | 475 test.expectData(44); |
| 477 test.expectData(44, test.terminate); | 476 test.expectData(44, test.terminate); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 491 test.add(87); | 490 test.add(87); |
| 492 sub.cancel(); | 491 sub.cancel(); |
| 493 }); | 492 }); |
| 494 test.expectCancel(() { | 493 test.expectCancel(() { |
| 495 test.add(37); | 494 test.add(37); |
| 496 test.terminate(); | 495 test.terminate(); |
| 497 }); | 496 }); |
| 498 }); | 497 }); |
| 499 } | 498 } |
| 500 | 499 |
| 500 void testSink({bool sync, bool broadcast, bool asBroadcast}) { | |
| 501 String type = "${sync?"S":"A"}${broadcast?"B":"S"}${asBroadcast?"aB":""}"; | |
| 502 test("$type-controller-sink", () { | |
| 503 var done = expectAsync0((){}); | |
| 504 var c = broadcast ? new StreamController.broadcast(sync: sync) | |
| 505 : new StreamController(sync: sync); | |
| 506 var expected = new Events() | |
| 507 ..add(42)..error("error") | |
| 508 ..add(1)..add(2)..add(3)..add(4)..add(5) | |
| 509 ..add(43)..close(); | |
| 510 var actual = new Events.capture(asBroadcast ? c.stream.asBroadcastStream() | |
| 511 : c.stream); | |
| 512 var sink = c.sink; | |
| 513 sink.add(42); | |
| 514 sink.addError("error"); | |
| 515 sink.addStream(new Stream.fromIterable([1, 2, 3, 4, 5])) | |
| 516 .then((_) { | |
| 517 sink.add(43); | |
| 518 return sink.close(); | |
| 519 }) | |
| 520 .then((_) { | |
| 521 Expect.listEquals(expected.events, actual.events); | |
| 522 done(); | |
| 523 }); | |
| 524 }); | |
| 525 | |
| 526 test("$type-controller-sink-canceled", () { | |
| 527 var done = expectAsync0((){}); | |
| 528 var c = broadcast ? new StreamController.broadcast(sync: sync) | |
| 529 : new StreamController(sync: sync); | |
| 530 var expected = new Events() | |
| 531 ..add(42)..error("error") | |
| 532 ..add(1)..add(2)..add(3); | |
| 533 var stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; | |
| 534 var actual = new Events(); | |
| 535 var sub; | |
| 536 // Cancel subscription after receiving "3" event. | |
| 537 sub = stream.listen((v) { | |
| 538 if (v == 3) sub.cancel(); | |
| 539 actual.add(v); | |
| 540 }, onError: actual.error); | |
| 541 var sink = c.sink; | |
| 542 sink.add(42); | |
| 543 sink.addError("error"); | |
| 544 sink.addStream(new Stream.fromIterable([1, 2, 3, 4, 5])) | |
| 545 .then((_) { | |
| 546 Expect.listEquals(expected.events, actual.events); | |
| 547 // Close controller as well. It has no listener. If it is a broadcast | |
| 548 // stream, it will still be open, and we read the done" future before | |
|
floitsch
2013/06/27 15:15:19
spurious '"'.
Lasse Reichstein Nielsen
2013/06/28 12:57:38
Done.
| |
| 549 // closing. A normal stream is already done when its listener cancels. | |
| 550 Future doneFuture = sink.done; | |
| 551 sink.close(); | |
| 552 return doneFuture; | |
| 553 }) | |
| 554 .then((_) { | |
| 555 // No change in events. | |
| 556 Expect.listEquals(expected.events, actual.events); | |
| 557 done(); | |
| 558 }); | |
| 559 }); | |
| 560 | |
| 561 test("$type-controller-sink-paused", () { | |
| 562 var done = expectAsync0((){}); | |
| 563 var c = broadcast ? new StreamController.broadcast(sync: sync) | |
| 564 : new StreamController(sync: sync); | |
| 565 var expected = new Events() | |
| 566 ..add(42)..error("error") | |
| 567 ..add(1)..add(2)..add(3); | |
| 568 if (!asBroadcast) { | |
| 569 // If asBroadcast, the done future fires when the done event leaves | |
| 570 // the controller's subscription. It has no way to know when the | |
| 571 // broadcast-stream's listeners are done. | |
| 572 expected..add(4)..add(5)..add(43)..close(); | |
|
floitsch
2013/06/27 15:15:19
If I understand correctly the broadcast subscripti
| |
| 573 } | |
| 574 var stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; | |
| 575 var actual = new Events(); | |
| 576 var sub; | |
| 577 sub = stream.listen( | |
| 578 (v) { | |
| 579 if (v == 3) { | |
| 580 sub.pause(new Future.delayed(const Duration(milliseconds: 15), | |
| 581 () => null)); | |
|
floitsch
2013/06/27 15:15:19
bad indentation
Lasse Reichstein Nielsen
2013/06/28 12:57:38
Done.
| |
| 582 } | |
| 583 actual.add(v); | |
| 584 }, | |
| 585 onError: actual.error, | |
| 586 onDone: actual.close); | |
| 587 var sink = c.sink; | |
| 588 sink.add(42); | |
| 589 sink.addError("error"); | |
| 590 sink.addStream(new Stream.fromIterable([1, 2, 3, 4, 5])) | |
| 591 .then((_) { | |
| 592 sink.add(43); | |
| 593 return sink.close(); | |
| 594 }) | |
| 595 .then((_) { | |
|
Lasse Reichstein Nielsen
2013/06/28 12:57:38
I'll try adding a pause here (50 ms, just to be su
| |
| 596 Expect.listEquals(expected.events, actual.events); | |
| 597 done(); | |
| 598 }); | |
| 599 }); | |
| 600 } | |
| 601 | |
| 501 main() { | 602 main() { |
| 502 testController(); | 603 testController(); |
| 503 testSingleController(); | 604 testSingleController(); |
| 504 testExtraMethods(); | 605 testExtraMethods(); |
| 505 testPause(); | 606 testPause(); |
| 506 testRethrow(); | 607 testRethrow(); |
| 507 testBroadcastController(); | 608 testBroadcastController(); |
| 609 testSink(sync: true, broadcast: false, asBroadcast: false); | |
| 610 testSink(sync: true, broadcast: false, asBroadcast: true); | |
| 611 testSink(sync: true, broadcast: true, asBroadcast: false); | |
| 612 testSink(sync: false, broadcast: false, asBroadcast: false); | |
| 613 testSink(sync: false, broadcast: false, asBroadcast: true); | |
| 614 testSink(sync: false, broadcast: true, asBroadcast: false); | |
|
floitsch
2013/06/27 15:15:19
add test, when the addStream produces errors.
| |
| 508 } | 615 } |
| OLD | NEW |