35
35
import org .junit .jupiter .params .provider .Arguments ;
36
36
import org .junit .jupiter .params .provider .MethodSource ;
37
37
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 .*;
44
39
import java .util .concurrent .CompletableFuture ;
45
40
import java .util .concurrent .CompletionStage ;
46
41
import java .util .concurrent .ExecutionException ;
47
42
import java .util .concurrent .atomic .AtomicBoolean ;
48
43
import java .util .concurrent .atomic .AtomicInteger ;
44
+ import java .util .function .Function ;
49
45
import java .util .function .Supplier ;
50
46
import java .util .stream .Collectors ;
51
47
import java .util .stream .Stream ;
52
48
53
49
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 .*;
56
51
import static java .util .concurrent .CompletableFuture .*;
57
52
import static org .awaitility .Awaitility .await ;
58
53
import static org .dataloader .DataLoaderFactory .newDataLoader ;
66
61
import static org .dataloader .fixtures .TestKit .listFrom ;
67
62
import static org .dataloader .impl .CompletableFutureKit .cause ;
68
63
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 .*;
73
65
import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
74
66
import static org .junit .jupiter .api .Assertions .fail ;
75
67
@@ -116,19 +108,25 @@ public void basic_map_batch_loading() {
116
108
};
117
109
DataLoader <String , String > loader = DataLoaderFactory .newMappedDataLoader (evensOnlyMappedBatchLoader );
118
110
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
+
119
116
loader .load ("A" );
120
117
loader .load ("B" );
121
- loader .loadMany (asList ("C" , "D" ));
118
+ loader .loadMany (keys );
119
+ loader .loadMany (keysAndContexts );
122
120
123
121
List <String > results = loader .dispatchAndJoin ();
124
122
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 )));
127
125
}
128
126
129
127
@ ParameterizedTest
130
128
@ 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 ) {
132
130
AtomicBoolean success = new AtomicBoolean ();
133
131
DataLoader <Integer , Integer > identityLoader = factory .idLoader (new DataLoaderOptions (), new ArrayList <>());
134
132
@@ -142,6 +140,26 @@ public void should_Support_loading_multiple_keys_in_one_call(TestDataLoaderFacto
142
140
assertThat (futureAll .toCompletableFuture ().join (), equalTo (asList (1 , 2 )));
143
141
}
144
142
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
+
145
163
@ ParameterizedTest
146
164
@ MethodSource ("dataLoaderFactories" )
147
165
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
159
177
160
178
@ ParameterizedTest
161
179
@ 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 ) {
163
196
AtomicBoolean success = new AtomicBoolean ();
164
197
DataLoader <Integer , Integer > identityLoader = factory .idLoader (new DataLoaderOptions (), new ArrayList <>());
165
198
CompletableFuture <List <Integer >> futureEmpty = identityLoader .loadMany (emptyList ());
@@ -172,6 +205,21 @@ public void should_Return_zero_entries_dispatched_when_no_keys_supplied(TestData
172
205
assertThat (dispatchResult .getKeysCount (), equalTo (0 ));
173
206
}
174
207
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
+
175
223
@ ParameterizedTest
176
224
@ MethodSource ("dataLoaderFactories" )
177
225
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
286
334
CompletableFuture <List <String >> future2 = identityLoader .loadMany (asList ("A" , "B" ));
287
335
identityLoader .dispatch ();
288
336
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 ());
290
344
assertThat (future1 .get (), equalTo ("A" ));
291
345
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" ))));
293
348
}
294
349
295
350
@ ParameterizedTest
0 commit comments