|
19 | 19 | import static org.mockito.Matchers.any;
|
20 | 20 | import static org.mockito.Mockito.*;
|
21 | 21 |
|
| 22 | +import java.lang.management.ManagementFactory; |
22 | 23 | import java.util.*;
|
23 | 24 | import java.util.concurrent.*;
|
24 | 25 | import java.util.concurrent.atomic.*;
|
|
31 | 32 | import rx.Observable.OnSubscribe;
|
32 | 33 | import rx.Observer;
|
33 | 34 | import rx.functions.*;
|
| 35 | +import rx.observables.ConnectableObservable; |
34 | 36 | import rx.observers.*;
|
35 | 37 | import rx.schedulers.*;
|
36 | 38 | import rx.subjects.ReplaySubject;
|
@@ -611,4 +613,155 @@ public void call(Throwable t) {
|
611 | 613 | assertNotNull("First subscriber didn't get the error", err1);
|
612 | 614 | assertNotNull("Second subscriber didn't get the error", err2);
|
613 | 615 | }
|
| 616 | + |
| 617 | + Observable<Object> source; |
| 618 | + |
| 619 | + @Test |
| 620 | + public void replayNoLeak() throws Exception { |
| 621 | + System.gc(); |
| 622 | + Thread.sleep(100); |
| 623 | + |
| 624 | + long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 625 | + |
| 626 | + source = Observable.fromCallable(new Callable<Object>() { |
| 627 | + @Override |
| 628 | + public Object call() throws Exception { |
| 629 | + return new byte[100 * 1000 * 1000]; |
| 630 | + } |
| 631 | + }) |
| 632 | + .replay(1) |
| 633 | + .refCount(); |
| 634 | + |
| 635 | + source.subscribe(); |
| 636 | + |
| 637 | + System.gc(); |
| 638 | + Thread.sleep(100); |
| 639 | + |
| 640 | + long after = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 641 | + |
| 642 | + source = null; |
| 643 | + assertTrue(String.format("%,3d -> %,3d%n", start, after), start + 20 * 1000 * 1000 > after); |
| 644 | + } |
| 645 | + |
| 646 | + @Test |
| 647 | + public void replayNoLeak2() throws Exception { |
| 648 | + System.gc(); |
| 649 | + Thread.sleep(100); |
| 650 | + |
| 651 | + long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 652 | + |
| 653 | + source = Observable.fromCallable(new Callable<Object>() { |
| 654 | + @Override |
| 655 | + public Object call() throws Exception { |
| 656 | + return new byte[100 * 1000 * 1000]; |
| 657 | + } |
| 658 | + }).concatWith(Observable.never()) |
| 659 | + .replay(1) |
| 660 | + .refCount(); |
| 661 | + |
| 662 | + Subscription s1 = source.subscribe(); |
| 663 | + Subscription s2 = source.subscribe(); |
| 664 | + |
| 665 | + s1.unsubscribe(); |
| 666 | + s2.unsubscribe(); |
| 667 | + |
| 668 | + s1 = null; |
| 669 | + s2 = null; |
| 670 | + |
| 671 | + System.gc(); |
| 672 | + Thread.sleep(100); |
| 673 | + |
| 674 | + long after = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 675 | + |
| 676 | + source = null; |
| 677 | + assertTrue(String.format("%,3d -> %,3d%n", start, after), start + 20 * 1000 * 1000 > after); |
| 678 | + } |
| 679 | + |
| 680 | + static final class ExceptionData extends Exception { |
| 681 | + private static final long serialVersionUID = -6763898015338136119L; |
| 682 | + |
| 683 | + public final Object data; |
| 684 | + |
| 685 | + public ExceptionData(Object data) { |
| 686 | + this.data = data; |
| 687 | + } |
| 688 | + } |
| 689 | + |
| 690 | + @Test |
| 691 | + public void publishNoLeak() throws Exception { |
| 692 | + System.gc(); |
| 693 | + Thread.sleep(100); |
| 694 | + |
| 695 | + long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 696 | + |
| 697 | + source = Observable.fromCallable(new Callable<Object>() { |
| 698 | + @Override |
| 699 | + public Object call() throws Exception { |
| 700 | + throw new ExceptionData(new byte[100 * 1000 * 1000]); |
| 701 | + } |
| 702 | + }) |
| 703 | + .publish() |
| 704 | + .refCount(); |
| 705 | + |
| 706 | + Action1<Throwable> err = Actions.empty(); |
| 707 | + source.subscribe(Actions.empty(), err); |
| 708 | + |
| 709 | + System.gc(); |
| 710 | + Thread.sleep(100); |
| 711 | + |
| 712 | + long after = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 713 | + |
| 714 | + source = null; |
| 715 | + assertTrue(String.format("%,3d -> %,3d%n", start, after), start + 20 * 1000 * 1000 > after); |
| 716 | + } |
| 717 | + |
| 718 | + @Test |
| 719 | + public void publishNoLeak2() throws Exception { |
| 720 | + System.gc(); |
| 721 | + Thread.sleep(100); |
| 722 | + |
| 723 | + long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 724 | + |
| 725 | + source = Observable.fromCallable(new Callable<Object>() { |
| 726 | + @Override |
| 727 | + public Object call() throws Exception { |
| 728 | + return new byte[100 * 1000 * 1000]; |
| 729 | + } |
| 730 | + }).concatWith(Observable.never()) |
| 731 | + .publish() |
| 732 | + .refCount(); |
| 733 | + |
| 734 | + Subscription s1 = source.test(0); |
| 735 | + Subscription s2 = source.test(0); |
| 736 | + |
| 737 | + s1.unsubscribe(); |
| 738 | + s2.unsubscribe(); |
| 739 | + |
| 740 | + s1 = null; |
| 741 | + s2 = null; |
| 742 | + |
| 743 | + System.gc(); |
| 744 | + Thread.sleep(100); |
| 745 | + |
| 746 | + long after = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 747 | + |
| 748 | + source = null; |
| 749 | + assertTrue(String.format("%,3d -> %,3d%n", start, after), start + 20 * 1000 * 1000 > after); |
| 750 | + } |
| 751 | + |
| 752 | + @Test |
| 753 | + public void replayIsUnsubscribed() { |
| 754 | + ConnectableObservable<Integer> co = Observable.just(1) |
| 755 | + .replay(); |
| 756 | + |
| 757 | + assertTrue(((Subscription)co).isUnsubscribed()); |
| 758 | + |
| 759 | + Subscription s = co.connect(); |
| 760 | + |
| 761 | + assertFalse(((Subscription)co).isUnsubscribed()); |
| 762 | + |
| 763 | + s.unsubscribe(); |
| 764 | + |
| 765 | + assertTrue(((Subscription)co).isUnsubscribed()); |
| 766 | + } |
614 | 767 | }
|
0 commit comments