Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ default int getContextUsageCount() {
void reset();

/**
* Clear all contexts from the cache, clearing context hierarchy information as well.
* Clear all contexts from the cache, explicitly
* {@linkplain org.springframework.context.ConfigurableApplicationContext#close() closing}
* each context that is an instance of {@code ConfigurableApplicationContext}
* and clearing context hierarchy information as well.
*/
void clear();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@ public void reset() {
@Override
public void clear() {
synchronized (this.contextMap) {
for (MergedContextConfiguration key : new ArrayList<>(this.contextMap.keySet())) {
remove(key, HierarchyMode.CURRENT_LEVEL);
}
this.contextMap.clear();
this.hierarchyMap.clear();
this.contextUsageMap.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,42 @@ void maxCacheSizeZero() {
assertThatIllegalArgumentException().isThrownBy(() -> new DefaultContextCache(0));
}

@Test
void clearClosesContexts() {
DefaultContextCache cache = new DefaultContextCache(4);

cache.put(fooConfig, key -> fooContext);
cache.put(barConfig, key -> barContext);
cache.put(bazConfig, key -> bazContext);
assertCacheContents(cache, "Foo", "Bar", "Baz");

cache.clear();
assertCacheContents(cache);

verify(fooContext, times(1)).close();
verify(barContext, times(1)).close();
verify(bazContext, times(1)).close();
verify(abcContext, never()).close();
}

@Test
void resetClosesContexts() {
DefaultContextCache cache = new DefaultContextCache(4);

cache.put(fooConfig, key -> fooContext);
cache.put(barConfig, key -> barContext);
cache.get(fooConfig);
assertThat(cache.getHitCount()).isEqualTo(1);
assertCacheContents(cache, "Bar", "Foo");

cache.reset();
assertCacheContents(cache);
assertThat(cache.getHitCount()).isZero();

verify(fooContext, times(1)).close();
verify(barContext, times(1)).close();
}


@Nested
@SuppressWarnings("deprecation")
Expand Down