|
27 | 27 | import java.util.ArrayList; |
28 | 28 | import java.util.Collection; |
29 | 29 | import java.util.List; |
| 30 | +import java.util.Optional; |
30 | 31 | import java.util.concurrent.CompletableFuture; |
31 | 32 | import java.util.concurrent.CompletionStage; |
32 | 33 | import java.util.concurrent.ExecutionException; |
@@ -826,6 +827,20 @@ public void should_Accept_a_custom_cache_map_implementation() throws ExecutionEx |
826 | 827 | assertArrayEquals(customMap.stash.keySet().toArray(), emptyList().toArray()); |
827 | 828 | } |
828 | 829 |
|
| 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 | + |
829 | 844 | @Test |
830 | 845 | public void batching_disabled_should_dispatch_immediately() { |
831 | 846 | List<Collection<String>> loadCalls = new ArrayList<>(); |
@@ -1097,10 +1112,15 @@ private static DataLoader<Integer, Object> idLoaderOddEvenExceptions( |
1097 | 1112 | }, options); |
1098 | 1113 | } |
1099 | 1114 |
|
1100 | | - |
1101 | 1115 | private <T> BatchLoader<T, T> keysAsValues() { |
1102 | 1116 | return CompletableFuture::completedFuture; |
1103 | 1117 | } |
1104 | 1118 |
|
| 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 | + } |
1105 | 1125 | } |
1106 | 1126 |
|
0 commit comments