Skip to content

Commit

Permalink
Make Java finalize test more reliable (closes #1061)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsmith committed Dec 23, 2023
1 parent ea0fd78 commit d410429
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions product/runtime/src/test/java/com/chaquo/java/PyObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ public void size() {
}

// ==== Object ===========================================================

@SuppressWarnings("AssertEqualsBetweenInconvertibleTypes")
@Test
public void equals() {
Expand Down Expand Up @@ -812,19 +812,22 @@ public void repr() {
assertEquals("'hello'", pyobjecttest.get("str_var").repr());
}

@SuppressWarnings({"UnusedAssignment", "unused"})
@Test
public void finalize_() {
PyObject DT = pyobjecttest.get("DelTrigger");
PyObject TestCase = python.getModule("unittest").get("TestCase");
PyObject test = TestCase.call("__init__"); // https://stackoverflow.com/a/18084492/220765
finalizeInner(DT, test);
DT.callAttr("assertTriggered", test, true);
}

// Setting a local variable to null doesn't reliably release the reference in a
// release build (#1061), so we wrap the reference's lifetime in a method call.
private void finalizeInner(PyObject DT, PyObject test) {
DT.callAttr("reset");
PyObject dt = DT.call();
DT.callAttr("assertTriggered", test, false);
dt.toString(); // Prevent Android Studio 3.1 release build from nulling dt prematurely.
dt = null;
DT.callAttr("assertTriggered", test, true);
dt.id(); // Prevent Java from releasing dt before the first assertTriggered.
}

}

0 comments on commit d410429

Please sign in to comment.