From bff19246c259dd6a133c327ffa0c2da613fff2a8 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Tue, 2 Jul 2024 10:22:44 +0200 Subject: [PATCH] Clear the Introspector caches in tests The introspector cache keeps a reference to the class loader and it might be some reason for our class loader leaks. Note that the calls to the Introspector are coming from REST Assured and especially from their Groovy machinery. --- .../main/java/io/quarkus/test/ClearCache.java | 19 +++++++++++++++++-- .../io/quarkus/test/QuarkusDevModeTest.java | 2 +- .../java/io/quarkus/test/QuarkusUnitTest.java | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/test-framework/junit5-internal/src/main/java/io/quarkus/test/ClearCache.java b/test-framework/junit5-internal/src/main/java/io/quarkus/test/ClearCache.java index 7312295a4c937..409b5e930b9f5 100644 --- a/test-framework/junit5-internal/src/main/java/io/quarkus/test/ClearCache.java +++ b/test-framework/junit5-internal/src/main/java/io/quarkus/test/ClearCache.java @@ -1,5 +1,6 @@ package io.quarkus.test; +import java.beans.Introspector; import java.lang.reflect.Field; import java.util.Map; @@ -10,14 +11,28 @@ public class ClearCache { private static final Logger log = Logger.getLogger(ClearCache.class); - public static void clearAnnotationCache() { + public static void clearCaches() { + clearAnnotationCache(); + clearBeansIntrospectorCache(); + } + + private static void clearAnnotationCache() { try { Field f = AnnotationUtils.class.getDeclaredField("repeatableAnnotationContainerCache"); f.setAccessible(true); ((Map) (f.get(null))).clear(); } catch (NoSuchFieldException | IllegalAccessException e) { //ignore - log.debug("Failed to clear cache", e); + log.debug("Failed to clear annotation cache", e); + } + } + + private static void clearBeansIntrospectorCache() { + try { + Introspector.flushCaches(); + } catch (Exception e) { + //ignore + log.debug("Failed to clear java.beans.Introspector cache", e); } } } diff --git a/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusDevModeTest.java b/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusDevModeTest.java index cb2dd82bcd2a7..1cad2435abab8 100644 --- a/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusDevModeTest.java +++ b/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusDevModeTest.java @@ -324,7 +324,7 @@ public void afterAll(ExtensionContext context) throws Exception { rootLogger.setHandlers(originalRootLoggerHandlers); inMemoryLogHandler.clearRecords(); inMemoryLogHandler.setFilter(null); - ClearCache.clearAnnotationCache(); + ClearCache.clearCaches(); TestConfigUtil.cleanUp(); } diff --git a/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusUnitTest.java b/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusUnitTest.java index 9b37b88520986..cf96eab2d6c90 100644 --- a/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusUnitTest.java +++ b/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusUnitTest.java @@ -769,7 +769,7 @@ public void afterAll(ExtensionContext extensionContext) throws Exception { if (afterAllCustomizer != null) { afterAllCustomizer.run(); } - ClearCache.clearAnnotationCache(); + ClearCache.clearCaches(); TestConfigUtil.cleanUp(); } if (records != null) {