forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit size of context cache in the TestContext framework
Prior to this commit, the size of the ApplicationContext cache in the Spring TestContext Framework could grow without bound, leading to issues with memory and performance in large test suites. This commit addresses this issue by introducing support for setting the maximum cache size via a JVM system property or Spring property called "spring.test.context.cache.maxSize". If no such property is set, a default value of 32 will be used. Furthermore, the DefaultContextCache has been refactored to use a synchronized LRU cache internally instead of a ConcurrentHashMap. The LRU cache is a simple bounded cache with a "least recently used" (LRU) eviction policy. Issue: SPR-8055
- Loading branch information
Showing
9 changed files
with
452 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
spring-test/src/main/java/org/springframework/test/context/cache/ContextCacheUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2002-2016 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 | ||
* | ||
* http://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.springframework.core.SpringProperties; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* Collection of utilities for working with {@link ContextCache ContextCaches}. | ||
* | ||
* @author Sam Brannen | ||
* @since 4.3 | ||
*/ | ||
public abstract class ContextCacheUtils { | ||
|
||
private ContextCacheUtils() { | ||
/* no-op */ | ||
} | ||
|
||
|
||
/** | ||
* Retrieve the maximum size of the {@link ContextCache}. | ||
* <p>Uses {@link SpringProperties} to retrieve a system property or Spring | ||
* property named {@code spring.test.context.cache.maxSize}. | ||
* <p>Falls back to the value of the {@link ContextCache#DEFAULT_MAX_CONTEXT_CACHE_SIZE} | ||
* if no such property has been set or if the property is not an integer. | ||
* @return the maximum size of the context cache | ||
* @see ContextCache#MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAME | ||
*/ | ||
public static int retrieveMaxCacheSize() { | ||
try { | ||
String maxSize = SpringProperties.getProperty(ContextCache.MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAME); | ||
if (StringUtils.hasText(maxSize)) { | ||
return Integer.parseInt(maxSize.trim()); | ||
} | ||
} | ||
catch (Exception ex) { | ||
/* ignore */ | ||
} | ||
|
||
// Fallback | ||
return ContextCache.DEFAULT_MAX_CONTEXT_CACHE_SIZE; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.