|
21 | 21 | import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely;
|
22 | 22 |
|
23 | 23 | import java.util.ArrayList;
|
| 24 | +import java.util.Collections; |
24 | 25 | import java.util.List;
|
25 | 26 | import java.util.concurrent.ExecutorService;
|
26 | 27 | import java.util.concurrent.Executors;
|
@@ -113,6 +114,37 @@ void when_cacheFillsUp_ValuesAreEvictedFromCache() {
|
113 | 114 | verify(simpleValueSupplier, times(1)).apply(simpleTestKeys.get(4));
|
114 | 115 | }
|
115 | 116 |
|
| 117 | + @Test |
| 118 | + void when_closeableValuesAreEvicted_CloseMethodIsCalled() { |
| 119 | + int cacheSize = 3; |
| 120 | + int evictNum = 2; |
| 121 | + LruCache<Integer, CloseableClass> cache = LruCache.builder(CloseableClass::new) |
| 122 | + .maxSize(cacheSize) |
| 123 | + .build(); |
| 124 | + CloseableClass.reset(); |
| 125 | + for (int i = 0; i < cacheSize + evictNum; i++) { |
| 126 | + cache.get(i); |
| 127 | + } |
| 128 | + assertThat(CloseableClass.evictedItems()).isNotEmpty(); |
| 129 | + assertThat(CloseableClass.evictedItems()).hasSize(evictNum); |
| 130 | + assertThat(CloseableClass.evictedItems().get(0)).isEqualTo(0); |
| 131 | + assertThat(CloseableClass.evictedItems().get(1)).isEqualTo(1); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void when_closeableValuesAreEvicted_NoExceptionsAreThrownIfCloseFails() { |
| 136 | + int cacheSize = 3; |
| 137 | + int evictNum = 2; |
| 138 | + LruCache<Integer, FaultyCloseableClass> cache = LruCache.builder(FaultyCloseableClass::new) |
| 139 | + .maxSize(cacheSize) |
| 140 | + .build(); |
| 141 | + CloseableClass.reset(); |
| 142 | + for (int i = 0; i < cacheSize + evictNum; i++) { |
| 143 | + cache.get(i); |
| 144 | + } |
| 145 | + assertThat(CloseableClass.evictedItems()).isEmpty(); |
| 146 | + } |
| 147 | + |
116 | 148 | @Test
|
117 | 149 | void when_mostRecentValueIsHit_ValuesAreReorderedCorrectly() {
|
118 | 150 | LruCache<Integer, String> cache = simpleCache.get();
|
@@ -257,4 +289,42 @@ public String apply(Integer key) {
|
257 | 289 | return value;
|
258 | 290 | }
|
259 | 291 | }
|
| 292 | + |
| 293 | + private static class CloseableClass implements AutoCloseable { |
| 294 | + |
| 295 | + private static List<Integer> evictedList = new ArrayList<>(); |
| 296 | + |
| 297 | + private final Integer key; |
| 298 | + CloseableClass(Integer key) { |
| 299 | + this.key = key; |
| 300 | + } |
| 301 | + public Integer get() throws Exception { |
| 302 | + return key; |
| 303 | + } |
| 304 | + |
| 305 | + public static void reset() { |
| 306 | + evictedList = new ArrayList<>(); |
| 307 | + } |
| 308 | + |
| 309 | + public static List<Integer> evictedItems() { |
| 310 | + return Collections.unmodifiableList(evictedList); |
| 311 | + } |
| 312 | + |
| 313 | + @Override |
| 314 | + public void close() { |
| 315 | + evictedList.add(key); |
| 316 | + } |
| 317 | + } |
| 318 | + |
| 319 | + private static class FaultyCloseableClass extends CloseableClass { |
| 320 | + |
| 321 | + FaultyCloseableClass(Integer key) { |
| 322 | + super(key); |
| 323 | + } |
| 324 | + |
| 325 | + @Override |
| 326 | + public void close() { |
| 327 | + throw new RuntimeException("Could not close resources!"); |
| 328 | + } |
| 329 | + } |
260 | 330 | }
|
0 commit comments