Skip to content

Commit

Permalink
Merge pull request #147 from AlexandreCarlton/add-check-to-ensure-we-…
Browse files Browse the repository at this point in the history
…catch-failing-cache-get

Verify a throwing CacheMap#get does not break DataLoader
  • Loading branch information
bbakerman authored Apr 30, 2024
2 parents 3a16d9c + 42cab40 commit 1bb63aa
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/test/java/org/dataloader/DataLoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -826,6 +827,20 @@ public void should_Accept_a_custom_cache_map_implementation() throws ExecutionEx
assertArrayEquals(customMap.stash.keySet().toArray(), emptyList().toArray());
}

@Test
public void should_degrade_gracefully_if_cache_get_throws() {
CacheMap<String, Object> cache = new ThrowingCacheMap();
DataLoaderOptions options = newOptions().setCachingEnabled(true).setCacheMap(cache);
List<Collection<String>> loadCalls = new ArrayList<>();
DataLoader<String, String> identityLoader = idLoader(options, loadCalls);

assertThat(identityLoader.getIfPresent("a"), equalTo(Optional.empty()));

CompletableFuture<String> future = identityLoader.load("a");
identityLoader.dispatch();
assertThat(future.join(), equalTo("a"));
}

@Test
public void batching_disabled_should_dispatch_immediately() {
List<Collection<String>> loadCalls = new ArrayList<>();
Expand Down Expand Up @@ -1097,10 +1112,15 @@ private static DataLoader<Integer, Object> idLoaderOddEvenExceptions(
}, options);
}


private <T> BatchLoader<T, T> keysAsValues() {
return CompletableFuture::completedFuture;
}

private static class ThrowingCacheMap extends CustomCacheMap {
@Override
public CompletableFuture<Object> get(String key) {
throw new RuntimeException("Cache implementation failed.");
}
}
}

0 comments on commit 1bb63aa

Please sign in to comment.