Browse Source

Ensure context cache stats are logged when ApplicationContext fails to load

Closes gh-30635
pull/30651/head
Sam Brannen 3 years ago
parent
commit
1a26e17f41
  1. 71
      spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java

71
spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java vendored

@ -109,48 +109,51 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
mergedContextConfiguration = replaceIfNecessary(mergedContextConfiguration); mergedContextConfiguration = replaceIfNecessary(mergedContextConfiguration);
synchronized (this.contextCache) { synchronized (this.contextCache) {
ApplicationContext context = this.contextCache.get(mergedContextConfiguration); ApplicationContext context = this.contextCache.get(mergedContextConfiguration);
if (context == null) { try {
try { if (context == null) {
if (mergedContextConfiguration instanceof AotMergedContextConfiguration aotMergedConfig) { try {
context = loadContextInAotMode(aotMergedConfig); if (mergedContextConfiguration instanceof AotMergedContextConfiguration aotMergedConfig) {
} context = loadContextInAotMode(aotMergedConfig);
else { }
context = loadContextInternal(mergedContextConfiguration); else {
} context = loadContextInternal(mergedContextConfiguration);
if (logger.isTraceEnabled()) { }
logger.trace("Storing ApplicationContext [%s] in cache under key %s".formatted( if (logger.isTraceEnabled()) {
System.identityHashCode(context), mergedContextConfiguration)); logger.trace("Storing ApplicationContext [%s] in cache under key %s".formatted(
System.identityHashCode(context), mergedContextConfiguration));
}
this.contextCache.put(mergedContextConfiguration, context);
} }
this.contextCache.put(mergedContextConfiguration, context); catch (Exception ex) {
} Throwable cause = ex;
catch (Exception ex) { if (ex instanceof ContextLoadException cle) {
Throwable cause = ex; cause = cle.getCause();
if (ex instanceof ContextLoadException cle) { for (ApplicationContextFailureProcessor contextFailureProcessor : this.contextFailureProcessors) {
cause = cle.getCause(); try {
for (ApplicationContextFailureProcessor contextFailureProcessor : this.contextFailureProcessors) { contextFailureProcessor.processLoadFailure(cle.getApplicationContext(), cause);
try { }
contextFailureProcessor.processLoadFailure(cle.getApplicationContext(), cause); catch (Throwable throwable) {
} if (logger.isDebugEnabled()) {
catch (Throwable throwable) { logger.debug("Ignoring exception thrown from ApplicationContextFailureProcessor [%s]: %s"
if (logger.isDebugEnabled()) { .formatted(contextFailureProcessor, throwable));
logger.debug("Ignoring exception thrown from ApplicationContextFailureProcessor [%s]: %s" }
.formatted(contextFailureProcessor, throwable));
} }
} }
} }
throw new IllegalStateException(
"Failed to load ApplicationContext for " + mergedContextConfiguration, cause);
} }
throw new IllegalStateException(
"Failed to load ApplicationContext for " + mergedContextConfiguration, cause);
} }
} else {
else { if (logger.isTraceEnabled()) {
if (logger.isTraceEnabled()) { logger.trace("Retrieved ApplicationContext [%s] from cache with key %s".formatted(
logger.trace("Retrieved ApplicationContext [%s] from cache with key %s".formatted( System.identityHashCode(context), mergedContextConfiguration));
System.identityHashCode(context), mergedContextConfiguration)); }
} }
} }
finally {
this.contextCache.logStatistics(); this.contextCache.logStatistics();
}
return context; return context;
} }

Loading…
Cancel
Save