4
4
import com .github .benmanes .caffeine .cache .Caffeine ;
5
5
import org .dataloader .fixtures .CaffeineValueCache ;
6
6
import org .dataloader .fixtures .CustomValueCache ;
7
+ import org .dataloader .fixtures .parameterized .TestDataLoaderFactory ;
7
8
import org .dataloader .impl .DataLoaderAssertionException ;
8
- import org .junit .jupiter .api .Test ;
9
+ import org .junit .jupiter .params .ParameterizedTest ;
10
+ import org .junit .jupiter .params .provider .MethodSource ;
9
11
10
12
import java .util .ArrayList ;
13
+ import java .util .Collection ;
11
14
import java .util .List ;
12
15
import java .util .concurrent .CompletableFuture ;
13
16
import java .util .concurrent .TimeUnit ;
18
21
import static java .util .Collections .singletonList ;
19
22
import static org .awaitility .Awaitility .await ;
20
23
import static org .dataloader .DataLoaderOptions .newOptions ;
21
- import static org .dataloader .fixtures .TestKit .idLoader ;
22
24
import static org .dataloader .fixtures .TestKit .snooze ;
23
25
import static org .dataloader .fixtures .TestKit .sort ;
24
26
import static org .dataloader .impl .CompletableFutureKit .failedFuture ;
30
32
31
33
public class DataLoaderValueCacheTest {
32
34
33
- @ Test
34
- public void test_by_default_we_have_no_value_caching () {
35
- List <List <String >> loadCalls = new ArrayList <>();
35
+ @ ParameterizedTest
36
+ @ MethodSource ("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#getWithoutPublisher" )
37
+ public void test_by_default_we_have_no_value_caching (TestDataLoaderFactory factory ) {
38
+ List <Collection <String >> loadCalls = new ArrayList <>();
36
39
DataLoaderOptions options = newOptions ();
37
- DataLoader <String , String > identityLoader = idLoader (options , loadCalls );
40
+ DataLoader <String , String > identityLoader = factory . idLoader (options , loadCalls );
38
41
39
42
CompletableFuture <String > fA = identityLoader .load ("a" );
40
43
CompletableFuture <String > fB = identityLoader .load ("b" );
@@ -64,12 +67,13 @@ public void test_by_default_we_have_no_value_caching() {
64
67
assertThat (loadCalls , equalTo (emptyList ()));
65
68
}
66
69
67
- @ Test
68
- public void should_accept_a_remote_value_store_for_caching () {
70
+ @ ParameterizedTest
71
+ @ MethodSource ("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#getWithoutPublisher" )
72
+ public void should_accept_a_remote_value_store_for_caching (TestDataLoaderFactory factory ) {
69
73
CustomValueCache customValueCache = new CustomValueCache ();
70
- List <List <String >> loadCalls = new ArrayList <>();
74
+ List <Collection <String >> loadCalls = new ArrayList <>();
71
75
DataLoaderOptions options = newOptions ().setValueCache (customValueCache );
72
- DataLoader <String , String > identityLoader = idLoader (options , loadCalls );
76
+ DataLoader <String , String > identityLoader = factory . idLoader (options , loadCalls );
73
77
74
78
// Fetches as expected
75
79
@@ -108,8 +112,9 @@ public void should_accept_a_remote_value_store_for_caching() {
108
112
assertArrayEquals (customValueCache .store .keySet ().toArray (), emptyList ().toArray ());
109
113
}
110
114
111
- @ Test
112
- public void can_use_caffeine_for_caching () {
115
+ @ ParameterizedTest
116
+ @ MethodSource ("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#getWithoutPublisher" )
117
+ public void can_use_caffeine_for_caching (TestDataLoaderFactory factory ) {
113
118
//
114
119
// Mostly to prove that some other CACHE library could be used
115
120
// as the backing value cache. Not really Caffeine specific.
@@ -121,9 +126,9 @@ public void can_use_caffeine_for_caching() {
121
126
122
127
ValueCache <String , Object > caffeineValueCache = new CaffeineValueCache (caffeineCache );
123
128
124
- List <List <String >> loadCalls = new ArrayList <>();
129
+ List <Collection <String >> loadCalls = new ArrayList <>();
125
130
DataLoaderOptions options = newOptions ().setValueCache (caffeineValueCache );
126
- DataLoader <String , String > identityLoader = idLoader (options , loadCalls );
131
+ DataLoader <String , String > identityLoader = factory . idLoader (options , loadCalls );
127
132
128
133
// Fetches as expected
129
134
@@ -148,8 +153,9 @@ public void can_use_caffeine_for_caching() {
148
153
assertArrayEquals (caffeineCache .asMap ().keySet ().toArray (), asList ("a" , "b" , "c" ).toArray ());
149
154
}
150
155
151
- @ Test
152
- public void will_invoke_loader_if_CACHE_GET_call_throws_exception () {
156
+ @ ParameterizedTest
157
+ @ MethodSource ("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#getWithoutPublisher" )
158
+ public void will_invoke_loader_if_CACHE_GET_call_throws_exception (TestDataLoaderFactory factory ) {
153
159
CustomValueCache customValueCache = new CustomValueCache () {
154
160
155
161
@ Override
@@ -163,9 +169,9 @@ public CompletableFuture<Object> get(String key) {
163
169
customValueCache .set ("a" , "Not From Cache" );
164
170
customValueCache .set ("b" , "From Cache" );
165
171
166
- List <List <String >> loadCalls = new ArrayList <>();
172
+ List <Collection <String >> loadCalls = new ArrayList <>();
167
173
DataLoaderOptions options = newOptions ().setValueCache (customValueCache );
168
- DataLoader <String , String > identityLoader = idLoader (options , loadCalls );
174
+ DataLoader <String , String > identityLoader = factory . idLoader (options , loadCalls );
169
175
170
176
CompletableFuture <String > fA = identityLoader .load ("a" );
171
177
CompletableFuture <String > fB = identityLoader .load ("b" );
@@ -178,8 +184,9 @@ public CompletableFuture<Object> get(String key) {
178
184
assertThat (loadCalls , equalTo (singletonList (singletonList ("a" ))));
179
185
}
180
186
181
- @ Test
182
- public void will_still_work_if_CACHE_SET_call_throws_exception () {
187
+ @ ParameterizedTest
188
+ @ MethodSource ("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#getWithoutPublisher" )
189
+ public void will_still_work_if_CACHE_SET_call_throws_exception (TestDataLoaderFactory factory ) {
183
190
CustomValueCache customValueCache = new CustomValueCache () {
184
191
@ Override
185
192
public CompletableFuture <Object > set (String key , Object value ) {
@@ -190,9 +197,9 @@ public CompletableFuture<Object> set(String key, Object value) {
190
197
}
191
198
};
192
199
193
- List <List <String >> loadCalls = new ArrayList <>();
200
+ List <Collection <String >> loadCalls = new ArrayList <>();
194
201
DataLoaderOptions options = newOptions ().setValueCache (customValueCache );
195
- DataLoader <String , String > identityLoader = idLoader (options , loadCalls );
202
+ DataLoader <String , String > identityLoader = factory . idLoader (options , loadCalls );
196
203
197
204
CompletableFuture <String > fA = identityLoader .load ("a" );
198
205
CompletableFuture <String > fB = identityLoader .load ("b" );
@@ -206,8 +213,9 @@ public CompletableFuture<Object> set(String key, Object value) {
206
213
assertArrayEquals (customValueCache .store .keySet ().toArray (), singletonList ("b" ).toArray ());
207
214
}
208
215
209
- @ Test
210
- public void caching_can_take_some_time_complete () {
216
+ @ ParameterizedTest
217
+ @ MethodSource ("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#getWithoutPublisher" )
218
+ public void caching_can_take_some_time_complete (TestDataLoaderFactory factory ) {
211
219
CustomValueCache customValueCache = new CustomValueCache () {
212
220
213
221
@ Override
@@ -228,9 +236,9 @@ public CompletableFuture<Object> get(String key) {
228
236
};
229
237
230
238
231
- List <List <String >> loadCalls = new ArrayList <>();
239
+ List <Collection <String >> loadCalls = new ArrayList <>();
232
240
DataLoaderOptions options = newOptions ().setValueCache (customValueCache );
233
- DataLoader <String , String > identityLoader = idLoader (options , loadCalls );
241
+ DataLoader <String , String > identityLoader = factory . idLoader (options , loadCalls );
234
242
235
243
CompletableFuture <String > fA = identityLoader .load ("a" );
236
244
CompletableFuture <String > fB = identityLoader .load ("b" );
@@ -247,8 +255,9 @@ public CompletableFuture<Object> get(String key) {
247
255
assertThat (loadCalls , equalTo (singletonList (asList ("missC" , "missD" ))));
248
256
}
249
257
250
- @ Test
251
- public void batch_caching_works_as_expected () {
258
+ @ ParameterizedTest
259
+ @ MethodSource ("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#getWithoutPublisher" )
260
+ public void batch_caching_works_as_expected (TestDataLoaderFactory factory ) {
252
261
CustomValueCache customValueCache = new CustomValueCache () {
253
262
254
263
@ Override
@@ -269,9 +278,9 @@ public CompletableFuture<List<Try<Object>>> getValues(List<String> keys) {
269
278
};
270
279
271
280
272
- List <List <String >> loadCalls = new ArrayList <>();
281
+ List <Collection <String >> loadCalls = new ArrayList <>();
273
282
DataLoaderOptions options = newOptions ().setValueCache (customValueCache );
274
- DataLoader <String , String > identityLoader = idLoader (options , loadCalls );
283
+ DataLoader <String , String > identityLoader = factory . idLoader (options , loadCalls );
275
284
276
285
CompletableFuture <String > fA = identityLoader .load ("a" );
277
286
CompletableFuture <String > fB = identityLoader .load ("b" );
@@ -293,8 +302,9 @@ public CompletableFuture<List<Try<Object>>> getValues(List<String> keys) {
293
302
assertThat (values , equalTo (asList ("missC" , "missD" )));
294
303
}
295
304
296
- @ Test
297
- public void assertions_will_be_thrown_if_the_cache_does_not_follow_contract () {
305
+ @ ParameterizedTest
306
+ @ MethodSource ("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#getWithoutPublisher" )
307
+ public void assertions_will_be_thrown_if_the_cache_does_not_follow_contract (TestDataLoaderFactory factory ) {
298
308
CustomValueCache customValueCache = new CustomValueCache () {
299
309
300
310
@ Override
@@ -312,9 +322,9 @@ public CompletableFuture<List<Try<Object>>> getValues(List<String> keys) {
312
322
}
313
323
};
314
324
315
- List <List <String >> loadCalls = new ArrayList <>();
325
+ List <Collection <String >> loadCalls = new ArrayList <>();
316
326
DataLoaderOptions options = newOptions ().setValueCache (customValueCache );
317
- DataLoader <String , String > identityLoader = idLoader (options , loadCalls );
327
+ DataLoader <String , String > identityLoader = factory . idLoader (options , loadCalls );
318
328
319
329
CompletableFuture <String > fA = identityLoader .load ("a" );
320
330
CompletableFuture <String > fB = identityLoader .load ("b" );
@@ -335,8 +345,9 @@ private boolean isAssertionException(CompletableFuture<String> fA) {
335
345
}
336
346
337
347
338
- @ Test
339
- public void if_caching_is_off_its_never_hit () {
348
+ @ ParameterizedTest
349
+ @ MethodSource ("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#getWithoutPublisher" )
350
+ public void if_caching_is_off_its_never_hit (TestDataLoaderFactory factory ) {
340
351
AtomicInteger getCalls = new AtomicInteger ();
341
352
CustomValueCache customValueCache = new CustomValueCache () {
342
353
@@ -347,9 +358,9 @@ public CompletableFuture<Object> get(String key) {
347
358
}
348
359
};
349
360
350
- List <List <String >> loadCalls = new ArrayList <>();
361
+ List <Collection <String >> loadCalls = new ArrayList <>();
351
362
DataLoaderOptions options = newOptions ().setValueCache (customValueCache ).setCachingEnabled (false );
352
- DataLoader <String , String > identityLoader = idLoader (options , loadCalls );
363
+ DataLoader <String , String > identityLoader = factory . idLoader (options , loadCalls );
353
364
354
365
CompletableFuture <String > fA = identityLoader .load ("a" );
355
366
CompletableFuture <String > fB = identityLoader .load ("b" );
@@ -368,8 +379,9 @@ public CompletableFuture<Object> get(String key) {
368
379
assertTrue (customValueCache .asMap ().isEmpty ());
369
380
}
370
381
371
- @ Test
372
- public void if_everything_is_cached_no_batching_happens () {
382
+ @ ParameterizedTest
383
+ @ MethodSource ("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#getWithoutPublisher" )
384
+ public void if_everything_is_cached_no_batching_happens (TestDataLoaderFactory factory ) {
373
385
AtomicInteger getCalls = new AtomicInteger ();
374
386
AtomicInteger setCalls = new AtomicInteger ();
375
387
CustomValueCache customValueCache = new CustomValueCache () {
@@ -390,9 +402,9 @@ public CompletableFuture<List<Object>> setValues(List<String> keys, List<Object>
390
402
customValueCache .asMap ().put ("b" , "cachedB" );
391
403
customValueCache .asMap ().put ("c" , "cachedC" );
392
404
393
- List <List <String >> loadCalls = new ArrayList <>();
405
+ List <Collection <String >> loadCalls = new ArrayList <>();
394
406
DataLoaderOptions options = newOptions ().setValueCache (customValueCache ).setCachingEnabled (true );
395
- DataLoader <String , String > identityLoader = idLoader (options , loadCalls );
407
+ DataLoader <String , String > identityLoader = factory . idLoader (options , loadCalls );
396
408
397
409
CompletableFuture <String > fA = identityLoader .load ("a" );
398
410
CompletableFuture <String > fB = identityLoader .load ("b" );
@@ -410,8 +422,9 @@ public CompletableFuture<List<Object>> setValues(List<String> keys, List<Object>
410
422
}
411
423
412
424
413
- @ Test
414
- public void if_batching_is_off_it_still_can_cache () {
425
+ @ ParameterizedTest
426
+ @ MethodSource ("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#getWithoutPublisher" )
427
+ public void if_batching_is_off_it_still_can_cache (TestDataLoaderFactory factory ) {
415
428
AtomicInteger getCalls = new AtomicInteger ();
416
429
AtomicInteger setCalls = new AtomicInteger ();
417
430
CustomValueCache customValueCache = new CustomValueCache () {
@@ -430,9 +443,9 @@ public CompletableFuture<List<Object>> setValues(List<String> keys, List<Object>
430
443
};
431
444
customValueCache .asMap ().put ("a" , "cachedA" );
432
445
433
- List <List <String >> loadCalls = new ArrayList <>();
446
+ List <Collection <String >> loadCalls = new ArrayList <>();
434
447
DataLoaderOptions options = newOptions ().setValueCache (customValueCache ).setCachingEnabled (true ).setBatchingEnabled (false );
435
- DataLoader <String , String > identityLoader = idLoader (options , loadCalls );
448
+ DataLoader <String , String > identityLoader = factory . idLoader (options , loadCalls );
436
449
437
450
CompletableFuture <String > fA = identityLoader .load ("a" );
438
451
CompletableFuture <String > fB = identityLoader .load ("b" );
0 commit comments