Skip to content

Commit b894528

Browse files
committed
Added more unit tests
1 parent a644de6 commit b894528

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

src/test/java/io/engagingspaces/vertx/dataloader/DataLoaderTest.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,118 @@ public void should_Clear_values_from_cache_after_errors() {
404404
assertThat(loadCalls, equalTo(Arrays.asList(Collections.singletonList(1), Collections.singletonList(1))));
405405
}
406406

407+
@Test
408+
public void should_Propagate_error_to_all_loads() {
409+
ArrayList<Collection> loadCalls = new ArrayList<>();
410+
DataLoader<Integer, Integer> errorLoader = idLoaderAllErrors(new DataLoaderOptions<>(), loadCalls);
411+
412+
Future<Integer> future1 = errorLoader.load(1);
413+
Future<Integer> future2 = errorLoader.load(2);
414+
errorLoader.dispatch();
415+
416+
await().until(future1::isComplete);
417+
assertThat(future1.failed(), is(true));
418+
Throwable cause = future1.cause();
419+
assertThat(cause, instanceOf(IllegalStateException.class));
420+
assertThat(cause.getMessage(), equalTo("Error"));
421+
422+
await().until(future2::isComplete);
423+
cause = future2.cause();
424+
assertThat(cause.getMessage(), equalTo(cause.getMessage()));
425+
assertThat(loadCalls, equalTo(Collections.singletonList(Arrays.asList(1, 2))));
426+
}
427+
428+
// Accept any kind of key.
429+
430+
@Test
431+
public void should_Accept_objects_as_keys() {
432+
ArrayList<Collection> loadCalls = new ArrayList<>();
433+
DataLoader<Object, Object> identityLoader = idLoader(new DataLoaderOptions<>(), loadCalls);
434+
435+
Object keyA = new Object();
436+
Object keyB = new Object();
437+
438+
// Fetches as expected
439+
440+
identityLoader.load(keyA);
441+
identityLoader.load(keyB);
442+
443+
identityLoader.dispatch().setHandler(rh -> {
444+
assertThat(rh.succeeded(), is(true));
445+
assertThat(rh.result().result(0), equalTo(keyA));
446+
assertThat(rh.result().result(1), equalTo(keyB));
447+
});
448+
449+
assertThat(loadCalls.size(), equalTo(1));
450+
assertThat(loadCalls.get(0).size(), equalTo(2));
451+
assertThat(loadCalls.get(0).toArray()[0], equalTo(keyA));
452+
assertThat(loadCalls.get(0).toArray()[1], equalTo(keyB));
453+
454+
// Caching
455+
identityLoader.clear(keyA);
456+
//noinspection SuspiciousMethodCalls
457+
loadCalls.remove(keyA);
458+
459+
identityLoader.load(keyA);
460+
identityLoader.load(keyB);
461+
462+
identityLoader.dispatch().setHandler(rh -> {
463+
assertThat(rh.succeeded(), is(true));
464+
assertThat(rh.result().result(0), equalTo(keyA));
465+
assertThat(identityLoader.getCacheKey(keyB), equalTo(keyB));
466+
});
467+
468+
assertThat(loadCalls.size(), equalTo(2));
469+
assertThat(loadCalls.get(1).size(), equalTo(1));
470+
assertThat(loadCalls.get(1).toArray()[0], equalTo(keyA));
471+
}
472+
473+
// Accepts options
474+
475+
@Test
476+
public void should_Disable_caching() {
477+
ArrayList<Collection> loadCalls = new ArrayList<>();
478+
DataLoaderOptions<String, String> options = new DataLoaderOptions<>();
479+
DataLoader<String, String> identityLoader = idLoader(options.setCachingEnabled(false), loadCalls);
480+
481+
Future<String> future1 = identityLoader.load("A");
482+
Future<String> future2 = identityLoader.load("B");
483+
identityLoader.dispatch();
484+
485+
await().until(() -> future1.isComplete() && future2.isComplete());
486+
assertThat(future1.result(), equalTo("A"));
487+
assertThat(future2.result(), equalTo("B"));
488+
assertThat(loadCalls, equalTo(Collections.singletonList(Arrays.asList("A", "B"))));
489+
490+
Future<String> future1a = identityLoader.load("A");
491+
Future<String> future3 = identityLoader.load("C");
492+
identityLoader.dispatch();
493+
494+
await().until(() -> future1a.isComplete() && future3.isComplete());
495+
assertThat(future1a.result(), equalTo("A"));
496+
assertThat(future3.result(), equalTo("C"));
497+
assertThat(loadCalls, equalTo(Arrays.asList(Arrays.asList("A", "B"), Arrays.asList("A", "C"))));
498+
499+
Future<String> future1b = identityLoader.load("A");
500+
Future<String> future2a = identityLoader.load("B");
501+
Future<String> future3a = identityLoader.load("C");
502+
identityLoader.dispatch();
503+
504+
await().until(() -> future1b.isComplete() && future2a.isComplete() && future3a.isComplete());
505+
assertThat(future1b.result(), equalTo("A"));
506+
assertThat(future2a.result(), equalTo("B"));
507+
assertThat(future3a.result(), equalTo("C"));
508+
assertThat(loadCalls, equalTo(Arrays.asList(Arrays.asList("A", "B"),
509+
Arrays.asList("A", "C"), Arrays.asList("A", "B", "C"))));
510+
}
511+
512+
// Accepts object key in custom cacheKey function
513+
514+
@Test
515+
public void should_Accept_objects_with_a_complex_key() {
516+
517+
}
518+
407519
@SuppressWarnings("unchecked")
408520
private static <K, V> DataLoader<K, V> idLoader(DataLoaderOptions<K, V> options, List<Collection> loadCalls) {
409521
return new DataLoader<>(keys -> {

0 commit comments

Comments
 (0)