From f2ef6f42b753463b67942a1ef99d2ace7901bdec Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Thu, 14 Mar 2019 17:25:40 +0100 Subject: [PATCH] bind/java: deflake testJavaRefKeep Retry a few times to ensure the garbage collector collects what we expect. Also merge and remove testJavaRefGC which was identical except for one line. Updates golang/go#30785 Change-Id: Id6c541b41fe483633c40eeea712c1f43b647e4f4 Reviewed-on: https://go-review.googlesource.com/c/mobile/+/167657 Run-TryBot: Elias Naur TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- bind/java/SeqTest.java | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/bind/java/SeqTest.java b/bind/java/SeqTest.java index ccaa813..f89693c 100644 --- a/bind/java/SeqTest.java +++ b/bind/java/SeqTest.java @@ -325,25 +325,27 @@ public class SeqTest extends InstrumentationTestCase { } } - public void testJavaRefGC() { + public void testJavaRefKeep() { finalizedAnI = false; AnI obj = new AnI_Traced(); Testpkg.callF(obj); assertTrue("want F to be called", obj.calledF); Testpkg.callF(obj); obj = null; - runGC(); - assertTrue("want obj to be collected", finalizedAnI); - } - - public void testJavaRefKeep() { - finalizedAnI = false; - AnI obj = new AnI_Traced(); - Testpkg.callF(obj); - Testpkg.callF(obj); - obj = null; - runGC(); - assertTrue("want obj not to be kept by Go", finalizedAnI); + int attempts = 0; + while (true) { + runGC(); + if (finalizedAnI) + break; + attempts++; + try { + Thread.sleep(100); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + if (attempts >= 10) + fail("want obj not to be kept by Go; tried " + attempts + " garbage collections."); + } finalizedAnI = false; obj = new AnI_Traced();