Avoid repeated attempts to load failing ApplicationContext in the TCF
This commit introduces initial support for a new "context failure threshold" feature in the Spring TestContext Framework (TCF). Specifically, DefaultCacheAwareContextLoaderDelegate now tracks how many times a failure occurs when attempting to load an ApplicationContext and preemptively throws an IllegalStateException for subsequent attempts to load the same context if the configured failure threshold has been exceeded. See gh-14182
This commit is contained in:
parent
ccb8db41f0
commit
c7ca5c81c3
|
|
@ -16,7 +16,9 @@
|
|||
|
||||
package org.springframework.test.context.cache;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
|
@ -60,6 +62,12 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
|
|||
|
||||
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.
|
||||
|
|
@ -74,6 +82,19 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
|
|||
|
||||
private final ContextCache contextCache;
|
||||
|
||||
/**
|
||||
* Map of context keys to context load failure counts.
|
||||
* @since 6.1
|
||||
*/
|
||||
private final Map<MergedContextConfiguration, Integer> failureCounts = new HashMap<>(32);
|
||||
|
||||
/**
|
||||
* The configured failure threshold for errors encountered while attempting to
|
||||
* load an {@link ApplicationContext}.
|
||||
* @since 6.1
|
||||
*/
|
||||
private final int failureThreshold;
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new {@code DefaultCacheAwareContextLoaderDelegate} using
|
||||
|
|
@ -95,6 +116,7 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
|
|||
public DefaultCacheAwareContextLoaderDelegate(ContextCache contextCache) {
|
||||
Assert.notNull(contextCache, "ContextCache must not be null");
|
||||
this.contextCache = contextCache;
|
||||
this.failureThreshold = DEFAULT_FAILURE_THRESHOLD;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -112,6 +134,13 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
|
|||
synchronized (this.contextCache) {
|
||||
ApplicationContext context = this.contextCache.get(mergedConfig);
|
||||
if (context == null) {
|
||||
Integer failureCount = this.failureCounts.getOrDefault(mergedConfig, 0);
|
||||
if (failureCount >= this.failureThreshold) {
|
||||
throw new IllegalStateException("""
|
||||
ApplicationContext failure threshold (%d) exceeded: \
|
||||
skipping repeated attempt to load context for %s"""
|
||||
.formatted(this.failureThreshold, mergedConfig));
|
||||
}
|
||||
try {
|
||||
if (mergedConfig instanceof AotMergedContextConfiguration aotMergedConfig) {
|
||||
context = loadContextInAotMode(aotMergedConfig);
|
||||
|
|
@ -126,6 +155,7 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
|
|||
this.contextCache.put(mergedConfig, context);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
this.failureCounts.put(mergedConfig, ++failureCount);
|
||||
Throwable cause = ex;
|
||||
if (ex instanceof ContextLoadException cle) {
|
||||
cause = cle.getCause();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.cache;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.springframework.test.context.cache.ContextCacheTestUtils.assertContextCacheStatistics;
|
||||
import static org.springframework.test.context.cache.ContextCacheTestUtils.resetContextCache;
|
||||
|
||||
/**
|
||||
* @author Sam Brannen
|
||||
* @since 6.1
|
||||
*/
|
||||
@SpringJUnitConfig
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
@Disabled
|
||||
class ContextFailureThresholdTests {
|
||||
|
||||
@BeforeAll
|
||||
static void verifyInitialCacheState() {
|
||||
resetContextCache();
|
||||
assertContextCacheStatistics("BeforeAll", 0, 0, 0);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void verifyFinalCacheState() {
|
||||
assertContextCacheStatistics("AfterAll", 0, 0, 3);
|
||||
resetContextCache();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void test1() {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void test2() {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void test3() {
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
static class FailingConfig {
|
||||
|
||||
@Bean
|
||||
String explosiveString() {
|
||||
throw new RuntimeException("Boom!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue