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

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

Issue 301193010: Update documentation for "close". (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add tests. Don't throw if listening on broadcast stream after close. Created 6 years, 6 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
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_test; 6 library stream_controller_test;
7 7
8 import "package:expect/expect.dart"; 8 import "package:expect/expect.dart";
9 import "package:async_helper/async_helper.dart"; 9 import "package:async_helper/async_helper.dart";
10 import 'dart:async'; 10 import 'dart:async';
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 StreamController c = new StreamController(sync: true); 412 StreamController c = new StreamController(sync: true);
413 Expect.isFalse(c.isClosed); 413 Expect.isFalse(c.isClosed);
414 c.add(42); 414 c.add(42);
415 Expect.isFalse(c.isClosed); 415 Expect.isFalse(c.isClosed);
416 c.addError("bad"); 416 c.addError("bad");
417 Expect.isFalse(c.isClosed); 417 Expect.isFalse(c.isClosed);
418 c.close(); 418 c.close();
419 Expect.isTrue(c.isClosed); 419 Expect.isTrue(c.isClosed);
420 } 420 }
421 421
422 void testCloseFuture() {
423 asyncStart();
424 asyncStart();
425 var c = new StreamController();
426 var f = c.close();
427 Expect.isTrue(c.isClosed);
428 bool doneSeen = false;
429 f.then((_) {
430 Expect.isTrue(doneSeen);
431 asyncEnd();
432 });
433 // Only listen after a while.
434 new Timer(const Duration(milliseconds: 250), () {
435 c.stream.listen(null, onDone: () {
436 asyncEnd();
437 doneSeen = true;
438 });
439 });
440 }
441
442 void testCloseFuture2() {
443 asyncStart();
444 asyncStart();
445 var c = new StreamController.broadcast();
446 var f = c.close();
447 Expect.isTrue(c.isClosed);
448 bool doneSeen = false;
449 f.then((_) {
450 // Done future on broadcast stream can happen
451 // before a listener is added.
452 Expect.isFalse(doneSeen);
453 asyncEnd();
454 });
455 // Only listen after a while.
456 new Timer(const Duration(milliseconds: 250), () {
457 c.stream.listen(null, onDone: () {
458 doneSeen = true;
459 asyncEnd();
460 });
461 });
462 }
463
464 void testCloseFuture3() {
465 asyncStart();
466 var c = new StreamController.broadcast();
467 c..add(1)..add(2)..add(3)..add(4);
468 c.stream.listen(null).cancel();
469 var f = c.close();
470 Expect.isTrue(c.isClosed);
471 f.then((_) {
472 asyncEnd();
473 });
474 }
475
422 void testStreamEquals() { 476 void testStreamEquals() {
423 StreamController c; 477 StreamController c;
424 c = new StreamController(sync: false); 478 c = new StreamController(sync: false);
425 Expect.equals(c.stream, c.stream); 479 Expect.equals(c.stream, c.stream);
426 c = new StreamController(sync: true); 480 c = new StreamController(sync: true);
427 Expect.equals(c.stream, c.stream); 481 Expect.equals(c.stream, c.stream);
428 c = new StreamController(sync: false, onListen:(){}); 482 c = new StreamController(sync: false, onListen:(){});
429 Expect.equals(c.stream, c.stream); 483 Expect.equals(c.stream, c.stream);
430 c = new StreamController(sync: true, onListen:(){}); 484 c = new StreamController(sync: true, onListen:(){});
431 Expect.equals(c.stream, c.stream); 485 Expect.equals(c.stream, c.stream);
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 // Only the last error ends up here. 578 // Only the last error ends up here.
525 Expect.equals("ERROR1", e); 579 Expect.equals("ERROR1", e);
526 asyncEnd(); 580 asyncEnd();
527 }); 581 });
528 }); 582 });
529 var addDone = c.addStream(c2.stream); 583 var addDone = c.addStream(c2.stream);
530 addDone.catchError(fail).whenComplete(asyncEnd); // Error must not go here. 584 addDone.catchError(fail).whenComplete(asyncEnd); // Error must not go here.
531 c.done.catchError(fail).whenComplete(asyncEnd); // Error must not go here. 585 c.done.catchError(fail).whenComplete(asyncEnd); // Error must not go here.
532 } 586 }
533 587
588 void testBroadcastListenAfterClose() {
589 asyncStart();
590 StreamController c = new StreamController.broadcast();
591 var f = c.close();
592 f.then((_) {
593 // Listening after close is allowed. The listener gets a done event.
594 c.stream.listen(null, onDone: () {
595 asyncEnd();
596 });
597 });
598 }
599
534 main() { 600 main() {
535 asyncStart(); 601 asyncStart();
536 testMultiController(); 602 testMultiController();
537 testSingleController(); 603 testSingleController();
538 testExtraMethods(); 604 testExtraMethods();
539 testClosed(); 605 testClosed();
606 testCloseFuture();
607 testCloseFuture2();
608 testCloseFuture3();
540 testStreamEquals(); 609 testStreamEquals();
541 testCancelThrow(); 610 testCancelThrow();
542 testCancelThrow2(); 611 testCancelThrow2();
543 testCancelThrow3(); 612 testCancelThrow3();
613 testBroadcastListenAfterClose();
544 asyncEnd(); 614 asyncEnd();
545 } 615 }
OLDNEW
« sdk/lib/async/stream.dart ('K') | « tests/lib/async/stream_controller_async_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698