Add regression test for invalid optimization in the presence finalize.
Bug: 143189461
Change-Id: I3205fbc2c63ba078554a59e38c2bbeb5f40ed4b3
diff --git a/src/test/java/com/android/tools/r8/ir/analysis/sideeffect/AnonymousObjectWithFinalizeTest.java b/src/test/java/com/android/tools/r8/ir/analysis/sideeffect/AnonymousObjectWithFinalizeTest.java
new file mode 100644
index 0000000..8ae2890
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/ir/analysis/sideeffect/AnonymousObjectWithFinalizeTest.java
@@ -0,0 +1,93 @@
+// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package com.android.tools.r8.ir.analysis.sideeffect;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.StringUtils;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class AnonymousObjectWithFinalizeTest extends TestBase {
+
+ final static String EXPECTED = StringUtils.lines(
+ "set up fence",
+ "run gc",
+ "count down fence",
+ "passed fence"
+ );
+
+ public static class TestClass {
+
+ public static void main(String[] args) {
+ Runtime.getRuntime().gc();
+ Runtime.getRuntime().runFinalization();
+
+ System.out.println("set up fence");
+ final CountDownLatch fence = new CountDownLatch(1);
+ new Object() {
+ @Override
+ protected void finalize() throws Throwable {
+ try {
+ System.out.println("count down fence");
+ fence.countDown();
+ } finally {
+ super.finalize();
+ }
+ }
+ };
+ try {
+ System.out.println("run gc");
+ Runtime.getRuntime().gc();
+ Runtime.getRuntime().runFinalization();
+ if (fence.await(10, TimeUnit.SECONDS)) {
+ System.out.println("passed fence");
+ } else {
+ System.out.println("fence await timed out");
+ }
+ } catch (InterruptedException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+ }
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ final TestParameters parameters;
+
+ public AnonymousObjectWithFinalizeTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testReference() throws Exception {
+ testForRuntime(parameters.getRuntime(), parameters.getApiLevel())
+ .addProgramClassesAndInnerClasses(TestClass.class)
+ .run(parameters.getRuntime(), TestClass.class)
+ .assertSuccessWithOutput(EXPECTED);
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ testForR8(parameters.getBackend())
+ .setMinApi(parameters.getApiLevel())
+ .addKeepMainRule(TestClass.class)
+ .addProgramClassesAndInnerClasses(TestClass.class)
+ .run(parameters.getRuntime(), TestClass.class)
+ // TODO(b/143189461): Update to correct expected output once fixed.
+ .assertSuccessWithOutputLines(
+ "set up fence",
+ "run gc",
+ "fence await timed out");
+ }
+}