Skip to content

Commit 2f437a7

Browse files
author
Harrison Cole
committed
DataLoader and CompletableFutureKit: add test cases for loadMany given a Map of keys and context objects.
1 parent 22413be commit 2f437a7

File tree

3 files changed

+124
-37
lines changed

3 files changed

+124
-37
lines changed

src/test/java/org/dataloader/DataLoaderBatchLoaderEnvironmentTest.java

+43-13
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import org.junit.jupiter.api.Test;
44

5-
import java.util.ArrayList;
6-
import java.util.HashMap;
7-
import java.util.List;
8-
import java.util.Map;
5+
import java.util.*;
96
import java.util.concurrent.CompletableFuture;
107
import java.util.concurrent.atomic.AtomicInteger;
118
import java.util.stream.Collectors;
@@ -50,10 +47,14 @@ public void context_is_passed_to_batch_loader_function() {
5047
loader.load("A");
5148
loader.load("B");
5249
loader.loadMany(asList("C", "D"));
50+
Map<String, ?> keysAndContexts = new LinkedHashMap<>();
51+
keysAndContexts.put("E", null);
52+
keysAndContexts.put("F", null);
53+
loader.loadMany(keysAndContexts);
5354

5455
List<String> results = loader.dispatchAndJoin();
5556

56-
assertThat(results, equalTo(asList("A-ctx", "B-ctx", "C-ctx", "D-ctx")));
57+
assertThat(results, equalTo(asList("A-ctx", "B-ctx", "C-ctx", "D-ctx", "E-ctx", "F-ctx")));
5758
}
5859

5960
@Test
@@ -66,10 +67,14 @@ public void key_contexts_are_passed_to_batch_loader_function() {
6667
loader.load("A", "aCtx");
6768
loader.load("B", "bCtx");
6869
loader.loadMany(asList("C", "D"), asList("cCtx", "dCtx"));
70+
Map<String, String> keysAndContexts = new LinkedHashMap<>();
71+
keysAndContexts.put("E", "eCtx");
72+
keysAndContexts.put("F", "fCtx");
73+
loader.loadMany(keysAndContexts);
6974

7075
List<String> results = loader.dispatchAndJoin();
7176

72-
assertThat(results, equalTo(asList("A-ctx-m:aCtx-l:aCtx", "B-ctx-m:bCtx-l:bCtx", "C-ctx-m:cCtx-l:cCtx", "D-ctx-m:dCtx-l:dCtx")));
77+
assertThat(results, equalTo(asList("A-ctx-m:aCtx-l:aCtx", "B-ctx-m:bCtx-l:bCtx", "C-ctx-m:cCtx-l:cCtx", "D-ctx-m:dCtx-l:dCtx", "E-ctx-m:eCtx-l:eCtx", "F-ctx-m:fCtx-l:fCtx")));
7378
}
7479

7580
@Test
@@ -82,12 +87,17 @@ public void key_contexts_are_passed_to_batch_loader_function_when_batching_disab
8287

8388
CompletableFuture<String> aLoad = loader.load("A", "aCtx");
8489
CompletableFuture<String> bLoad = loader.load("B", "bCtx");
85-
CompletableFuture<List<String>> canDLoad = loader.loadMany(asList("C", "D"), asList("cCtx", "dCtx"));
90+
CompletableFuture<List<String>> cAndDLoad = loader.loadMany(asList("C", "D"), asList("cCtx", "dCtx"));
91+
Map<String, String> keysAndContexts = new LinkedHashMap<>();
92+
keysAndContexts.put("E", "eCtx");
93+
keysAndContexts.put("F", "fCtx");
94+
CompletableFuture<Map<String, String>> eAndFLoad = loader.loadMany(keysAndContexts);
8695

8796
List<String> results = new ArrayList<>(asList(aLoad.join(), bLoad.join()));
88-
results.addAll(canDLoad.join());
97+
results.addAll(cAndDLoad.join());
98+
results.addAll(eAndFLoad.join().values());
8999

90-
assertThat(results, equalTo(asList("A-ctx-m:aCtx-l:aCtx", "B-ctx-m:bCtx-l:bCtx", "C-ctx-m:cCtx-l:cCtx", "D-ctx-m:dCtx-l:dCtx")));
100+
assertThat(results, equalTo(asList("A-ctx-m:aCtx-l:aCtx", "B-ctx-m:bCtx-l:bCtx", "C-ctx-m:cCtx-l:cCtx", "D-ctx-m:dCtx-l:dCtx", "E-ctx-m:eCtx-l:eCtx", "F-ctx-m:fCtx-l:fCtx")));
91101
}
92102

93103
@Test
@@ -101,9 +111,14 @@ public void missing_key_contexts_are_passed_to_batch_loader_function() {
101111
loader.load("B");
102112
loader.loadMany(asList("C", "D"), singletonList("cCtx"));
103113

114+
Map<String, String> keysAndContexts = new LinkedHashMap<>();
115+
keysAndContexts.put("E", "eCtx");
116+
keysAndContexts.put("F", null);
117+
loader.loadMany(keysAndContexts);
118+
104119
List<String> results = loader.dispatchAndJoin();
105120

106-
assertThat(results, equalTo(asList("A-ctx-m:aCtx-l:aCtx", "B-ctx-m:null-l:null", "C-ctx-m:cCtx-l:cCtx", "D-ctx-m:null-l:null")));
121+
assertThat(results, equalTo(asList("A-ctx-m:aCtx-l:aCtx", "B-ctx-m:null-l:null", "C-ctx-m:cCtx-l:cCtx", "D-ctx-m:null-l:null", "E-ctx-m:eCtx-l:eCtx", "F-ctx-m:null-l:null")));
107122
}
108123

109124
@Test
@@ -125,9 +140,14 @@ public void context_is_passed_to_map_batch_loader_function() {
125140
loader.load("B");
126141
loader.loadMany(asList("C", "D"), singletonList("cCtx"));
127142

143+
Map<String, String> keysAndContexts = new LinkedHashMap<>();
144+
keysAndContexts.put("E", "eCtx");
145+
keysAndContexts.put("F", null);
146+
loader.loadMany(keysAndContexts);
147+
128148
List<String> results = loader.dispatchAndJoin();
129149

130-
assertThat(results, equalTo(asList("A-ctx-aCtx", "B-ctx-null", "C-ctx-cCtx", "D-ctx-null")));
150+
assertThat(results, equalTo(asList("A-ctx-aCtx", "B-ctx-null", "C-ctx-cCtx", "D-ctx-null", "E-ctx-eCtx", "F-ctx-null")));
131151
}
132152

133153
@Test
@@ -142,9 +162,14 @@ public void null_is_passed_as_context_if_you_do_nothing() {
142162
loader.load("B");
143163
loader.loadMany(asList("C", "D"));
144164

165+
Map<String, String> keysAndContexts = new LinkedHashMap<>();
166+
keysAndContexts.put("E", null);
167+
keysAndContexts.put("F", null);
168+
loader.loadMany(keysAndContexts);
169+
145170
List<String> results = loader.dispatchAndJoin();
146171

147-
assertThat(results, equalTo(asList("A-null", "B-null", "C-null", "D-null")));
172+
assertThat(results, equalTo(asList("A-null", "B-null", "C-null", "D-null", "E-null", "F-null")));
148173
}
149174

150175
@Test
@@ -160,9 +185,14 @@ public void null_is_passed_as_context_to_map_loader_if_you_do_nothing() {
160185
loader.load("B");
161186
loader.loadMany(asList("C", "D"));
162187

188+
Map<String, String> keysAndContexts = new LinkedHashMap<>();
189+
keysAndContexts.put("E", null);
190+
keysAndContexts.put("F", null);
191+
loader.loadMany(keysAndContexts);
192+
163193
List<String> results = loader.dispatchAndJoin();
164194

165-
assertThat(results, equalTo(asList("A-null", "B-null", "C-null", "D-null")));
195+
assertThat(results, equalTo(asList("A-null", "B-null", "C-null", "D-null", "E-null", "F-null")));
166196
}
167197

168198
@Test

src/test/java/org/dataloader/DataLoaderStatsTest.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.util.ArrayList;
1515
import java.util.List;
16+
import java.util.Map;
1617
import java.util.concurrent.CompletableFuture;
1718

1819
import static java.util.Arrays.asList;
@@ -118,19 +119,20 @@ public void stats_are_collected_with_caching_disabled() {
118119
loader.load("A");
119120
loader.load("B");
120121
loader.loadMany(asList("C", "D"));
122+
loader.loadMany(Map.of("E", "E", "F", "F"));
121123

122124
Statistics stats = loader.getStatistics();
123-
assertThat(stats.getLoadCount(), equalTo(4L));
125+
assertThat(stats.getLoadCount(), equalTo(6L));
124126
assertThat(stats.getBatchInvokeCount(), equalTo(0L));
125127
assertThat(stats.getBatchLoadCount(), equalTo(0L));
126128
assertThat(stats.getCacheHitCount(), equalTo(0L));
127129

128130
loader.dispatch();
129131

130132
stats = loader.getStatistics();
131-
assertThat(stats.getLoadCount(), equalTo(4L));
133+
assertThat(stats.getLoadCount(), equalTo(6L));
132134
assertThat(stats.getBatchInvokeCount(), equalTo(1L));
133-
assertThat(stats.getBatchLoadCount(), equalTo(4L));
135+
assertThat(stats.getBatchLoadCount(), equalTo(6L));
134136
assertThat(stats.getCacheHitCount(), equalTo(0L));
135137

136138
loader.load("A");
@@ -139,9 +141,9 @@ public void stats_are_collected_with_caching_disabled() {
139141
loader.dispatch();
140142

141143
stats = loader.getStatistics();
142-
assertThat(stats.getLoadCount(), equalTo(6L));
144+
assertThat(stats.getLoadCount(), equalTo(8L));
143145
assertThat(stats.getBatchInvokeCount(), equalTo(2L));
144-
assertThat(stats.getBatchLoadCount(), equalTo(6L));
146+
assertThat(stats.getBatchLoadCount(), equalTo(8L));
145147
assertThat(stats.getCacheHitCount(), equalTo(0L));
146148
}
147149

src/test/java/org/dataloader/DataLoaderTest.java

+74-19
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,19 @@
3535
import org.junit.jupiter.params.provider.Arguments;
3636
import org.junit.jupiter.params.provider.MethodSource;
3737

38-
import java.util.ArrayList;
39-
import java.util.Collection;
40-
import java.util.HashMap;
41-
import java.util.List;
42-
import java.util.Map;
43-
import java.util.Optional;
38+
import java.util.*;
4439
import java.util.concurrent.CompletableFuture;
4540
import java.util.concurrent.CompletionStage;
4641
import java.util.concurrent.ExecutionException;
4742
import java.util.concurrent.atomic.AtomicBoolean;
4843
import java.util.concurrent.atomic.AtomicInteger;
44+
import java.util.function.Function;
4945
import java.util.function.Supplier;
5046
import java.util.stream.Collectors;
5147
import java.util.stream.Stream;
5248

5349
import static java.util.Arrays.asList;
54-
import static java.util.Collections.emptyList;
55-
import static java.util.Collections.singletonList;
50+
import static java.util.Collections.*;
5651
import static java.util.concurrent.CompletableFuture.*;
5752
import static org.awaitility.Awaitility.await;
5853
import static org.dataloader.DataLoaderFactory.newDataLoader;
@@ -66,10 +61,7 @@
6661
import static org.dataloader.fixtures.TestKit.listFrom;
6762
import static org.dataloader.impl.CompletableFutureKit.cause;
6863
import static org.hamcrest.MatcherAssert.assertThat;
69-
import static org.hamcrest.Matchers.empty;
70-
import static org.hamcrest.Matchers.equalTo;
71-
import static org.hamcrest.Matchers.instanceOf;
72-
import static org.hamcrest.Matchers.is;
64+
import static org.hamcrest.Matchers.*;
7365
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
7466
import static org.junit.jupiter.api.Assertions.fail;
7567

@@ -116,19 +108,25 @@ public void basic_map_batch_loading() {
116108
};
117109
DataLoader<String, String> loader = DataLoaderFactory.newMappedDataLoader(evensOnlyMappedBatchLoader);
118110

111+
final List<String> keys = asList("C", "D");
112+
final Map<String, ?> keysAndContexts = new LinkedHashMap<>();
113+
keysAndContexts.put("E", null);
114+
keysAndContexts.put("F", null);
115+
119116
loader.load("A");
120117
loader.load("B");
121-
loader.loadMany(asList("C", "D"));
118+
loader.loadMany(keys);
119+
loader.loadMany(keysAndContexts);
122120

123121
List<String> results = loader.dispatchAndJoin();
124122

125-
assertThat(results.size(), equalTo(4));
126-
assertThat(results, equalTo(asList("A", null, "C", null)));
123+
assertThat(results.size(), equalTo(6));
124+
assertThat(results, equalTo(asList("A", null, "C", null, "E", null)));
127125
}
128126

129127
@ParameterizedTest
130128
@MethodSource("dataLoaderFactories")
131-
public void should_Support_loading_multiple_keys_in_one_call(TestDataLoaderFactory factory) {
129+
public void should_Support_loading_multiple_keys_in_one_call_via_list(TestDataLoaderFactory factory) {
132130
AtomicBoolean success = new AtomicBoolean();
133131
DataLoader<Integer, Integer> identityLoader = factory.idLoader(new DataLoaderOptions(), new ArrayList<>());
134132

@@ -142,6 +140,26 @@ public void should_Support_loading_multiple_keys_in_one_call(TestDataLoaderFacto
142140
assertThat(futureAll.toCompletableFuture().join(), equalTo(asList(1, 2)));
143141
}
144142

143+
@ParameterizedTest
144+
@MethodSource("dataLoaderFactories")
145+
public void should_Support_loading_multiple_keys_in_one_call_via_map(TestDataLoaderFactory factory) {
146+
AtomicBoolean success = new AtomicBoolean();
147+
DataLoader<Integer, Integer> identityLoader = factory.idLoader(new DataLoaderOptions(), new ArrayList<>());
148+
149+
final Map<Integer, ?> keysAndContexts = new LinkedHashMap<>();
150+
keysAndContexts.put(1, null);
151+
keysAndContexts.put(2, null);
152+
153+
CompletionStage<Map<Integer, Integer>> futureAll = identityLoader.loadMany(keysAndContexts);
154+
futureAll.thenAccept(promisedValues -> {
155+
assertThat(promisedValues.size(), is(2));
156+
success.set(true);
157+
});
158+
identityLoader.dispatch();
159+
await().untilAtomic(success, is(true));
160+
assertThat(futureAll.toCompletableFuture().join(), equalTo(Map.of(1, 1, 2, 2)));
161+
}
162+
145163
@ParameterizedTest
146164
@MethodSource("dataLoaderFactories")
147165
public void should_Resolve_to_empty_list_when_no_keys_supplied(TestDataLoaderFactory factory) {
@@ -159,7 +177,22 @@ public void should_Resolve_to_empty_list_when_no_keys_supplied(TestDataLoaderFac
159177

160178
@ParameterizedTest
161179
@MethodSource("dataLoaderFactories")
162-
public void should_Return_zero_entries_dispatched_when_no_keys_supplied(TestDataLoaderFactory factory) {
180+
public void should_Resolve_to_empty_map_when_no_keys_supplied(TestDataLoaderFactory factory) {
181+
AtomicBoolean success = new AtomicBoolean();
182+
DataLoader<Integer, Integer> identityLoader = factory.idLoader(new DataLoaderOptions(), new ArrayList<>());
183+
CompletableFuture<Map<Integer, Integer>> futureEmpty = identityLoader.loadMany(emptyMap());
184+
futureEmpty.thenAccept(promisedValues -> {
185+
assertThat(promisedValues.size(), is(0));
186+
success.set(true);
187+
});
188+
identityLoader.dispatch();
189+
await().untilAtomic(success, is(true));
190+
assertThat(futureEmpty.join(), anEmptyMap());
191+
}
192+
193+
@ParameterizedTest
194+
@MethodSource("dataLoaderFactories")
195+
public void should_Return_zero_entries_dispatched_when_no_keys_supplied_via_list(TestDataLoaderFactory factory) {
163196
AtomicBoolean success = new AtomicBoolean();
164197
DataLoader<Integer, Integer> identityLoader = factory.idLoader(new DataLoaderOptions(), new ArrayList<>());
165198
CompletableFuture<List<Integer>> futureEmpty = identityLoader.loadMany(emptyList());
@@ -172,6 +205,21 @@ public void should_Return_zero_entries_dispatched_when_no_keys_supplied(TestData
172205
assertThat(dispatchResult.getKeysCount(), equalTo(0));
173206
}
174207

208+
@ParameterizedTest
209+
@MethodSource("dataLoaderFactories")
210+
public void should_Return_zero_entries_dispatched_when_no_keys_supplied_via_map(TestDataLoaderFactory factory) {
211+
AtomicBoolean success = new AtomicBoolean();
212+
DataLoader<Integer, Integer> identityLoader = factory.idLoader(new DataLoaderOptions(), new ArrayList<>());
213+
CompletableFuture<Map<Integer, Integer>> futureEmpty = identityLoader.loadMany(emptyMap());
214+
futureEmpty.thenAccept(promisedValues -> {
215+
assertThat(promisedValues.size(), is(0));
216+
success.set(true);
217+
});
218+
DispatchResult<Integer> dispatchResult = identityLoader.dispatchWithCounts();
219+
await().untilAtomic(success, is(true));
220+
assertThat(dispatchResult.getKeysCount(), equalTo(0));
221+
}
222+
175223
@ParameterizedTest
176224
@MethodSource("dataLoaderFactories")
177225
public void should_Batch_multiple_requests(TestDataLoaderFactory factory) throws ExecutionException, InterruptedException {
@@ -286,10 +334,17 @@ public void should_Cache_on_redispatch(TestDataLoaderFactory factory) throws Exe
286334
CompletableFuture<List<String>> future2 = identityLoader.loadMany(asList("A", "B"));
287335
identityLoader.dispatch();
288336

289-
await().until(() -> future1.isDone() && future2.isDone());
337+
Map<String, ?> keysAndContexts = new LinkedHashMap<>();
338+
keysAndContexts.put("A", null);
339+
keysAndContexts.put("C", null);
340+
CompletableFuture<Map<String, String>> future3 = identityLoader.loadMany(keysAndContexts);
341+
identityLoader.dispatch();
342+
343+
await().until(() -> future1.isDone() && future2.isDone() && future3.isDone());
290344
assertThat(future1.get(), equalTo("A"));
291345
assertThat(future2.get(), equalTo(asList("A", "B")));
292-
assertThat(loadCalls, equalTo(asList(singletonList("A"), singletonList("B"))));
346+
assertThat(future3.get(), equalTo(keysAndContexts.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getKey))));
347+
assertThat(loadCalls, equalTo(asList(singletonList("A"), singletonList("B"), singletonList("C"))));
293348
}
294349

295350
@ParameterizedTest

0 commit comments

Comments
 (0)