Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: tests/lib/async/stream_controller_async_test.dart

Issue 16240008: Make StreamController be a StreamSink, not just an EventSink. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/lib/async/event_helper.dart ('k') | tests/lib/async/stream_controller_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
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 ..add(4)..add(5)..add(43)..close();
569 var stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream;
570 var actual = new Events();
571 var sub;
572 sub = stream.listen(
573 (v) {
574 if (v == 3) {
575 sub.pause(new Future.delayed(const Duration(milliseconds: 15),
576 () => null));
577 }
578 actual.add(v);
579 },
580 onError: actual.error,
581 onDone: actual.close);
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 if (asBroadcast) {
592 // The done-future of the sink completes when it passes
593 // the done event to the asBroadcastStream controller, which is
594 // before the final listener gets the event.
595 // Wait for the pause to end before testing the events.
596 return new Future.delayed(const Duration(milliseconds: 50), () {
597 Expect.listEquals(expected.events, actual.events);
598 done();
599 });
600 } else {
601 Expect.listEquals(expected.events, actual.events);
602 done();
603 }
604 });
605 });
606 }
607
501 main() { 608 main() {
502 testController(); 609 testController();
503 testSingleController(); 610 testSingleController();
504 testExtraMethods(); 611 testExtraMethods();
505 testPause(); 612 testPause();
506 testRethrow(); 613 testRethrow();
507 testBroadcastController(); 614 testBroadcastController();
615 testSink(sync: true, broadcast: false, asBroadcast: false);
616 testSink(sync: true, broadcast: false, asBroadcast: true);
617 testSink(sync: true, broadcast: true, asBroadcast: false);
618 testSink(sync: false, broadcast: false, asBroadcast: false);
619 testSink(sync: false, broadcast: false, asBroadcast: true);
620 testSink(sync: false, broadcast: true, asBroadcast: false);
508 } 621 }
OLDNEW
« no previous file with comments | « tests/lib/async/event_helper.dart ('k') | tests/lib/async/stream_controller_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698