Commit 7284136f authored by Brandon Wylie's avatar Brandon Wylie Committed by Commit Bot

Alter/use GarbageCollectionTestUtils to deflake unit tests

Bug: 1067622
Change-Id: I1fddfec245cf267100f56756bc9fd38ddacb87ab
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2137868
Commit-Queue: Brandon Wylie <wylieb@chromium.org>
Reviewed-by: default avatarWei-Yin Chen (陳威尹) <wychen@chromium.org>
Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#757127}
parent cff9eaf9
......@@ -43,11 +43,9 @@ public class DiscardableReferencePoolTest {
Assert.assertNull(discardableReference.get());
// The object is not (strongly) reachable anymore, so the weak reference may or may not be
// null (it could be if a GC has happened since the pool was drained).
// After an explicit GC call it definitely should be null.
Runtime.getRuntime().gc();
Assert.assertNull(weakReference.get());
// null (it could be if a GC has happened since the pool was drained). It should be
// eligible for garbage collection.
Assert.assertTrue(GarbageCollectionTestUtils.canBeGarbageCollected(weakReference));
}
@Test
......@@ -72,11 +70,9 @@ public class DiscardableReferencePoolTest {
Assert.assertNull(discardableReference.get());
// The object is not (strongly) reachable anymore, so the weak reference may or may not be
// null (it could be if a GC has happened since the pool was drained).
// After an explicit GC call it definitely should be null.
Runtime.getRuntime().gc();
Assert.assertNull(weakReference.get());
// null (it could be if a GC has happened since the pool was drained). It should be
// eligible for garbage collection.
Assert.assertTrue(GarbageCollectionTestUtils.canBeGarbageCollected(weakReference));
}
@Test
......@@ -101,11 +97,9 @@ public class DiscardableReferencePoolTest {
Assert.assertNull(discardableReference.get());
// The object is not (strongly) reachable anymore, so the weak reference may or may not be
// null (it could be if a GC has happened since the pool was drained).
// After an explicit GC call it definitely should be null.
Runtime.getRuntime().gc();
Assert.assertNull(weakReference.get());
// null (it could be if a GC has happened since the pool was drained). It should be
// eligible for garbage collection.
Assert.assertTrue(GarbageCollectionTestUtils.canBeGarbageCollected(weakReference));
}
/**
......@@ -129,10 +123,8 @@ public class DiscardableReferencePoolTest {
discardableReference = null;
// The object is not (strongly) reachable anymore, so the weak reference may or may not be
// null (it could be if a GC has happened since the pool was drained).
// After an explicit GC call it definitely should be null.
Runtime.getRuntime().gc();
Assert.assertNull(weakReference.get());
// null (it could be if a GC has happened since the pool was drained). It should be
// eligible for garbage collection.
Assert.assertTrue(GarbageCollectionTestUtils.canBeGarbageCollected(weakReference));
}
}
......@@ -4,8 +4,6 @@
package org.chromium.base;
import android.os.Build;
import java.lang.ref.WeakReference;
/**
......@@ -17,8 +15,8 @@ public class GarbageCollectionTestUtils {
* Note that {@link #MAX_GC_ITERATIONS} * {@link #GC_SLEEP_TIME} should not be too large,
* since there are tests asserting objects NOT garbage collected.
*/
private final static int MAX_GC_ITERATIONS = 3;
private final static long GC_SLEEP_TIME = 100;
private static final int MAX_GC_ITERATIONS = 3;
private static final long GC_SLEEP_TIME = 10;
/**
* Do garbage collection and see if an object is released.
......@@ -27,8 +25,8 @@ public class GarbageCollectionTestUtils {
*/
public static boolean canBeGarbageCollected(WeakReference<?> reference) {
// Robolectric tests, one iteration is enough.
final int iterations = isInRobolectric() ? 1 : MAX_GC_ITERATIONS;
final long sleepTime = isInRobolectric() ? 0 : GC_SLEEP_TIME;
final int iterations = MAX_GC_ITERATIONS;
final long sleepTime = GC_SLEEP_TIME;
Runtime runtime = Runtime.getRuntime();
for (int i = 0; i < iterations; i++) {
runtime.runFinalization();
......@@ -45,8 +43,4 @@ public class GarbageCollectionTestUtils {
return reference.get() == null;
}
private static boolean isInRobolectric() {
return Build.FINGERPRINT.equals("robolectric");
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment