diff options
author | Elliott Hughes <enh@google.com> | 2012-09-24 11:02:11 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2012-09-24 11:36:53 -0700 |
commit | fce022be38a2c79f9cdc8d9fea33c72cf2f16528 (patch) | |
tree | ee09dbb9af205627d887f8de8860687b432276fd | |
parent | 09fec8d6f75c933984d31195c0efcb686eff46c6 (diff) | |
download | libcore-fce022be38a2c79f9cdc8d9fea33c72cf2f16528.zip libcore-fce022be38a2c79f9cdc8d9fea33c72cf2f16528.tar.gz libcore-fce022be38a2c79f9cdc8d9fea33c72cf2f16528.tar.bz2 |
Fix flaky finalization tests.
(cherry-pick of bb9878546d29f4f4688d68f12b2af9d8c9a68fe4.)
Bug: 6973520
Change-Id: I372e451b5858e2f01a0aeeb7cb659f7df9f9348b
6 files changed, 22 insertions, 25 deletions
diff --git a/luni/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java b/luni/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java index 17d251b..a423f22 100644 --- a/luni/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java +++ b/luni/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java @@ -32,6 +32,7 @@ import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; +import libcore.java.lang.ref.FinalizationTester; public class ZipFileTest extends junit.framework.TestCase { @@ -150,11 +151,8 @@ public class ZipFileTest extends junit.framework.TestCase { * entry1); entry1 = null; zip = null; */ - assertNotNull("Did not find entry", - test_finalize1(test_finalize2(file))); - System.gc(); - System.gc(); - System.runFinalization(); + assertNotNull("Did not find entry", test_finalize1(test_finalize2(file))); + FinalizationTester.induceFinalization(); file.delete(); assertTrue("Zip should not exist", !file.exists()); } diff --git a/luni/src/test/java/tests/api/java/lang/ref/PhantomReferenceTest.java b/luni/src/test/java/tests/api/java/lang/ref/PhantomReferenceTest.java index 06221c9..6470579 100644 --- a/luni/src/test/java/tests/api/java/lang/ref/PhantomReferenceTest.java +++ b/luni/src/test/java/tests/api/java/lang/ref/PhantomReferenceTest.java @@ -20,6 +20,7 @@ package tests.api.java.lang.ref; import java.lang.ref.PhantomReference; import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; +import libcore.java.lang.ref.FinalizationTester; //TODO: write a test to verify that the referent's finalize() happens // before the PhantomReference is enqueued. @@ -81,8 +82,8 @@ public class PhantomReferenceTest extends junit.framework.TestCase { Thread t = new TestThread(); t.start(); t.join(); - System.gc(); - System.runFinalization(); + + FinalizationTester.induceFinalization(); assertNull("get() should return null.", tprs[0].get()); assertNull("get() should return null.", tprs[1].get()); diff --git a/luni/src/test/java/tests/api/java/lang/ref/ReferenceQueueTest.java b/luni/src/test/java/tests/api/java/lang/ref/ReferenceQueueTest.java index dc7e738..cad61b3 100644 --- a/luni/src/test/java/tests/api/java/lang/ref/ReferenceQueueTest.java +++ b/luni/src/test/java/tests/api/java/lang/ref/ReferenceQueueTest.java @@ -22,6 +22,7 @@ import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java.lang.ref.SoftReference; import java.lang.ref.WeakReference; +import libcore.java.lang.ref.FinalizationTester; public class ReferenceQueueTest extends junit.framework.TestCase { static Boolean b; @@ -97,8 +98,7 @@ public class ReferenceQueueTest extends junit.framework.TestCase { sr.enqueue(); wr.enqueue(); - System.gc(); - System.runFinalization(); + FinalizationTester.induceFinalization(); assertNull(rq.poll()); } diff --git a/luni/src/test/java/tests/api/java/lang/ref/ReferenceTest.java b/luni/src/test/java/tests/api/java/lang/ref/ReferenceTest.java index a1a7a8c..7461b47 100644 --- a/luni/src/test/java/tests/api/java/lang/ref/ReferenceTest.java +++ b/luni/src/test/java/tests/api/java/lang/ref/ReferenceTest.java @@ -22,6 +22,7 @@ import java.lang.ref.ReferenceQueue; import java.lang.ref.SoftReference; import java.lang.ref.WeakReference; import junit.framework.AssertionFailedError; +import libcore.java.lang.ref.FinalizationTester; public class ReferenceTest extends junit.framework.TestCase { Object tmpA, tmpB, tmpC, obj; @@ -146,16 +147,14 @@ public class ReferenceTest extends junit.framework.TestCase { ReferenceQueue<Object> queue = new ReferenceQueue<Object>(); r = newWeakReference(queue); - System.gc(); - System.runFinalization(); + FinalizationTester.induceFinalization(); Reference ref = queue.remove(); assertNotNull("Object not enqueued.", ref); assertSame("Unexpected ref1", ref, r); assertNull("Object could not be reclaimed1.", r.get()); r = newWeakReference(queue); - System.gc(); - System.runFinalization(); + FinalizationTester.induceFinalization(); // wait for the reference queue thread to enqueue the newly-finalized object Thread.yield(); @@ -213,8 +212,7 @@ public class ReferenceTest extends junit.framework.TestCase { Thread t = new TestThread(); t.start(); t.join(); - System.gc(); - System.runFinalization(); + FinalizationTester.induceFinalization(); ref = rq.remove(5000L); // Give up after five seconds. assertNotNull("Object not garbage collected.", ref); @@ -238,8 +236,7 @@ public class ReferenceTest extends junit.framework.TestCase { public void test_get() { WeakReference ref = newWeakReference(null); - System.gc(); - System.runFinalization(); + FinalizationTester.induceFinalization(); assertNull("get() doesn't return null after gc for WeakReference", ref.get()); obj = new Object(); @@ -322,8 +319,7 @@ public class ReferenceTest extends junit.framework.TestCase { Thread t = new TestThread(); t.start(); t.join(); - System.gc(); - System.runFinalization(); + FinalizationTester.induceFinalization(); Thread.sleep(1000); if (error != null) { throw error; diff --git a/luni/src/test/java/tests/api/java/lang/ref/SoftReferenceTest.java b/luni/src/test/java/tests/api/java/lang/ref/SoftReferenceTest.java index 77c6536..197d829 100644 --- a/luni/src/test/java/tests/api/java/lang/ref/SoftReferenceTest.java +++ b/luni/src/test/java/tests/api/java/lang/ref/SoftReferenceTest.java @@ -22,6 +22,7 @@ import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java.lang.ref.SoftReference; import java.util.Vector; +import libcore.java.lang.ref.FinalizationTester; public class SoftReferenceTest extends junit.framework.TestCase { static Boolean bool; @@ -124,8 +125,7 @@ public class SoftReferenceTest extends junit.framework.TestCase { TestThread t = new TestThread(); t.start(); t.join(); - System.gc(); - System.runFinalization(); + FinalizationTester.induceFinalization(); ref = rq.poll(); assertNotNull("Object not garbage collected.", ref); assertNull("Object is not null.", ref.get()); diff --git a/luni/src/test/java/tests/api/java/util/WeakHashMapTest.java b/luni/src/test/java/tests/api/java/util/WeakHashMapTest.java index 0e43bf6..d1a43e5 100644 --- a/luni/src/test/java/tests/api/java/util/WeakHashMapTest.java +++ b/luni/src/test/java/tests/api/java/util/WeakHashMapTest.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.WeakHashMap; +import libcore.java.lang.ref.FinalizationTester; import tests.support.Support_MapTest2; @@ -208,7 +209,7 @@ public class WeakHashMapTest extends junit.framework.TestCase { do { System.gc(); System.gc(); - Runtime.getRuntime().runFinalization(); + FinalizationTester.induceFinalization(); count++; } while (count <= 5 && entrySet.size() == 100); @@ -240,7 +241,8 @@ public class WeakHashMapTest extends junit.framework.TestCase { WeakHashMap map = new WeakHashMap(); map.put(null, "value"); // add null key System.gc(); - System.runFinalization(); + System.gc(); + FinalizationTester.induceFinalization(); map.remove("nothing"); // Cause objects in queue to be removed assertEquals("null key was removed", 1, map.size()); } @@ -315,7 +317,7 @@ public class WeakHashMapTest extends junit.framework.TestCase { do { System.gc(); System.gc(); - Runtime.getRuntime().runFinalization(); + FinalizationTester.induceFinalization(); count++; } while (count <= 5 && keySet.size() == 100); @@ -352,7 +354,7 @@ public class WeakHashMapTest extends junit.framework.TestCase { do { System.gc(); System.gc(); - Runtime.getRuntime().runFinalization(); + FinalizationTester.induceFinalization(); count++; } while (count <= 5 && valuesCollection.size() == 100); |