|
17 | 17 | import static org.mockito.ArgumentMatchers.any;
|
18 | 18 | import static org.mockito.Mockito.*;
|
19 | 19 |
|
| 20 | +import java.lang.management.ManagementFactory; |
20 | 21 | import java.util.*;
|
21 | 22 | import java.util.concurrent.*;
|
22 | 23 | import java.util.concurrent.atomic.*;
|
23 | 24 |
|
24 |
| -import org.junit.*; |
| 25 | +import org.junit.Test; |
25 | 26 | import org.mockito.InOrder;
|
26 | 27 | import org.reactivestreams.*;
|
27 | 28 |
|
28 | 29 | import io.reactivex.*;
|
29 | 30 | import io.reactivex.disposables.Disposable;
|
30 | 31 | import io.reactivex.flowables.ConnectableFlowable;
|
31 | 32 | import io.reactivex.functions.*;
|
| 33 | +import io.reactivex.internal.functions.Functions; |
32 | 34 | import io.reactivex.internal.subscriptions.BooleanSubscription;
|
33 | 35 | import io.reactivex.processors.ReplayProcessor;
|
34 | 36 | import io.reactivex.schedulers.*;
|
@@ -619,4 +621,154 @@ protected void subscribeActual(Subscriber<? super Integer> observer) {
|
619 | 621 |
|
620 | 622 | assertEquals(1, calls[0]);
|
621 | 623 | }
|
| 624 | + |
| 625 | + Flowable<Object> source; |
| 626 | + |
| 627 | + @Test |
| 628 | + public void replayNoLeak() throws Exception { |
| 629 | + System.gc(); |
| 630 | + Thread.sleep(100); |
| 631 | + |
| 632 | + long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 633 | + |
| 634 | + source = Flowable.fromCallable(new Callable<Object>() { |
| 635 | + @Override |
| 636 | + public Object call() throws Exception { |
| 637 | + return new byte[100 * 1000 * 1000]; |
| 638 | + } |
| 639 | + }) |
| 640 | + .replay(1) |
| 641 | + .refCount(); |
| 642 | + |
| 643 | + source.subscribe(); |
| 644 | + |
| 645 | + System.gc(); |
| 646 | + Thread.sleep(100); |
| 647 | + |
| 648 | + long after = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 649 | + |
| 650 | + source = null; |
| 651 | + assertTrue(String.format("%,3d -> %,3d%n", start, after), start + 20 * 1000 * 1000 > after); |
| 652 | + } |
| 653 | + |
| 654 | + @Test |
| 655 | + public void replayNoLeak2() throws Exception { |
| 656 | + System.gc(); |
| 657 | + Thread.sleep(100); |
| 658 | + |
| 659 | + long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 660 | + |
| 661 | + source = Flowable.fromCallable(new Callable<Object>() { |
| 662 | + @Override |
| 663 | + public Object call() throws Exception { |
| 664 | + return new byte[100 * 1000 * 1000]; |
| 665 | + } |
| 666 | + }).concatWith(Flowable.never()) |
| 667 | + .replay(1) |
| 668 | + .refCount(); |
| 669 | + |
| 670 | + Disposable s1 = source.subscribe(); |
| 671 | + Disposable s2 = source.subscribe(); |
| 672 | + |
| 673 | + s1.dispose(); |
| 674 | + s2.dispose(); |
| 675 | + |
| 676 | + s1 = null; |
| 677 | + s2 = null; |
| 678 | + |
| 679 | + System.gc(); |
| 680 | + Thread.sleep(100); |
| 681 | + |
| 682 | + long after = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 683 | + |
| 684 | + source = null; |
| 685 | + assertTrue(String.format("%,3d -> %,3d%n", start, after), start + 20 * 1000 * 1000 > after); |
| 686 | + } |
| 687 | + |
| 688 | + static final class ExceptionData extends Exception { |
| 689 | + private static final long serialVersionUID = -6763898015338136119L; |
| 690 | + |
| 691 | + public final Object data; |
| 692 | + |
| 693 | + public ExceptionData(Object data) { |
| 694 | + this.data = data; |
| 695 | + } |
| 696 | + } |
| 697 | + |
| 698 | + @Test |
| 699 | + public void publishNoLeak() throws Exception { |
| 700 | + System.gc(); |
| 701 | + Thread.sleep(100); |
| 702 | + |
| 703 | + long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 704 | + |
| 705 | + source = Flowable.fromCallable(new Callable<Object>() { |
| 706 | + @Override |
| 707 | + public Object call() throws Exception { |
| 708 | + throw new ExceptionData(new byte[100 * 1000 * 1000]); |
| 709 | + } |
| 710 | + }) |
| 711 | + .publish() |
| 712 | + .refCount(); |
| 713 | + |
| 714 | + source.subscribe(Functions.emptyConsumer(), Functions.emptyConsumer()); |
| 715 | + |
| 716 | + System.gc(); |
| 717 | + Thread.sleep(100); |
| 718 | + |
| 719 | + long after = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 720 | + |
| 721 | + source = null; |
| 722 | + assertTrue(String.format("%,3d -> %,3d%n", start, after), start + 20 * 1000 * 1000 > after); |
| 723 | + } |
| 724 | + |
| 725 | + @Test |
| 726 | + public void publishNoLeak2() throws Exception { |
| 727 | + System.gc(); |
| 728 | + Thread.sleep(100); |
| 729 | + |
| 730 | + long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 731 | + |
| 732 | + source = Flowable.fromCallable(new Callable<Object>() { |
| 733 | + @Override |
| 734 | + public Object call() throws Exception { |
| 735 | + return new byte[100 * 1000 * 1000]; |
| 736 | + } |
| 737 | + }).concatWith(Flowable.never()) |
| 738 | + .publish() |
| 739 | + .refCount(); |
| 740 | + |
| 741 | + Disposable s1 = source.test(); |
| 742 | + Disposable s2 = source.test(); |
| 743 | + |
| 744 | + s1.dispose(); |
| 745 | + s2.dispose(); |
| 746 | + |
| 747 | + s1 = null; |
| 748 | + s2 = null; |
| 749 | + |
| 750 | + System.gc(); |
| 751 | + Thread.sleep(100); |
| 752 | + |
| 753 | + long after = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 754 | + |
| 755 | + source = null; |
| 756 | + assertTrue(String.format("%,3d -> %,3d%n", start, after), start + 20 * 1000 * 1000 > after); |
| 757 | + } |
| 758 | + |
| 759 | + @Test |
| 760 | + public void replayIsUnsubscribed() { |
| 761 | + ConnectableFlowable<Integer> co = Flowable.just(1) |
| 762 | + .replay(); |
| 763 | + |
| 764 | + assertTrue(((Disposable)co).isDisposed()); |
| 765 | + |
| 766 | + Disposable s = co.connect(); |
| 767 | + |
| 768 | + assertFalse(((Disposable)co).isDisposed()); |
| 769 | + |
| 770 | + s.dispose(); |
| 771 | + |
| 772 | + assertTrue(((Disposable)co).isDisposed()); |
| 773 | + } |
622 | 774 | }
|
0 commit comments