summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Wilson <jessewilson@google.com>2011-11-17 15:15:47 -0500
committerJesse Wilson <jessewilson@google.com>2011-11-17 15:15:47 -0500
commit1a9cff8f68deffd618c5cba1f22f0fb0e396e067 (patch)
tree855df013c6df60346d481e3d84430a98ad994379
parent03aaa13a6c9eb7912d903ebbd792f6c303b43757 (diff)
downloadlibcore-1a9cff8f68deffd618c5cba1f22f0fb0e396e067.zip
libcore-1a9cff8f68deffd618c5cba1f22f0fb0e396e067.tar.gz
libcore-1a9cff8f68deffd618c5cba1f22f0fb0e396e067.tar.bz2
Always trigger finalization the same way in our tests.
This came up when I was recently writing a ZipFile finalization test and I needed to copy-paste the finalizer recipe. Change-Id: Ia67061b3dba1a7011c93c9a81e2a963876b238a1
-rw-r--r--luni/src/test/java/libcore/java/io/RandomAccessFileTest.java4
-rw-r--r--luni/src/test/java/libcore/java/lang/OldThreadTest.java3
-rw-r--r--luni/src/test/java/libcore/java/lang/ThreadTest.java5
-rw-r--r--luni/src/test/java/libcore/java/lang/ref/FinalizationTester.java45
-rw-r--r--luni/src/test/java/libcore/java/lang/ref/FinalizeTest.java30
-rw-r--r--support/src/test/java/libcore/dalvik/system/CloseGuardTester.java7
6 files changed, 61 insertions, 33 deletions
diff --git a/luni/src/test/java/libcore/java/io/RandomAccessFileTest.java b/luni/src/test/java/libcore/java/io/RandomAccessFileTest.java
index 11142d5..afe49b7 100644
--- a/luni/src/test/java/libcore/java/io/RandomAccessFileTest.java
+++ b/luni/src/test/java/libcore/java/io/RandomAccessFileTest.java
@@ -21,6 +21,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import junit.framework.TestCase;
+import libcore.java.lang.ref.FinalizationTester;
public final class RandomAccessFileTest extends TestCase {
@@ -69,8 +70,7 @@ public final class RandomAccessFileTest extends TestCase {
File file = File.createTempFile("RandomAccessFileTest", "tmp");
for (int i = 0; i < tooManyOpenFiles; i++) {
createRandomAccessFile(file);
- System.gc();
- System.runFinalization();
+ FinalizationTester.induceFinalization();
}
}
private void createRandomAccessFile(File file) throws Exception {
diff --git a/luni/src/test/java/libcore/java/lang/OldThreadTest.java b/luni/src/test/java/libcore/java/lang/OldThreadTest.java
index 1e189f6..2b304f2 100644
--- a/luni/src/test/java/libcore/java/lang/OldThreadTest.java
+++ b/luni/src/test/java/libcore/java/lang/OldThreadTest.java
@@ -21,6 +21,7 @@ import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.LockSupport;
+import libcore.java.lang.ref.FinalizationTester;
public class OldThreadTest extends junit.framework.TestCase {
@@ -478,7 +479,7 @@ public class OldThreadTest extends junit.framework.TestCase {
spinner = null;
st = null;
ct = null;
- System.runFinalization();
+ FinalizationTester.induceFinalization();
} catch (Exception e) {
}
}
diff --git a/luni/src/test/java/libcore/java/lang/ThreadTest.java b/luni/src/test/java/libcore/java/lang/ThreadTest.java
index 3695e3e..42a18ef 100644
--- a/luni/src/test/java/libcore/java/lang/ThreadTest.java
+++ b/luni/src/test/java/libcore/java/lang/ThreadTest.java
@@ -18,6 +18,7 @@ package libcore.java.lang;
import java.util.concurrent.atomic.AtomicInteger;
import junit.framework.TestCase;
+import libcore.java.lang.ref.FinalizationTester;
public final class ThreadTest extends TestCase {
@@ -30,7 +31,7 @@ public final class ThreadTest extends TestCase {
break;
}
}
- System.runFinalization();
+ FinalizationTester.induceFinalization();
assertTrue("Started threads were never finalized!", finalizedThreadsCount.get() > 0);
}
@@ -43,7 +44,7 @@ public final class ThreadTest extends TestCase {
break;
}
}
- System.runFinalization();
+ FinalizationTester.induceFinalization();
assertTrue("Unstarted threads were never finalized!", finalizedThreadsCount.get() > 0);
}
diff --git a/luni/src/test/java/libcore/java/lang/ref/FinalizationTester.java b/luni/src/test/java/libcore/java/lang/ref/FinalizationTester.java
new file mode 100644
index 0000000..66ac1a4
--- /dev/null
+++ b/luni/src/test/java/libcore/java/lang/ref/FinalizationTester.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2011 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.java.lang.ref;
+
+/**
+ * Triggers finalizers to run. Note that live-precise bugs may interfere with
+ * analysis of what is reachable. Unreachable references in local frames may not
+ * be finalized even after a call to {@link #induceFinalization}. To work
+ * around, create finalizable objects in helper methods. http://b/4191345
+ */
+public final class FinalizationTester {
+ private FinalizationTester() {}
+
+ public static void induceFinalization() {
+ System.gc();
+ enqueueReferences();
+ System.runFinalization();
+ }
+
+ public static void enqueueReferences() {
+ /*
+ * Hack. We don't have a programmatic way to wait for the reference queue
+ * daemon to move references to the appropriate queues.
+ */
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ throw new AssertionError();
+ }
+ }
+}
diff --git a/luni/src/test/java/libcore/java/lang/ref/FinalizeTest.java b/luni/src/test/java/libcore/java/lang/ref/FinalizeTest.java
index b6f930c..10a26fe 100644
--- a/luni/src/test/java/libcore/java/lang/ref/FinalizeTest.java
+++ b/luni/src/test/java/libcore/java/lang/ref/FinalizeTest.java
@@ -27,17 +27,13 @@ public final class FinalizeTest extends TestCase {
AtomicBoolean finalized = new AtomicBoolean();
createFinalizableObject(finalized);
- induceFinalization();
+ FinalizationTester.induceFinalization();
if (!finalized.get()) {
fail();
}
}
- /**
- * Prevent live-precise bugs from interfering with analysis of what is
- * reachable. Do not inline this method; otherwise tests may fail on VMs
- * that are not live-precise. http://b/4191345
- */
+ /** Do not inline this method; that could break non-precise GCs. See FinalizationTester. */
private void createFinalizableObject(final AtomicBoolean finalized) {
new X() {
@Override protected void finalize() throws Throwable {
@@ -56,24 +52,10 @@ public final class FinalizeTest extends TestCase {
} catch (AssertionError expected) {
}
- induceFinalization();
+ FinalizationTester.induceFinalization();
assertTrue("object whose constructor threw was not finalized", ConstructionFails.finalized);
}
- private void induceFinalization() throws Exception {
- System.gc();
- enqueueReferences();
- System.runFinalization();
- }
-
- /**
- * Hack. We don't have a programmatic way to wait for the reference queue
- * daemon to move references to the appropriate queues.
- */
- private void enqueueReferences() throws InterruptedException {
- Thread.sleep(100);
- }
-
static class ConstructionFails {
private static boolean finalized;
@@ -97,7 +79,7 @@ public final class FinalizeTest extends TestCase {
createSlowFinalizer(2000, latch);
createSlowFinalizer(4000, latch);
createSlowFinalizer(8000, latch);
- induceFinalization();
+ FinalizationTester.induceFinalization();
latch.await();
}
@@ -119,7 +101,7 @@ public final class FinalizeTest extends TestCase {
AtomicInteger count = new AtomicInteger();
AtomicBoolean keepGoing = new AtomicBoolean(true);
createChainedFinalizer(count, keepGoing);
- induceFinalization();
+ FinalizationTester.induceFinalization();
keepGoing.set(false);
assertTrue(count.get() > 0);
}
@@ -133,7 +115,7 @@ public final class FinalizeTest extends TestCase {
createChainedFinalizer(counter, keepGoing); // recursive!
}
System.gc();
- enqueueReferences();
+ FinalizationTester.enqueueReferences();
}
};
}
diff --git a/support/src/test/java/libcore/dalvik/system/CloseGuardTester.java b/support/src/test/java/libcore/dalvik/system/CloseGuardTester.java
index 4ed59a2..e82d33d 100644
--- a/support/src/test/java/libcore/dalvik/system/CloseGuardTester.java
+++ b/support/src/test/java/libcore/dalvik/system/CloseGuardTester.java
@@ -26,6 +26,7 @@ import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import junit.framework.Assert;
+import libcore.java.lang.ref.FinalizationTester;
public final class CloseGuardTester implements Closeable {
@@ -45,14 +46,12 @@ public final class CloseGuardTester implements Closeable {
* Collect immediately before we start monitoring the CloseGuard logs.
* This lowers the chance that we'll report an unrelated leak.
*/
- System.gc();
- System.runFinalization();
+ FinalizationTester.induceFinalization();
logger.addHandler(logWatcher);
}
public void assertEverythingWasClosed() {
- System.gc();
- System.runFinalization();
+ FinalizationTester.induceFinalization();
if (!logRecords.isEmpty()) {
// print the log records with the output of this test