Make test context failure threshold configurable

This commit makes the failure threshold value configurable via a JVM
system property or Spring property named
"spring.test.context.failure.threshold".

See gh-14182
This commit is contained in:
Sam Brannen 2023-06-08 18:23:13 +02:00
parent c7ca5c81c3
commit 52ae97cffc
3 changed files with 74 additions and 16 deletions

View File

@ -36,6 +36,31 @@ import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
*/ */
public interface CacheAwareContextLoaderDelegate { public interface CacheAwareContextLoaderDelegate {
/**
* The default failure threshold for errors encountered while attempting to
* load an application context: {@value}.
* @since 6.1
* @see #CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME
*/
int DEFAULT_CONTEXT_FAILURE_THRESHOLD = 1;
/**
* System property used to configure the failure threshold for errors
* encountered while attempting to load an application context: {@value}.
* <p>May alternatively be configured via the
* {@link org.springframework.core.SpringProperties} mechanism.
* <p>Implementations of {@code CacheAwareContextLoaderDelegate} are not
* required to support this feature. Consult the documentation of the
* corresponding implementation for details. Note, however, that the standard
* {@code CacheAwareContextLoaderDelegate} implementation in Spring supports
* this feature.
* @since 6.1
* @see #DEFAULT_CONTEXT_FAILURE_THRESHOLD
* @see #loadContext(MergedContextConfiguration)
*/
String CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME = "spring.test.context.failure.threshold";
/** /**
* Determine if the {@linkplain ApplicationContext application context} for * Determine if the {@linkplain ApplicationContext application context} for
* the supplied {@link MergedContextConfiguration} has been loaded (i.e., * the supplied {@link MergedContextConfiguration} has been loaded (i.e.,
@ -72,6 +97,13 @@ public interface CacheAwareContextLoaderDelegate {
* mechanism, catch any exception thrown by the {@link ContextLoader}, and * mechanism, catch any exception thrown by the {@link ContextLoader}, and
* delegate to each of the configured failure processors to process the context * delegate to each of the configured failure processors to process the context
* load failure if the exception is an instance of {@link ContextLoadException}. * load failure if the exception is an instance of {@link ContextLoadException}.
* <p>As of Spring Framework 6.1, implementations of this method are encouraged
* to support the <em>failure threshold</em> feature. Specifically, if repeated
* attempts are made to load an application context and that application
* context consistently fails to load &mdash; for example, due to a configuration
* error that prevents the context from successfully loading &mdash; this
* method should preemptively throw an {@link IllegalStateException} if the
* configured failure threshold has been exceeded.
* <p>The cache statistics should be logged by invoking * <p>The cache statistics should be logged by invoking
* {@link org.springframework.test.context.cache.ContextCache#logStatistics()}. * {@link org.springframework.test.context.cache.ContextCache#logStatistics()}.
* @param mergedConfig the merged context configuration to use to load the * @param mergedConfig the merged context configuration to use to load the
@ -81,6 +113,7 @@ public interface CacheAwareContextLoaderDelegate {
* the application context * the application context
* @see #isContextLoaded * @see #isContextLoaded
* @see #closeContext * @see #closeContext
* @see #CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME
*/ */
ApplicationContext loadContext(MergedContextConfiguration mergedConfig); ApplicationContext loadContext(MergedContextConfiguration mergedConfig);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,10 +17,11 @@
package org.springframework.test.context.cache; package org.springframework.test.context.cache;
import org.springframework.core.SpringProperties; import org.springframework.core.SpringProperties;
import org.springframework.test.context.CacheAwareContextLoaderDelegate;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
* Collection of utilities for working with {@link ContextCache ContextCaches}. * Collection of utilities for working with context caching.
* *
* @author Sam Brannen * @author Sam Brannen
* @since 4.3 * @since 4.3
@ -30,17 +31,40 @@ public abstract class ContextCacheUtils {
/** /**
* Retrieve the maximum size of the {@link ContextCache}. * Retrieve the maximum size of the {@link ContextCache}.
* <p>Uses {@link SpringProperties} to retrieve a system property or Spring * <p>Uses {@link SpringProperties} to retrieve a system property or Spring
* property named {@code spring.test.context.cache.maxSize}. * property named {@value ContextCache#MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAME}.
* <p>Falls back to the value of the {@link ContextCache#DEFAULT_MAX_CONTEXT_CACHE_SIZE} * <p>Defaults to {@value ContextCache#DEFAULT_MAX_CONTEXT_CACHE_SIZE}
* if no such property has been set or if the property is not an integer. * if no such property has been set or if the property is not an integer.
* @return the maximum size of the context cache * @return the maximum size of the context cache
* @see ContextCache#MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAME * @see ContextCache#MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAME
*/ */
public static int retrieveMaxCacheSize() { public static int retrieveMaxCacheSize() {
String propertyName = ContextCache.MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAME;
int defaultValue = ContextCache.DEFAULT_MAX_CONTEXT_CACHE_SIZE;
return retrieveProperty(propertyName, defaultValue);
}
/**
* Retrieve the <em>failure threshold</em> for application context loading.
* <p>Uses {@link SpringProperties} to retrieve a system property or Spring
* property named {@value CacheAwareContextLoaderDelegate#CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME}.
* <p>Defaults to {@value CacheAwareContextLoaderDelegate#DEFAULT_CONTEXT_FAILURE_THRESHOLD}
* if no such property has been set or if the property is not an integer.
* @return the failure threshold
* @since 6.1
* @see CacheAwareContextLoaderDelegate#CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME
* @see CacheAwareContextLoaderDelegate#DEFAULT_CONTEXT_FAILURE_THRESHOLD
*/
public static int retrieveContextFailureThreshold() {
String propertyName = CacheAwareContextLoaderDelegate.CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME;
int defaultValue = CacheAwareContextLoaderDelegate.DEFAULT_CONTEXT_FAILURE_THRESHOLD;
return retrieveProperty(propertyName, defaultValue);
}
private static int retrieveProperty(String key, int defaultValue) {
try { try {
String maxSize = SpringProperties.getProperty(ContextCache.MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAME); String value = SpringProperties.getProperty(key);
if (StringUtils.hasText(maxSize)) { if (StringUtils.hasText(value)) {
return Integer.parseInt(maxSize.trim()); return Integer.parseInt(value.trim());
} }
} }
catch (Exception ex) { catch (Exception ex) {
@ -48,7 +72,7 @@ public abstract class ContextCacheUtils {
} }
// Fallback // Fallback
return ContextCache.DEFAULT_MAX_CONTEXT_CACHE_SIZE; return defaultValue;
} }
} }

View File

@ -62,13 +62,6 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
private static final Log logger = LogFactory.getLog(DefaultCacheAwareContextLoaderDelegate.class); private static final Log logger = LogFactory.getLog(DefaultCacheAwareContextLoaderDelegate.class);
/**
* The default failure threshold for errors encountered while attempting to
* load an {@link ApplicationContext}: {@value}.
* @since 6.1
*/
private static final int DEFAULT_FAILURE_THRESHOLD = 1;
/** /**
* Default static cache of Spring application contexts. * Default static cache of Spring application contexts.
*/ */
@ -114,9 +107,17 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
* @see #DefaultCacheAwareContextLoaderDelegate() * @see #DefaultCacheAwareContextLoaderDelegate()
*/ */
public DefaultCacheAwareContextLoaderDelegate(ContextCache contextCache) { public DefaultCacheAwareContextLoaderDelegate(ContextCache contextCache) {
this(contextCache, ContextCacheUtils.retrieveContextFailureThreshold());
}
/**
* @since 6.1
*/
DefaultCacheAwareContextLoaderDelegate(ContextCache contextCache, int failureThreshold) {
Assert.notNull(contextCache, "ContextCache must not be null"); Assert.notNull(contextCache, "ContextCache must not be null");
Assert.isTrue(failureThreshold > 0, "'failureThreshold' must be positive");
this.contextCache = contextCache; this.contextCache = contextCache;
this.failureThreshold = DEFAULT_FAILURE_THRESHOLD; this.failureThreshold = failureThreshold;
} }