Skip to content

Commit 1bb63aa

Browse files
authored
Merge pull request #147 from AlexandreCarlton/add-check-to-ensure-we-catch-failing-cache-get
Verify a throwing CacheMap#get does not break DataLoader
2 parents 3a16d9c + 42cab40 commit 1bb63aa

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/test/java/org/dataloader/DataLoaderTest.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.ArrayList;
2828
import java.util.Collection;
2929
import java.util.List;
30+
import java.util.Optional;
3031
import java.util.concurrent.CompletableFuture;
3132
import java.util.concurrent.CompletionStage;
3233
import java.util.concurrent.ExecutionException;
@@ -826,6 +827,20 @@ public void should_Accept_a_custom_cache_map_implementation() throws ExecutionEx
826827
assertArrayEquals(customMap.stash.keySet().toArray(), emptyList().toArray());
827828
}
828829

830+
@Test
831+
public void should_degrade_gracefully_if_cache_get_throws() {
832+
CacheMap<String, Object> cache = new ThrowingCacheMap();
833+
DataLoaderOptions options = newOptions().setCachingEnabled(true).setCacheMap(cache);
834+
List<Collection<String>> loadCalls = new ArrayList<>();
835+
DataLoader<String, String> identityLoader = idLoader(options, loadCalls);
836+
837+
assertThat(identityLoader.getIfPresent("a"), equalTo(Optional.empty()));
838+
839+
CompletableFuture<String> future = identityLoader.load("a");
840+
identityLoader.dispatch();
841+
assertThat(future.join(), equalTo("a"));
842+
}
843+
829844
@Test
830845
public void batching_disabled_should_dispatch_immediately() {
831846
List<Collection<String>> loadCalls = new ArrayList<>();
@@ -1097,10 +1112,15 @@ private static DataLoader<Integer, Object> idLoaderOddEvenExceptions(
10971112
}, options);
10981113
}
10991114

1100-
11011115
private <T> BatchLoader<T, T> keysAsValues() {
11021116
return CompletableFuture::completedFuture;
11031117
}
11041118

1119+
private static class ThrowingCacheMap extends CustomCacheMap {
1120+
@Override
1121+
public CompletableFuture<Object> get(String key) {
1122+
throw new RuntimeException("Cache implementation failed.");
1123+
}
1124+
}
11051125
}
11061126

0 commit comments

Comments
 (0)