From 04f1d79d720e00aa564f60374f98f033c591d3b8 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 9 May 2019 12:09:06 +0200 Subject: [PATCH] Try GC in MemoryLeakTestUtil.assertReferencesQueueSize() It appears that we observe a partion GC run on some systems, especially ones with few resources. Hopefully this increases the chances to observe the expected GC affects so that the unit test passes also on those systems. --- .../smack/util/MemoryLeakTestUtil.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/smack-core/src/test/java/org/jivesoftware/smack/util/MemoryLeakTestUtil.java b/smack-core/src/test/java/org/jivesoftware/smack/util/MemoryLeakTestUtil.java index 7e41d5793..1b453979f 100644 --- a/smack-core/src/test/java/org/jivesoftware/smack/util/MemoryLeakTestUtil.java +++ b/smack-core/src/test/java/org/jivesoftware/smack/util/MemoryLeakTestUtil.java @@ -16,8 +16,8 @@ */ package org.jivesoftware.smack.util; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.fail; import java.lang.ref.PhantomReference; import java.lang.ref.Reference; @@ -105,9 +105,26 @@ public class MemoryLeakTestUtil { private static void assertReferencesQueueSize(ReferenceQueue referenceQueue, int expectedSize) throws IllegalArgumentException, InterruptedException { final int timeout = 120000; + final int maxAttempts = 3; for (int itemsRemoved = 0; itemsRemoved < expectedSize; ++itemsRemoved) { - Reference reference = referenceQueue.remove(timeout); - assertNotNull("No reference found after " + timeout + "ms", reference); + int attempt = 0; + Reference reference = null; + do { + reference = referenceQueue.remove(timeout); + if (reference != null) { + break; + } + + attempt++; + String message = "No reference to a gc'ed object found after " + timeout + "ms in the " + attempt + + ". attempt."; + if (attempt >= maxAttempts) { + fail(message); + } + + LOGGER.warning(message); + triggerGarbageCollection(); + } while (true); reference.clear(); }