| 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_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'; |
| 11 import 'event_helper.dart'; | 11 import 'event_helper.dart'; |
| 12 | 12 |
| 13 const MS = const Duration(milliseconds: 1); |
| 14 |
| 13 fail(e) { Expect.fail("Unexepected error: $e"); } | 15 fail(e) { Expect.fail("Unexepected error: $e"); } |
| 14 | 16 |
| 15 void testMultiController() { | 17 void testMultiController() { |
| 16 // Test normal flow. | 18 // Test normal flow. |
| 17 var c = new StreamController(sync: true); | 19 var c = new StreamController(sync: true); |
| 18 Events expectedEvents = new Events() | 20 Events expectedEvents = new Events() |
| 19 ..add(42) | 21 ..add(42) |
| 20 ..add("dibs") | 22 ..add("dibs") |
| 21 ..error("error!") | 23 ..error("error!") |
| 22 ..error("error too!") | 24 ..error("error too!") |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 StreamController c = new StreamController(sync: true); | 414 StreamController c = new StreamController(sync: true); |
| 413 Expect.isFalse(c.isClosed); | 415 Expect.isFalse(c.isClosed); |
| 414 c.add(42); | 416 c.add(42); |
| 415 Expect.isFalse(c.isClosed); | 417 Expect.isFalse(c.isClosed); |
| 416 c.addError("bad"); | 418 c.addError("bad"); |
| 417 Expect.isFalse(c.isClosed); | 419 Expect.isFalse(c.isClosed); |
| 418 c.close(); | 420 c.close(); |
| 419 Expect.isTrue(c.isClosed); | 421 Expect.isTrue(c.isClosed); |
| 420 } | 422 } |
| 421 | 423 |
| 424 void testCloseFuture() { |
| 425 asyncStart(); |
| 426 asyncStart(); |
| 427 var c = new StreamController(); |
| 428 var f = c.close(); |
| 429 Expect.isTrue(c.isClosed); |
| 430 bool doneSeen = false; |
| 431 f.then((_) { |
| 432 Expect.isTrue(doneSeen); |
| 433 asyncEnd(); |
| 434 }); |
| 435 // Only listen after a while. |
| 436 new Timer(MS * 250, () { |
| 437 c.stream.listen(null, onDone: () { |
| 438 asyncEnd(); |
| 439 doneSeen = true; |
| 440 }); |
| 441 }); |
| 442 } |
| 443 |
| 444 void testCloseFuture2() { |
| 445 asyncStart(); |
| 446 asyncStart(); |
| 447 var c = new StreamController.broadcast(); |
| 448 var f = c.close(); |
| 449 Expect.isTrue(c.isClosed); |
| 450 bool doneSeen = false; |
| 451 f.then((_) { |
| 452 // Done future on broadcast stream can happen |
| 453 // before a listener is added. |
| 454 Expect.isFalse(doneSeen); |
| 455 asyncEnd(); |
| 456 }); |
| 457 // Only listen after a while. |
| 458 new Timer(MS * 250, () { |
| 459 c.stream.listen(null, onDone: () { |
| 460 doneSeen = true; |
| 461 asyncEnd(); |
| 462 }); |
| 463 }); |
| 464 } |
| 465 |
| 466 void testCloseFuture3() { |
| 467 asyncStart(); |
| 468 var c = new StreamController.broadcast(); |
| 469 c..add(1)..add(2)..add(3)..add(4); |
| 470 c.stream.listen(null).cancel(); |
| 471 var f = c.close(); |
| 472 Expect.isTrue(c.isClosed); |
| 473 f.then((_) { |
| 474 asyncEnd(); |
| 475 }); |
| 476 } |
| 477 |
| 422 void testStreamEquals() { | 478 void testStreamEquals() { |
| 423 StreamController c; | 479 StreamController c; |
| 424 c = new StreamController(sync: false); | 480 c = new StreamController(sync: false); |
| 425 Expect.equals(c.stream, c.stream); | 481 Expect.equals(c.stream, c.stream); |
| 426 c = new StreamController(sync: true); | 482 c = new StreamController(sync: true); |
| 427 Expect.equals(c.stream, c.stream); | 483 Expect.equals(c.stream, c.stream); |
| 428 c = new StreamController(sync: false, onListen:(){}); | 484 c = new StreamController(sync: false, onListen:(){}); |
| 429 Expect.equals(c.stream, c.stream); | 485 Expect.equals(c.stream, c.stream); |
| 430 c = new StreamController(sync: true, onListen:(){}); | 486 c = new StreamController(sync: true, onListen:(){}); |
| 431 Expect.equals(c.stream, c.stream); | 487 Expect.equals(c.stream, c.stream); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 // Only the last error ends up here. | 580 // Only the last error ends up here. |
| 525 Expect.equals("ERROR1", e); | 581 Expect.equals("ERROR1", e); |
| 526 asyncEnd(); | 582 asyncEnd(); |
| 527 }); | 583 }); |
| 528 }); | 584 }); |
| 529 var addDone = c.addStream(c2.stream); | 585 var addDone = c.addStream(c2.stream); |
| 530 addDone.catchError(fail).whenComplete(asyncEnd); // Error must not go here. | 586 addDone.catchError(fail).whenComplete(asyncEnd); // Error must not go here. |
| 531 c.done.catchError(fail).whenComplete(asyncEnd); // Error must not go here. | 587 c.done.catchError(fail).whenComplete(asyncEnd); // Error must not go here. |
| 532 } | 588 } |
| 533 | 589 |
| 590 void testBroadcastListenAfterClose() { |
| 591 asyncStart(); |
| 592 StreamController c = new StreamController.broadcast(); |
| 593 var f = c.close(); |
| 594 f.then((_) { |
| 595 // Listening after close is allowed. The listener gets a done event. |
| 596 c.stream.listen(null, onDone: asyncEnd); |
| 597 }); |
| 598 } |
| 599 |
| 600 void testBroadcastListenAfterClosePaused() { |
| 601 asyncStart(); |
| 602 StreamController c = new StreamController.broadcast(); |
| 603 var f = c.close(); |
| 604 f.then((_) { |
| 605 // Listening after close is allowed. The listener gets a done event. |
| 606 var sub = c.stream.listen(null, onDone: () { |
| 607 Expect.fail("wrong done"); |
| 608 }); |
| 609 sub.pause(); |
| 610 sub.pause(); |
| 611 new Timer(MS * 100, () { |
| 612 sub.resume(); |
| 613 new Timer(MS * 100, () { |
| 614 sub.onDone(asyncEnd); |
| 615 sub.resume(); |
| 616 }); |
| 617 }); |
| 618 }); |
| 619 } |
| 620 |
| 621 void testAsBroadcastListenAfterClose() { |
| 622 asyncStart(); |
| 623 asyncStart(); |
| 624 StreamController c = new StreamController(); |
| 625 Stream s = c.stream.asBroadcastStream(); |
| 626 s.listen(null, onDone: asyncEnd); |
| 627 var f = c.close(); |
| 628 f.then((_) { |
| 629 // Listening after close is allowed. The listener gets a done event. |
| 630 s.listen(null, onDone: asyncEnd); |
| 631 }); |
| 632 } |
| 633 |
| 634 void testAsBroadcastListenAfterClosePaused() { |
| 635 asyncStart(); |
| 636 asyncStart(); |
| 637 StreamController c = new StreamController(); |
| 638 Stream s = c.stream.asBroadcastStream(); |
| 639 s.listen(null, onDone: asyncEnd); |
| 640 var f = c.close(); |
| 641 f.then((_) { |
| 642 // Listening after close is allowed. The listener gets a done event. |
| 643 var sub = s.listen(null, onDone: () { |
| 644 Expect.fail("wrong done"); |
| 645 }); |
| 646 sub.pause(); |
| 647 sub.pause(); |
| 648 new Timer(MS * 100, () { |
| 649 sub.resume(); |
| 650 new Timer(MS * 100, () { |
| 651 sub.onDone(asyncEnd); |
| 652 sub.resume(); |
| 653 }); |
| 654 }); |
| 655 }); |
| 656 } |
| 657 |
| 534 main() { | 658 main() { |
| 535 asyncStart(); | 659 asyncStart(); |
| 536 testMultiController(); | 660 testMultiController(); |
| 537 testSingleController(); | 661 testSingleController(); |
| 538 testExtraMethods(); | 662 testExtraMethods(); |
| 539 testClosed(); | 663 testClosed(); |
| 664 testCloseFuture(); |
| 665 testCloseFuture2(); |
| 666 testCloseFuture3(); |
| 540 testStreamEquals(); | 667 testStreamEquals(); |
| 541 testCancelThrow(); | 668 testCancelThrow(); |
| 542 testCancelThrow2(); | 669 testCancelThrow2(); |
| 543 testCancelThrow3(); | 670 testCancelThrow3(); |
| 671 testBroadcastListenAfterClose(); |
| 672 testBroadcastListenAfterClosePaused(); |
| 673 testAsBroadcastListenAfterClose(); |
| 674 testAsBroadcastListenAfterClosePaused(); |
| 544 asyncEnd(); | 675 asyncEnd(); |
| 545 } | 676 } |
| OLD | NEW |