A test for unexpected finalization on objects that failed creation.
Change-Id: Idec898d766cabb947ec905ecf0a97040c776d149
http://b/issue?id=2645458
diff --git a/luni/src/test/java/java/lang/SystemTest.java b/luni/src/test/java/java/lang/SystemTest.java
index b6fc5de..6cc8f73 100644
--- a/luni/src/test/java/java/lang/SystemTest.java
+++ b/luni/src/test/java/java/lang/SystemTest.java
@@ -61,4 +61,44 @@
e.getMessage());
}
}
+
+ /**
+ * http://b/issue?id=2136462
+ */
+ public void testBackFromTheDead() {
+ try {
+ new ConstructionFails();
+ } catch (AssertionError expected) {
+ }
+
+ for (int i = 0; i < 20; i++) {
+ if (ConstructionFails.INSTANCE != null) {
+ fail("finalize() called, even though constructor failed!");
+ }
+
+ induceGc(i);
+ }
+ }
+
+ private void induceGc(int rev) {
+ System.gc();
+ try {
+ byte[] b = new byte[1024 << rev];
+ } catch (OutOfMemoryError e) {
+ }
+ }
+
+ static class ConstructionFails {
+ private static ConstructionFails INSTANCE;
+
+ ConstructionFails() {
+ throw new AssertionError();
+ }
+
+ @Override protected void finalize() throws Throwable {
+ INSTANCE = this;
+ new AssertionError("finalize() called, even though constructor failed!")
+ .printStackTrace();
+ }
+ }
}