18
18
19
19
import io .vertx .core .CompositeFuture ;
20
20
import io .vertx .core .Future ;
21
+ import io .vertx .core .json .JsonObject ;
21
22
import io .vertx .ext .unit .junit .RunTestOnContext ;
22
23
import io .vertx .ext .unit .junit .VertxUnitRunner ;
23
24
import org .junit .Before ;
@@ -54,7 +55,7 @@ public class DataLoaderTest {
54
55
55
56
@ Before
56
57
public void setUp () {
57
- identityLoader = idLoader (new DataLoaderOptions <> (), new ArrayList <>());
58
+ identityLoader = idLoader (new DataLoaderOptions (), new ArrayList <>());
58
59
}
59
60
60
61
@ Test
@@ -108,7 +109,7 @@ public void should_Resolve_to_empty_list_when_no_keys_supplied() {
108
109
@ Test
109
110
public void should_Batch_multiple_requests () {
110
111
ArrayList <Collection > loadCalls = new ArrayList <>();
111
- DataLoader <Integer , Integer > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
112
+ DataLoader <Integer , Integer > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
112
113
113
114
Future <Integer > future1 = identityLoader .load (1 );
114
115
Future <Integer > future2 = identityLoader .load (2 );
@@ -123,7 +124,7 @@ public void should_Batch_multiple_requests() {
123
124
@ Test
124
125
public void should_Coalesce_identical_requests () {
125
126
ArrayList <Collection > loadCalls = new ArrayList <>();
126
- DataLoader <Integer , Integer > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
127
+ DataLoader <Integer , Integer > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
127
128
128
129
Future <Integer > future1a = identityLoader .load (1 );
129
130
Future <Integer > future1b = identityLoader .load (1 );
@@ -139,7 +140,7 @@ public void should_Coalesce_identical_requests() {
139
140
@ Test
140
141
public void should_Cache_repeated_requests () {
141
142
ArrayList <Collection > loadCalls = new ArrayList <>();
142
- DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
143
+ DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
143
144
144
145
Future <String > future1 = identityLoader .load ("A" );
145
146
Future <String > future2 = identityLoader .load ("B" );
@@ -174,7 +175,7 @@ public void should_Cache_repeated_requests() {
174
175
@ Test
175
176
public void should_Clear_single_value_in_loader () {
176
177
ArrayList <Collection > loadCalls = new ArrayList <>();
177
- DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
178
+ DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
178
179
179
180
Future <String > future1 = identityLoader .load ("A" );
180
181
Future <String > future2 = identityLoader .load ("B" );
@@ -200,7 +201,7 @@ public void should_Clear_single_value_in_loader() {
200
201
@ Test
201
202
public void should_Clear_all_values_in_loader () {
202
203
ArrayList <Collection > loadCalls = new ArrayList <>();
203
- DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
204
+ DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
204
205
205
206
Future <String > future1 = identityLoader .load ("A" );
206
207
Future <String > future2 = identityLoader .load ("B" );
@@ -226,7 +227,7 @@ public void should_Clear_all_values_in_loader() {
226
227
@ Test
227
228
public void should_Allow_priming_the_cache () {
228
229
ArrayList <Collection > loadCalls = new ArrayList <>();
229
- DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
230
+ DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
230
231
231
232
identityLoader .prime ("A" , "A" );
232
233
@@ -243,7 +244,7 @@ public void should_Allow_priming_the_cache() {
243
244
@ Test
244
245
public void should_Not_prime_keys_that_already_exist () {
245
246
ArrayList <Collection > loadCalls = new ArrayList <>();
246
- DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
247
+ DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
247
248
248
249
identityLoader .prime ("A" , "X" );
249
250
@@ -271,7 +272,7 @@ public void should_Not_prime_keys_that_already_exist() {
271
272
@ Test
272
273
public void should_Allow_to_forcefully_prime_the_cache () {
273
274
ArrayList <Collection > loadCalls = new ArrayList <>();
274
- DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
275
+ DataLoader <String , String > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
275
276
276
277
identityLoader .prime ("A" , "X" );
277
278
@@ -299,7 +300,7 @@ public void should_Allow_to_forcefully_prime_the_cache() {
299
300
@ Test
300
301
public void should_Resolve_to_error_to_indicate_failure () {
301
302
ArrayList <Collection > loadCalls = new ArrayList <>();
302
- DataLoader <Integer , Integer > evenLoader = idLoaderWithErrors (new DataLoaderOptions <> (), loadCalls );
303
+ DataLoader <Integer , Integer > evenLoader = idLoaderWithErrors (new DataLoaderOptions (), loadCalls );
303
304
304
305
Future <Integer > future1 = evenLoader .load (1 );
305
306
evenLoader .dispatch ();
@@ -319,7 +320,7 @@ public void should_Resolve_to_error_to_indicate_failure() {
319
320
@ Test
320
321
public void should_Represent_failures_and_successes_simultaneously () {
321
322
ArrayList <Collection > loadCalls = new ArrayList <>();
322
- DataLoader <Integer , Integer > evenLoader = idLoaderWithErrors (new DataLoaderOptions <> (), loadCalls );
323
+ DataLoader <Integer , Integer > evenLoader = idLoaderWithErrors (new DataLoaderOptions (), loadCalls );
323
324
324
325
Future <Integer > future1 = evenLoader .load (1 );
325
326
Future <Integer > future2 = evenLoader .load (2 );
@@ -337,7 +338,7 @@ public void should_Represent_failures_and_successes_simultaneously() {
337
338
@ Test
338
339
public void should_Cache_failed_fetches () {
339
340
ArrayList <Collection > loadCalls = new ArrayList <>();
340
- DataLoader <Integer , Integer > errorLoader = idLoaderAllErrors (new DataLoaderOptions <> (), loadCalls );
341
+ DataLoader <Integer , Integer > errorLoader = idLoaderAllErrors (new DataLoaderOptions (), loadCalls );
341
342
342
343
Future <Integer > future1 = errorLoader .load (1 );
343
344
errorLoader .dispatch ();
@@ -358,7 +359,7 @@ public void should_Cache_failed_fetches() {
358
359
@ Test
359
360
public void should_Handle_priming_the_cache_with_an_error () {
360
361
ArrayList <Collection > loadCalls = new ArrayList <>();
361
- DataLoader <Integer , Integer > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
362
+ DataLoader <Integer , Integer > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
362
363
363
364
identityLoader .prime (1 , new IllegalStateException ("Error" ));
364
365
@@ -374,7 +375,7 @@ public void should_Handle_priming_the_cache_with_an_error() {
374
375
@ Test
375
376
public void should_Clear_values_from_cache_after_errors () {
376
377
ArrayList <Collection > loadCalls = new ArrayList <>();
377
- DataLoader <Integer , Integer > errorLoader = idLoaderAllErrors (new DataLoaderOptions <> (), loadCalls );
378
+ DataLoader <Integer , Integer > errorLoader = idLoaderAllErrors (new DataLoaderOptions (), loadCalls );
378
379
379
380
Future <Integer > future1 = errorLoader .load (1 );
380
381
future1 .setHandler (rh -> {
@@ -407,7 +408,7 @@ public void should_Clear_values_from_cache_after_errors() {
407
408
@ Test
408
409
public void should_Propagate_error_to_all_loads () {
409
410
ArrayList <Collection > loadCalls = new ArrayList <>();
410
- DataLoader <Integer , Integer > errorLoader = idLoaderAllErrors (new DataLoaderOptions <> (), loadCalls );
411
+ DataLoader <Integer , Integer > errorLoader = idLoaderAllErrors (new DataLoaderOptions (), loadCalls );
411
412
412
413
Future <Integer > future1 = errorLoader .load (1 );
413
414
Future <Integer > future2 = errorLoader .load (2 );
@@ -430,7 +431,7 @@ public void should_Propagate_error_to_all_loads() {
430
431
@ Test
431
432
public void should_Accept_objects_as_keys () {
432
433
ArrayList <Collection > loadCalls = new ArrayList <>();
433
- DataLoader <Object , Object > identityLoader = idLoader (new DataLoaderOptions <> (), loadCalls );
434
+ DataLoader <Object , Object > identityLoader = idLoader (new DataLoaderOptions (), loadCalls );
434
435
435
436
Object keyA = new Object ();
436
437
Object keyB = new Object ();
@@ -475,8 +476,8 @@ public void should_Accept_objects_as_keys() {
475
476
@ Test
476
477
public void should_Disable_caching () {
477
478
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 );
480
481
481
482
Future <String > future1 = identityLoader .load ("A" );
482
483
Future <String > future2 = identityLoader .load ("B" );
@@ -513,11 +514,32 @@ public void should_Disable_caching() {
513
514
514
515
@ Test
515
516
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
+ }
516
533
534
+ private static CacheKey <JsonObject > getJsonObjectCacheMapFn () {
535
+ return key -> key .stream ()
536
+ .sorted ()
537
+ .map (entry -> entry .getKey () + ":" + entry .getValue ())
538
+ .collect (Collectors .joining ());
517
539
}
518
540
519
541
@ 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 ) {
521
543
return new DataLoader <>(keys -> {
522
544
loadCalls .add (new ArrayList (keys ));
523
545
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,
527
549
528
550
@ SuppressWarnings ("unchecked" )
529
551
private static <K , V > DataLoader <K , V > idLoaderAllErrors (
530
- DataLoaderOptions < K , V > options , List <Collection > loadCalls ) {
552
+ DataLoaderOptions options , List <Collection > loadCalls ) {
531
553
return new DataLoader <>(keys -> {
532
554
loadCalls .add (new ArrayList (keys ));
533
555
List <Future > futures = keys .stream ()
@@ -539,7 +561,7 @@ private static <K, V> DataLoader<K, V> idLoaderAllErrors(
539
561
540
562
@ SuppressWarnings ("unchecked" )
541
563
private static DataLoader <Integer , Integer > idLoaderWithErrors (
542
- DataLoaderOptions < Integer , Integer > options , List <Collection > loadCalls ) {
564
+ DataLoaderOptions options , List <Collection > loadCalls ) {
543
565
return new DataLoader <>(keys -> {
544
566
loadCalls .add (new ArrayList (keys ));
545
567
List <Future > futures = keys .stream ()
0 commit comments