Skip to content

Commit d9c0760

Browse files
authored
3.x: Add more time for GC in RefCountTest.publishNoLeak (#6581)
* 3.x: Add more time for GC in RefCountTest.publishNoLeak * Some test restructuring and dynamic GC wait
1 parent e4eee64 commit d9c0760

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

src/test/java/io/reactivex/internal/operators/flowable/FlowableRefCountTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -731,13 +731,12 @@ static final class ExceptionData extends Exception {
731731
}
732732
}
733733

734+
static final int GC_SLEEP_TIME = 250;
735+
734736
@Test
735737
public void publishNoLeak() throws Exception {
736-
Thread.sleep(100);
737738
System.gc();
738-
Thread.sleep(100);
739-
740-
long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
739+
Thread.sleep(GC_SLEEP_TIME);
741740

742741
source = Flowable.fromCallable(new Callable<Object>() {
743742
@Override
@@ -748,11 +747,12 @@ public Object call() throws Exception {
748747
.publish()
749748
.refCount();
750749

750+
long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
751+
751752
source.subscribe(Functions.emptyConsumer(), Functions.emptyConsumer());
752753

753-
Thread.sleep(100);
754754
System.gc();
755-
Thread.sleep(200);
755+
Thread.sleep(GC_SLEEP_TIME);
756756

757757
long after = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
758758

@@ -764,7 +764,7 @@ public Object call() throws Exception {
764764
@Test
765765
public void publishNoLeak2() throws Exception {
766766
System.gc();
767-
Thread.sleep(100);
767+
Thread.sleep(GC_SLEEP_TIME);
768768

769769
long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
770770

@@ -787,7 +787,7 @@ public Object call() throws Exception {
787787
d2 = null;
788788

789789
System.gc();
790-
Thread.sleep(100);
790+
Thread.sleep(GC_SLEEP_TIME);
791791

792792
long after = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
793793

src/test/java/io/reactivex/internal/operators/observable/ObservableRefCountTest.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -704,12 +704,12 @@ static final class ExceptionData extends Exception {
704704
}
705705
}
706706

707+
static final int GC_SLEEP_TIME = 250;
708+
707709
@Test
708710
public void publishNoLeak() throws Exception {
709711
System.gc();
710-
Thread.sleep(100);
711-
712-
long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
712+
Thread.sleep(GC_SLEEP_TIME);
713713

714714
source = Observable.fromCallable(new Callable<Object>() {
715715
@Override
@@ -720,12 +720,23 @@ public Object call() throws Exception {
720720
.publish()
721721
.refCount();
722722

723+
long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
724+
723725
source.subscribe(Functions.emptyConsumer(), Functions.emptyConsumer());
724726

725-
System.gc();
726-
Thread.sleep(100);
727+
long after = 0L;
727728

728-
long after = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
729+
for (int i = 0; i < 10; i++) {
730+
System.gc();
731+
732+
after = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
733+
734+
if (start + 20 * 1000 * 1000 > after) {
735+
break;
736+
}
737+
738+
Thread.sleep(GC_SLEEP_TIME);
739+
}
729740

730741
source = null;
731742
assertTrue(String.format("%,3d -> %,3d%n", start, after), start + 20 * 1000 * 1000 > after);
@@ -734,7 +745,7 @@ public Object call() throws Exception {
734745
@Test
735746
public void publishNoLeak2() throws Exception {
736747
System.gc();
737-
Thread.sleep(100);
748+
Thread.sleep(GC_SLEEP_TIME);
738749

739750
long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
740751

@@ -757,7 +768,7 @@ public Object call() throws Exception {
757768
d2 = null;
758769

759770
System.gc();
760-
Thread.sleep(100);
771+
Thread.sleep(GC_SLEEP_TIME);
761772

762773
long after = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
763774

0 commit comments

Comments
 (0)