Skip to content

Commit

Permalink
Verify a throwing CacheMap#get does not break DataLoader
Browse files Browse the repository at this point in the history
As a fast follow to #146, we add a regression test to ensure that we
still handle a `CacheMap` whose `#get` throws an exception (by falling
back to the underlying batch loader).
  • Loading branch information
AlexandreCarlton committed Apr 30, 2024
1 parent 3a16d9c commit 42cab40
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 42cab40

Please sign in to comment.