Skip to content

Commit a373ebe

Browse files
committed
Added tests that ensure that only new future requests are dispatched
Issue graphql-java#6
1 parent 666716b commit a373ebe

File tree

1 file changed

+62
-27
lines changed

1 file changed

+62
-27
lines changed

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

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.concurrent.atomic.AtomicBoolean;
3333
import java.util.stream.Collectors;
3434

35+
import static java.util.Arrays.asList;
3536
import static org.awaitility.Awaitility.await;
3637
import static org.hamcrest.Matchers.*;
3738
import static org.junit.Assert.assertArrayEquals;
@@ -85,14 +86,14 @@ public void should_Support_loading_multiple_keys_in_one_call() {
8586
.map(Future::succeededFuture)
8687
.collect(Collectors.toCollection(ArrayList::new))));
8788

88-
CompositeFuture futureAll = identityLoader.loadMany(Arrays.asList(1, 2));
89+
CompositeFuture futureAll = identityLoader.loadMany(asList(1, 2));
8990
futureAll.setHandler(rh -> {
9091
assertThat(rh.result().size(), is(2));
9192
success.set(rh.succeeded());
9293
});
9394
identityLoader.dispatch();
9495
await().untilAtomic(success, is(true));
95-
assertThat(futureAll.list(), equalTo(Arrays.asList(1, 2)));
96+
assertThat(futureAll.list(), equalTo(asList(1, 2)));
9697
}
9798

9899
@Test
@@ -120,7 +121,7 @@ public void should_Batch_multiple_requests() {
120121
await().until(() -> future1.isComplete() && future2.isComplete());
121122
assertThat(future1.result(), equalTo(1));
122123
assertThat(future2.result(), equalTo(2));
123-
assertThat(loadCalls, equalTo(Collections.singletonList(Arrays.asList(1, 2))));
124+
assertThat(loadCalls, equalTo(Collections.singletonList(asList(1, 2))));
124125
}
125126

126127
@Test
@@ -151,7 +152,7 @@ public void should_Cache_repeated_requests() {
151152
await().until(() -> future1.isComplete() && future2.isComplete());
152153
assertThat(future1.result(), equalTo("A"));
153154
assertThat(future2.result(), equalTo("B"));
154-
assertThat(loadCalls, equalTo(Collections.singletonList(Arrays.asList("A", "B"))));
155+
assertThat(loadCalls, equalTo(Collections.singletonList(asList("A", "B"))));
155156

156157
Future<String> future1a = identityLoader.load("A");
157158
Future<String> future3 = identityLoader.load("C");
@@ -160,7 +161,7 @@ public void should_Cache_repeated_requests() {
160161
await().until(() -> future1a.isComplete() && future3.isComplete());
161162
assertThat(future1a.result(), equalTo("A"));
162163
assertThat(future3.result(), equalTo("C"));
163-
assertThat(loadCalls, equalTo(Arrays.asList(Arrays.asList("A", "B"), Collections.singletonList("C"))));
164+
assertThat(loadCalls, equalTo(asList(asList("A", "B"), Collections.singletonList("C"))));
164165

165166
Future<String> future1b = identityLoader.load("A");
166167
Future<String> future2a = identityLoader.load("B");
@@ -171,7 +172,41 @@ public void should_Cache_repeated_requests() {
171172
assertThat(future1b.result(), equalTo("A"));
172173
assertThat(future2a.result(), equalTo("B"));
173174
assertThat(future3a.result(), equalTo("C"));
174-
assertThat(loadCalls, equalTo(Arrays.asList(Arrays.asList("A", "B"), Collections.singletonList("C"))));
175+
assertThat(loadCalls, equalTo(asList(asList("A", "B"), Collections.singletonList("C"))));
176+
}
177+
178+
@Test
179+
public void should_Not_redispatch_previous_load() {
180+
ArrayList<Collection> loadCalls = new ArrayList<>();
181+
DataLoader<String, String> identityLoader = idLoader(new DataLoaderOptions(), loadCalls);
182+
183+
Future<String> future1 = identityLoader.load("A");
184+
identityLoader.dispatch();
185+
186+
Future<String> future2 = identityLoader.load("B");
187+
identityLoader.dispatch();
188+
189+
await().until(() -> future1.isComplete() && future2.isComplete());
190+
assertThat(future1.result(), equalTo("A"));
191+
assertThat(future2.result(), equalTo("B"));
192+
assertThat(loadCalls, equalTo(asList(asList("A"), asList("B"))));
193+
}
194+
195+
@Test
196+
public void should_Cache_on_redispatch() {
197+
ArrayList<Collection> loadCalls = new ArrayList<>();
198+
DataLoader<String, String> identityLoader = idLoader(new DataLoaderOptions(), loadCalls);
199+
200+
Future<String> future1 = identityLoader.load("A");
201+
identityLoader.dispatch();
202+
203+
CompositeFuture future2 = identityLoader.loadMany(asList("A", "B"));
204+
identityLoader.dispatch();
205+
206+
await().until(() -> future1.isComplete() && future2.isComplete());
207+
assertThat(future1.result(), equalTo("A"));
208+
assertThat(future2.list(), equalTo(asList("A", "B")));
209+
assertThat(loadCalls, equalTo(asList(asList("A"), asList("B"))));
175210
}
176211

177212
@Test
@@ -186,7 +221,7 @@ public void should_Clear_single_value_in_loader() {
186221
await().until(() -> future1.isComplete() && future2.isComplete());
187222
assertThat(future1.result(), equalTo("A"));
188223
assertThat(future2.result(), equalTo("B"));
189-
assertThat(loadCalls, equalTo(Collections.singletonList(Arrays.asList("A", "B"))));
224+
assertThat(loadCalls, equalTo(Collections.singletonList(asList("A", "B"))));
190225

191226
identityLoader.clear("A");
192227

@@ -197,7 +232,7 @@ public void should_Clear_single_value_in_loader() {
197232
await().until(() -> future1a.isComplete() && future2a.isComplete());
198233
assertThat(future1a.result(), equalTo("A"));
199234
assertThat(future2a.result(), equalTo("B"));
200-
assertThat(loadCalls, equalTo(Arrays.asList(Arrays.asList("A", "B"), Collections.singletonList("A"))));
235+
assertThat(loadCalls, equalTo(asList(asList("A", "B"), Collections.singletonList("A"))));
201236
}
202237

203238
@Test
@@ -212,7 +247,7 @@ public void should_Clear_all_values_in_loader() {
212247
await().until(() -> future1.isComplete() && future2.isComplete());
213248
assertThat(future1.result(), equalTo("A"));
214249
assertThat(future2.result(), equalTo("B"));
215-
assertThat(loadCalls, equalTo(Collections.singletonList(Arrays.asList("A", "B"))));
250+
assertThat(loadCalls, equalTo(Collections.singletonList(asList("A", "B"))));
216251

217252
identityLoader.clearAll();
218253

@@ -223,7 +258,7 @@ public void should_Clear_all_values_in_loader() {
223258
await().until(() -> future1a.isComplete() && future2a.isComplete());
224259
assertThat(future1a.result(), equalTo("A"));
225260
assertThat(future2a.result(), equalTo("B"));
226-
assertThat(loadCalls, equalTo(Arrays.asList(Arrays.asList("A", "B"), Arrays.asList("A", "B"))));
261+
assertThat(loadCalls, equalTo(asList(asList("A", "B"), asList("A", "B"))));
227262
}
228263

229264
@Test
@@ -316,7 +351,7 @@ public void should_Resolve_to_error_to_indicate_failure() {
316351

317352
await().until(future2::isComplete);
318353
assertThat(future2.result(), equalTo(2));
319-
assertThat(loadCalls, equalTo(Arrays.asList(Collections.singletonList(1), Collections.singletonList(2))));
354+
assertThat(loadCalls, equalTo(asList(Collections.singletonList(1), Collections.singletonList(2))));
320355
}
321356

322357
@Test
@@ -339,7 +374,7 @@ public void should_Represent_failures_and_successes_simultaneously() {
339374
assertThat(future3.failed(), is(true));
340375
assertThat(future4.result(), equalTo(4));
341376

342-
assertThat(loadCalls, equalTo(Collections.singletonList(Arrays.asList(1, 2, 3, 4))));
377+
assertThat(loadCalls, equalTo(Collections.singletonList(asList(1, 2, 3, 4))));
343378
}
344379

345380
@Test
@@ -409,7 +444,7 @@ public void should_Clear_values_from_cache_after_errors() {
409444
await().until(future2::isComplete);
410445
assertThat(future2.failed(), is(true));
411446
assertThat(future2.cause(), instanceOf(IllegalStateException.class));
412-
assertThat(loadCalls, equalTo(Arrays.asList(Collections.singletonList(1), Collections.singletonList(1))));
447+
assertThat(loadCalls, equalTo(asList(Collections.singletonList(1), Collections.singletonList(1))));
413448
}
414449

415450
@Test
@@ -430,7 +465,7 @@ public void should_Propagate_error_to_all_loads() {
430465
await().until(future2::isComplete);
431466
cause = future2.cause();
432467
assertThat(cause.getMessage(), equalTo(cause.getMessage()));
433-
assertThat(loadCalls, equalTo(Collections.singletonList(Arrays.asList(1, 2))));
468+
assertThat(loadCalls, equalTo(Collections.singletonList(asList(1, 2))));
434469
}
435470

436471
// Accept any kind of key.
@@ -493,7 +528,7 @@ public void should_Disable_caching() {
493528
await().until(() -> future1.isComplete() && future2.isComplete());
494529
assertThat(future1.result(), equalTo("A"));
495530
assertThat(future2.result(), equalTo("B"));
496-
assertThat(loadCalls, equalTo(Collections.singletonList(Arrays.asList("A", "B"))));
531+
assertThat(loadCalls, equalTo(Collections.singletonList(asList("A", "B"))));
497532

498533
Future<String> future1a = identityLoader.load("A");
499534
Future<String> future3 = identityLoader.load("C");
@@ -502,7 +537,7 @@ public void should_Disable_caching() {
502537
await().until(() -> future1a.isComplete() && future3.isComplete());
503538
assertThat(future1a.result(), equalTo("A"));
504539
assertThat(future3.result(), equalTo("C"));
505-
assertThat(loadCalls, equalTo(Arrays.asList(Arrays.asList("A", "B"), Arrays.asList("A", "C"))));
540+
assertThat(loadCalls, equalTo(asList(asList("A", "B"), asList("A", "C"))));
506541

507542
Future<String> future1b = identityLoader.load("A");
508543
Future<String> future2a = identityLoader.load("B");
@@ -513,8 +548,8 @@ public void should_Disable_caching() {
513548
assertThat(future1b.result(), equalTo("A"));
514549
assertThat(future2a.result(), equalTo("B"));
515550
assertThat(future3a.result(), equalTo("C"));
516-
assertThat(loadCalls, equalTo(Arrays.asList(Arrays.asList("A", "B"),
517-
Arrays.asList("A", "C"), Arrays.asList("A", "B", "C"))));
551+
assertThat(loadCalls, equalTo(asList(asList("A", "B"),
552+
asList("A", "C"), asList("A", "B", "C"))));
518553
}
519554

520555
// Accepts object key in custom cacheKey function
@@ -557,7 +592,7 @@ public void should_Clear_objects_with_complex_key() {
557592
identityLoader.dispatch();
558593

559594
await().until(future2::isComplete);
560-
assertThat(loadCalls, equalTo(Arrays.asList(Collections.singletonList(key1), Collections.singletonList(key1))));
595+
assertThat(loadCalls, equalTo(asList(Collections.singletonList(key1), Collections.singletonList(key1))));
561596
assertThat(future1.result(), equalTo(key1));
562597
assertThat(future2.result(), equalTo(key1));
563598
}
@@ -622,8 +657,8 @@ public void should_Accept_a_custom_cache_map_implementation() {
622657
assertThat(future1.result(), equalTo("a"));
623658
assertThat(future2.result(), equalTo("b"));
624659

625-
assertThat(loadCalls, equalTo(Collections.singletonList(Arrays.asList("a", "b"))));
626-
assertArrayEquals(customMap.stash.keySet().toArray(), Arrays.asList("a", "b").toArray());
660+
assertThat(loadCalls, equalTo(Collections.singletonList(asList("a", "b"))));
661+
assertArrayEquals(customMap.stash.keySet().toArray(), asList("a", "b").toArray());
627662

628663
Future future3 = identityLoader.load("c");
629664
Future future2a = identityLoader.load("b");
@@ -633,22 +668,22 @@ public void should_Accept_a_custom_cache_map_implementation() {
633668
assertThat(future3.result(), equalTo("c"));
634669
assertThat(future2a.result(), equalTo("b"));
635670

636-
assertThat(loadCalls, equalTo(Arrays.asList(Arrays.asList("a", "b"), Collections.singletonList("c"))));
637-
assertArrayEquals(customMap.stash.keySet().toArray(), Arrays.asList("a", "b", "c").toArray());
671+
assertThat(loadCalls, equalTo(asList(asList("a", "b"), Collections.singletonList("c"))));
672+
assertArrayEquals(customMap.stash.keySet().toArray(), asList("a", "b", "c").toArray());
638673

639674
// Supports clear
640675

641676
identityLoader.clear("b");
642-
assertArrayEquals(customMap.stash.keySet().toArray(), Arrays.asList("a", "c").toArray());
677+
assertArrayEquals(customMap.stash.keySet().toArray(), asList("a", "c").toArray());
643678

644679
Future future2b = identityLoader.load("b");
645680
composite = identityLoader.dispatch();
646681

647682
await().until((Callable<Boolean>) composite::isComplete);
648683
assertThat(future2b.result(), equalTo("b"));
649-
assertThat(loadCalls, equalTo(Arrays.asList(Arrays.asList("a", "b"),
684+
assertThat(loadCalls, equalTo(asList(asList("a", "b"),
650685
Collections.singletonList("c"), Collections.singletonList("b"))));
651-
assertArrayEquals(customMap.stash.keySet().toArray(), Arrays.asList("a", "c", "b").toArray());
686+
assertArrayEquals(customMap.stash.keySet().toArray(), asList("a", "c", "b").toArray());
652687

653688
// Supports clear all
654689

@@ -678,7 +713,7 @@ public void should_Batch_loads_occurring_within_futures() {
678713

679714
await().until((Callable<Boolean>) composite::isComplete);
680715
assertThat(loadCalls, equalTo(
681-
Collections.singletonList(Arrays.asList("a", "b", "c", "d"))));
716+
Collections.singletonList(asList("a", "b", "c", "d"))));
682717
}
683718

684719
@Test

0 commit comments

Comments
 (0)