44import com .github .benmanes .caffeine .cache .Caffeine ;
55import org .dataloader .fixtures .CaffeineValueCache ;
66import org .dataloader .fixtures .CustomValueCache ;
7- import org .dataloader .fixtures .TestKit ;
87import org .dataloader .impl .DataLoaderAssertionException ;
98import org .junit .Test ;
109
2019import static org .awaitility .Awaitility .await ;
2120import static org .dataloader .DataLoaderOptions .newOptions ;
2221import static org .dataloader .fixtures .TestKit .idLoader ;
22+ import static org .dataloader .fixtures .TestKit .snooze ;
23+ import static org .dataloader .fixtures .TestKit .sort ;
2324import static org .dataloader .impl .CompletableFutureKit .failedFuture ;
2425import static org .hamcrest .Matchers .equalTo ;
2526import static org .junit .Assert .assertArrayEquals ;
@@ -48,6 +49,7 @@ public void test_by_default_we_have_no_value_caching() {
4849 assertThat (loadCalls , equalTo (singletonList (asList ("a" , "b" ))));
4950
5051 // futures are still cached but not values
52+ loadCalls .clear ();
5153
5254 fA = identityLoader .load ("a" );
5355 fB = identityLoader .load ("b" );
@@ -59,8 +61,7 @@ public void test_by_default_we_have_no_value_caching() {
5961 assertThat (fA .join (), equalTo ("a" ));
6062 assertThat (fB .join (), equalTo ("b" ));
6163
62- assertThat (loadCalls , equalTo (singletonList (asList ("a" , "b" ))));
63-
64+ assertThat (loadCalls , equalTo (emptyList ()));
6465 }
6566
6667 @ Test
@@ -213,12 +214,12 @@ public void caching_can_take_some_time_complete() {
213214 public CompletableFuture <Object > get (String key ) {
214215 if (key .startsWith ("miss" )) {
215216 return CompletableFuture .supplyAsync (() -> {
216- TestKit . snooze (1000 );
217+ snooze (1000 );
217218 throw new IllegalStateException ("no a in cache" );
218219 });
219220 } else {
220221 return CompletableFuture .supplyAsync (() -> {
221- TestKit . snooze (1000 );
222+ snooze (1000 );
222223 return key ;
223224 });
224225 }
@@ -261,7 +262,7 @@ public CompletableFuture<List<Try<Object>>> getValues(List<String> keys) {
261262 }
262263 }
263264 return CompletableFuture .supplyAsync (() -> {
264- TestKit . snooze (1000 );
265+ snooze (1000 );
265266 return cacheCalls ;
266267 });
267268 }
@@ -364,4 +365,91 @@ public CompletableFuture<Object> get(String key) {
364365 assertThat (getCalls .get (), equalTo (0 ));
365366 assertTrue (customValueCache .asMap ().isEmpty ());
366367 }
368+
369+ @ Test
370+ public void if_everything_is_cached_no_batching_happens () {
371+ AtomicInteger getCalls = new AtomicInteger ();
372+ AtomicInteger setCalls = new AtomicInteger ();
373+ CustomValueCache customValueCache = new CustomValueCache () {
374+
375+ @ Override
376+ public CompletableFuture <Object > get (String key ) {
377+ getCalls .incrementAndGet ();
378+ return super .get (key );
379+ }
380+
381+ @ Override
382+ public CompletableFuture <List <Object >> setValues (List <String > keys , List <Object > values ) {
383+ setCalls .incrementAndGet ();
384+ return super .setValues (keys , values );
385+ }
386+ };
387+ customValueCache .asMap ().put ("a" , "cachedA" );
388+ customValueCache .asMap ().put ("b" , "cachedB" );
389+ customValueCache .asMap ().put ("c" , "cachedC" );
390+
391+ List <List <String >> loadCalls = new ArrayList <>();
392+ DataLoaderOptions options = newOptions ().setValueCache (customValueCache ).setCachingEnabled (true );
393+ DataLoader <String , String > identityLoader = idLoader (options , loadCalls );
394+
395+ CompletableFuture <String > fA = identityLoader .load ("a" );
396+ CompletableFuture <String > fB = identityLoader .load ("b" );
397+ CompletableFuture <String > fC = identityLoader .load ("c" );
398+
399+ await ().until (identityLoader .dispatch ()::isDone );
400+
401+ assertThat (fA .join (), equalTo ("cachedA" ));
402+ assertThat (fB .join (), equalTo ("cachedB" ));
403+ assertThat (fC .join (), equalTo ("cachedC" ));
404+
405+ assertThat (loadCalls , equalTo (emptyList ()));
406+ assertThat (getCalls .get (), equalTo (3 ));
407+ assertThat (setCalls .get (), equalTo (0 ));
408+ }
409+
410+
411+ @ Test
412+ public void if_batching_is_off_it_still_can_cache () {
413+ AtomicInteger getCalls = new AtomicInteger ();
414+ AtomicInteger setCalls = new AtomicInteger ();
415+ CustomValueCache customValueCache = new CustomValueCache () {
416+
417+ @ Override
418+ public CompletableFuture <Object > get (String key ) {
419+ getCalls .incrementAndGet ();
420+ return super .get (key );
421+ }
422+
423+ @ Override
424+ public CompletableFuture <List <Object >> setValues (List <String > keys , List <Object > values ) {
425+ setCalls .incrementAndGet ();
426+ return super .setValues (keys , values );
427+ }
428+ };
429+ customValueCache .asMap ().put ("a" , "cachedA" );
430+
431+ List <List <String >> loadCalls = new ArrayList <>();
432+ DataLoaderOptions options = newOptions ().setValueCache (customValueCache ).setCachingEnabled (true ).setBatchingEnabled (false );
433+ DataLoader <String , String > identityLoader = idLoader (options , loadCalls );
434+
435+ CompletableFuture <String > fA = identityLoader .load ("a" );
436+ CompletableFuture <String > fB = identityLoader .load ("b" );
437+ CompletableFuture <String > fC = identityLoader .load ("c" );
438+
439+ assertTrue (fA .isDone ()); // with batching off they are dispatched immediately
440+ assertTrue (fB .isDone ());
441+ assertTrue (fC .isDone ());
442+
443+ await ().until (identityLoader .dispatch ()::isDone );
444+
445+ assertThat (fA .join (), equalTo ("cachedA" ));
446+ assertThat (fB .join (), equalTo ("b" ));
447+ assertThat (fC .join (), equalTo ("c" ));
448+
449+ assertThat (loadCalls , equalTo (asList (singletonList ("b" ), singletonList ("c" ))));
450+ assertThat (getCalls .get (), equalTo (3 ));
451+ assertThat (setCalls .get (), equalTo (2 ));
452+
453+ assertThat (sort (customValueCache .asMap ().values ()), equalTo (sort (asList ("b" , "c" , "cachedA" ))));
454+ }
367455}
0 commit comments