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.
This commit is contained in:
Florian Schmaus 2019-05-09 12:09:06 +02:00
parent 2dedd75cd7
commit 04f1d79d72
1 changed files with 20 additions and 3 deletions

View File

@ -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();
}