1818
1919import io .vertx .core .CompositeFuture ;
2020import io .vertx .core .Future ;
21+ import io .vertx .core .json .JsonObject ;
2122import io .vertx .ext .unit .junit .RunTestOnContext ;
2223import io .vertx .ext .unit .junit .VertxUnitRunner ;
2324import org .junit .Before ;
@@ -54,7 +55,7 @@ public class DataLoaderTest {
5455
5556 @ Before
5657 public void setUp () {
57- identityLoader = idLoader (new DataLoaderOptions <> (), new ArrayList <>());
58+ identityLoader = idLoader (new DataLoaderOptions (), new ArrayList <>());
5859 }
5960
6061 @ Test
@@ -108,7 +109,7 @@ public void should_Resolve_to_empty_list_when_no_keys_supplied() {
108109 @ Test
109110 public void should_Batch_multiple_requests () {
110111 ArrayList <Collection > loadCalls = new ArrayList <>();
111- DataLoader <Integer , Integer > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
112+ DataLoader <Integer , Integer > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
112113
113114 Future <Integer > future1 = identityLoader .load (1 );
114115 Future <Integer > future2 = identityLoader .load (2 );
@@ -123,7 +124,7 @@ public void should_Batch_multiple_requests() {
123124 @ Test
124125 public void should_Coalesce_identical_requests () {
125126 ArrayList <Collection > loadCalls = new ArrayList <>();
126- DataLoader <Integer , Integer > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
127+ DataLoader <Integer , Integer > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
127128
128129 Future <Integer > future1a = identityLoader .load (1 );
129130 Future <Integer > future1b = identityLoader .load (1 );
@@ -139,7 +140,7 @@ public void should_Coalesce_identical_requests() {
139140 @ Test
140141 public void should_Cache_repeated_requests () {
141142 ArrayList <Collection > loadCalls = new ArrayList <>();
142- DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
143+ DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
143144
144145 Future <String > future1 = identityLoader .load ("A" );
145146 Future <String > future2 = identityLoader .load ("B" );
@@ -174,7 +175,7 @@ public void should_Cache_repeated_requests() {
174175 @ Test
175176 public void should_Clear_single_value_in_loader () {
176177 ArrayList <Collection > loadCalls = new ArrayList <>();
177- DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
178+ DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
178179
179180 Future <String > future1 = identityLoader .load ("A" );
180181 Future <String > future2 = identityLoader .load ("B" );
@@ -200,7 +201,7 @@ public void should_Clear_single_value_in_loader() {
200201 @ Test
201202 public void should_Clear_all_values_in_loader () {
202203 ArrayList <Collection > loadCalls = new ArrayList <>();
203- DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
204+ DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
204205
205206 Future <String > future1 = identityLoader .load ("A" );
206207 Future <String > future2 = identityLoader .load ("B" );
@@ -226,7 +227,7 @@ public void should_Clear_all_values_in_loader() {
226227 @ Test
227228 public void should_Allow_priming_the_cache () {
228229 ArrayList <Collection > loadCalls = new ArrayList <>();
229- DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
230+ DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
230231
231232 identityLoader .prime ("A" , "A" );
232233
@@ -243,7 +244,7 @@ public void should_Allow_priming_the_cache() {
243244 @ Test
244245 public void should_Not_prime_keys_that_already_exist () {
245246 ArrayList <Collection > loadCalls = new ArrayList <>();
246- DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
247+ DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
247248
248249 identityLoader .prime ("A" , "X" );
249250
@@ -271,7 +272,7 @@ public void should_Not_prime_keys_that_already_exist() {
271272 @ Test
272273 public void should_Allow_to_forcefully_prime_the_cache () {
273274 ArrayList <Collection > loadCalls = new ArrayList <>();
274- DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
275+ DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
275276
276277 identityLoader .prime ("A" , "X" );
277278
@@ -299,7 +300,7 @@ public void should_Allow_to_forcefully_prime_the_cache() {
299300 @ Test
300301 public void should_Resolve_to_error_to_indicate_failure () {
301302 ArrayList <Collection > loadCalls = new ArrayList <>();
302- DataLoader <Integer , Integer > evenLoader = idLoaderWithErrors (new DataLoaderOptions <> (), loadCalls );
303+ DataLoader <Integer , Integer > evenLoader = idLoaderWithErrors (new DataLoaderOptions (), loadCalls );
303304
304305 Future <Integer > future1 = evenLoader .load (1 );
305306 evenLoader .dispatch ();
@@ -319,7 +320,7 @@ public void should_Resolve_to_error_to_indicate_failure() {
319320 @ Test
320321 public void should_Represent_failures_and_successes_simultaneously () {
321322 ArrayList <Collection > loadCalls = new ArrayList <>();
322- DataLoader <Integer , Integer > evenLoader = idLoaderWithErrors (new DataLoaderOptions <> (), loadCalls );
323+ DataLoader <Integer , Integer > evenLoader = idLoaderWithErrors (new DataLoaderOptions (), loadCalls );
323324
324325 Future <Integer > future1 = evenLoader .load (1 );
325326 Future <Integer > future2 = evenLoader .load (2 );
@@ -337,7 +338,7 @@ public void should_Represent_failures_and_successes_simultaneously() {
337338 @ Test
338339 public void should_Cache_failed_fetches () {
339340 ArrayList <Collection > loadCalls = new ArrayList <>();
340- DataLoader <Integer , Integer > errorLoader = idLoaderAllErrors (new DataLoaderOptions <> (), loadCalls );
341+ DataLoader <Integer , Integer > errorLoader = idLoaderAllErrors (new DataLoaderOptions (), loadCalls );
341342
342343 Future <Integer > future1 = errorLoader .load (1 );
343344 errorLoader .dispatch ();
@@ -358,7 +359,7 @@ public void should_Cache_failed_fetches() {
358359 @ Test
359360 public void should_Handle_priming_the_cache_with_an_error () {
360361 ArrayList <Collection > loadCalls = new ArrayList <>();
361- DataLoader <Integer , Integer > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
362+ DataLoader <Integer , Integer > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
362363
363364 identityLoader .prime (1 , new IllegalStateException ("Error" ));
364365
@@ -374,7 +375,7 @@ public void should_Handle_priming_the_cache_with_an_error() {
374375 @ Test
375376 public void should_Clear_values_from_cache_after_errors () {
376377 ArrayList <Collection > loadCalls = new ArrayList <>();
377- DataLoader <Integer , Integer > errorLoader = idLoaderAllErrors (new DataLoaderOptions <> (), loadCalls );
378+ DataLoader <Integer , Integer > errorLoader = idLoaderAllErrors (new DataLoaderOptions (), loadCalls );
378379
379380 Future <Integer > future1 = errorLoader .load (1 );
380381 future1 .setHandler (rh -> {
@@ -407,7 +408,7 @@ public void should_Clear_values_from_cache_after_errors() {
407408 @ Test
408409 public void should_Propagate_error_to_all_loads () {
409410 ArrayList <Collection > loadCalls = new ArrayList <>();
410- DataLoader <Integer , Integer > errorLoader = idLoaderAllErrors (new DataLoaderOptions <> (), loadCalls );
411+ DataLoader <Integer , Integer > errorLoader = idLoaderAllErrors (new DataLoaderOptions (), loadCalls );
411412
412413 Future <Integer > future1 = errorLoader .load (1 );
413414 Future <Integer > future2 = errorLoader .load (2 );
@@ -430,7 +431,7 @@ public void should_Propagate_error_to_all_loads() {
430431 @ Test
431432 public void should_Accept_objects_as_keys () {
432433 ArrayList <Collection > loadCalls = new ArrayList <>();
433- DataLoader <Object , Object > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
434+ DataLoader <Object , Object > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
434435
435436 Object keyA = new Object ();
436437 Object keyB = new Object ();
@@ -475,8 +476,8 @@ public void should_Accept_objects_as_keys() {
475476 @ Test
476477 public void should_Disable_caching () {
477478 ArrayList <Collection > loadCalls = new ArrayList <>();
478- DataLoaderOptions <String , String > options = new DataLoaderOptions <>();
479- DataLoader < String , String > identityLoader = idLoader (options .setCachingEnabled (false ), loadCalls );
479+ DataLoader <String , String > identityLoader =
480+ idLoader (DataLoaderOptions . create () .setCachingEnabled (false ), loadCalls );
480481
481482 Future <String > future1 = identityLoader .load ("A" );
482483 Future <String > future2 = identityLoader .load ("B" );
@@ -513,11 +514,32 @@ public void should_Disable_caching() {
513514
514515 @ Test
515516 public void should_Accept_objects_with_a_complex_key () {
517+ ArrayList <Collection > loadCalls = new ArrayList <>();
518+ DataLoaderOptions options = DataLoaderOptions .create ().setCacheKeyFunction (getJsonObjectCacheMapFn ());
519+ DataLoader <JsonObject , Integer > identityLoader = idLoader (options , loadCalls );
520+
521+ JsonObject key1 = new JsonObject ().put ("id" , 123 );
522+ JsonObject key2 = new JsonObject ().put ("id" , 123 );
523+
524+ Future <Integer > future1 = identityLoader .load (key1 );
525+ Future <Integer > future2 = identityLoader .load (key2 );
526+ identityLoader .dispatch ();
527+
528+ await ().until (() -> future1 .isComplete () && future2 .isComplete ());
529+ assertThat (loadCalls , equalTo (Collections .singletonList (Collections .singletonList (key1 ))));
530+ assertThat (future1 .result (), equalTo (key1 ));
531+ assertThat (future2 .result (), equalTo (key1 ));
532+ }
516533
534+ private static CacheKey <JsonObject > getJsonObjectCacheMapFn () {
535+ return key -> key .stream ()
536+ .sorted ()
537+ .map (entry -> entry .getKey () + ":" + entry .getValue ())
538+ .collect (Collectors .joining ());
517539 }
518540
519541 @ SuppressWarnings ("unchecked" )
520- private static <K , V > DataLoader <K , V > idLoader (DataLoaderOptions < K , V > options , List <Collection > loadCalls ) {
542+ private static <K , V > DataLoader <K , V > idLoader (DataLoaderOptions options , List <Collection > loadCalls ) {
521543 return new DataLoader <>(keys -> {
522544 loadCalls .add (new ArrayList (keys ));
523545 List <Future > futures = keys .stream ().map (Future ::succeededFuture ).collect (Collectors .toList ());
@@ -527,7 +549,7 @@ private static <K, V> DataLoader<K, V> idLoader(DataLoaderOptions<K, V> options,
527549
528550 @ SuppressWarnings ("unchecked" )
529551 private static <K , V > DataLoader <K , V > idLoaderAllErrors (
530- DataLoaderOptions < K , V > options , List <Collection > loadCalls ) {
552+ DataLoaderOptions options , List <Collection > loadCalls ) {
531553 return new DataLoader <>(keys -> {
532554 loadCalls .add (new ArrayList (keys ));
533555 List <Future > futures = keys .stream ()
@@ -539,7 +561,7 @@ private static <K, V> DataLoader<K, V> idLoaderAllErrors(
539561
540562 @ SuppressWarnings ("unchecked" )
541563 private static DataLoader <Integer , Integer > idLoaderWithErrors (
542- DataLoaderOptions < Integer , Integer > options , List <Collection > loadCalls ) {
564+ DataLoaderOptions options , List <Collection > loadCalls ) {
543565 return new DataLoader <>(keys -> {
544566 loadCalls .add (new ArrayList (keys ));
545567 List <Future > futures = keys .stream ()
0 commit comments