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 <mail@eliasnaur.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
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 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();