-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathScheduledDataLoaderRegistryTest.java
More file actions
421 lines (329 loc) · 15.7 KB
/
ScheduledDataLoaderRegistryTest.java
File metadata and controls
421 lines (329 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package org.dataloader.registries;
import org.awaitility.core.ConditionTimeoutException;
import org.dataloader.DataLoader;
import org.dataloader.DataLoaderRegistry;
import org.dataloader.errors.StrictModeRegistryException;
import org.dataloader.fixtures.parameterized.TestDataLoaderFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.awaitility.Awaitility.await;
import static org.awaitility.Duration.TWO_SECONDS;
import static org.dataloader.DataLoaderFactory.newDataLoader;
import static org.dataloader.fixtures.TestKit.snooze;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class ScheduledDataLoaderRegistryTest {
DispatchPredicate alwaysDispatch = (key, dl) -> true;
DispatchPredicate neverDispatch = (key, dl) -> false;
@ParameterizedTest
@MethodSource("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#get")
public void basic_setup_works_like_a_normal_dlr(TestDataLoaderFactory factory) {
List<Collection<String>> aCalls = new ArrayList<>();
List<Collection<String>> bCalls = new ArrayList<>();
DataLoader<String, String> dlA = factory.idLoader(aCalls);
dlA.load("AK1");
dlA.load("AK2");
DataLoader<String, String> dlB = factory.idLoader(bCalls);
dlB.load("BK1");
dlB.load("BK2");
DataLoaderRegistry otherDLR = DataLoaderRegistry.newRegistry().register("b", dlB).build();
ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
.register("a", dlA)
.registerAll(otherDLR)
.dispatchPredicate(alwaysDispatch)
.scheduledExecutorService(Executors.newSingleThreadScheduledExecutor())
.schedule(Duration.ofMillis(100))
.build();
assertThat(registry.getScheduleDuration(), equalTo(Duration.ofMillis(100)));
int count = registry.dispatchAllWithCount();
assertThat(count, equalTo(4));
assertThat(aCalls, equalTo(singletonList(asList("AK1", "AK2"))));
assertThat(bCalls, equalTo(singletonList(asList("BK1", "BK2"))));
}
@ParameterizedTest
@MethodSource("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#get")
public void predicate_always_false(TestDataLoaderFactory factory) {
List<Collection<String>> calls = new ArrayList<>();
DataLoader<String, String> dlA = factory.idLoader(calls);
dlA.load("K1");
dlA.load("K2");
ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
.register("a", dlA)
.dispatchPredicate(neverDispatch)
.schedule(Duration.ofMillis(10))
.build();
int count = registry.dispatchAllWithCount();
assertThat(count, equalTo(0));
assertThat(calls.size(), equalTo(0));
snooze(200);
count = registry.dispatchAllWithCount();
assertThat(count, equalTo(0));
assertThat(calls.size(), equalTo(0));
snooze(200);
count = registry.dispatchAllWithCount();
assertThat(count, equalTo(0));
assertThat(calls.size(), equalTo(0));
}
@ParameterizedTest
@MethodSource("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#get")
public void predicate_that_eventually_returns_true(TestDataLoaderFactory factory) {
AtomicInteger counter = new AtomicInteger();
DispatchPredicate neverDispatch = (key, dl) -> counter.incrementAndGet() > 5;
List<Collection<String>> calls = new ArrayList<>();
DataLoader<String, String> dlA = factory.idLoader(calls);
CompletableFuture<String> p1 = dlA.load("K1");
CompletableFuture<String> p2 = dlA.load("K2");
ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
.register("a", dlA)
.dispatchPredicate(neverDispatch)
.schedule(Duration.ofMillis(10))
.build();
int count = registry.dispatchAllWithCount();
assertThat(count, equalTo(0));
assertThat(calls.size(), equalTo(0));
assertFalse(p1.isDone());
assertFalse(p2.isDone());
snooze(200);
registry.dispatchAll();
assertTrue(p1.isDone());
assertTrue(p2.isDone());
}
@ParameterizedTest
@MethodSource("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#get")
public void dispatchAllWithCountImmediately(TestDataLoaderFactory factory) {
List<Collection<String>> calls = new ArrayList<>();
DataLoader<String, String> dlA = factory.idLoader(calls);
dlA.load("K1");
dlA.load("K2");
ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
.register("a", dlA)
.dispatchPredicate(neverDispatch)
.schedule(Duration.ofMillis(10))
.build();
int count = registry.dispatchAllWithCountImmediately();
assertThat(count, equalTo(2));
assertThat(calls, equalTo(singletonList(asList("K1", "K2"))));
}
@ParameterizedTest
@MethodSource("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#get")
public void dispatchAllImmediately(TestDataLoaderFactory factory) {
List<Collection<String>> calls = new ArrayList<>();
DataLoader<String, String> dlA = factory.idLoader(calls);
dlA.load("K1");
dlA.load("K2");
ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
.register("a", dlA)
.dispatchPredicate(neverDispatch)
.schedule(Duration.ofMillis(10))
.build();
registry.dispatchAllImmediately();
assertThat(calls, equalTo(singletonList(asList("K1", "K2"))));
}
@ParameterizedTest
@MethodSource("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#get")
public void rescheduleNow(TestDataLoaderFactory factory) {
AtomicInteger i = new AtomicInteger();
DispatchPredicate countingPredicate = (dataLoaderKey, dataLoader) -> i.incrementAndGet() > 5;
List<Collection<String>> calls = new ArrayList<>();
DataLoader<String, String> dlA = factory.idLoader(calls);
dlA.load("K1");
dlA.load("K2");
ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
.register("a", dlA)
.dispatchPredicate(countingPredicate)
.schedule(Duration.ofMillis(100))
.build();
// we never called dispatch per say - we started the scheduling direct
registry.rescheduleNow();
assertTrue(calls.isEmpty());
snooze(2000);
assertThat(calls, equalTo(singletonList(asList("K1", "K2"))));
}
@ParameterizedTest
@MethodSource("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#get")
public void it_will_take_out_the_schedule_once_it_dispatches(TestDataLoaderFactory factory) {
AtomicInteger counter = new AtomicInteger();
DispatchPredicate countingPredicate = (dataLoaderKey, dataLoader) -> counter.incrementAndGet() > 5;
List<Collection<String>> calls = new ArrayList<>();
DataLoader<String, String> dlA = factory.idLoader(calls);
dlA.load("K1");
dlA.load("K2");
ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
.register("a", dlA)
.dispatchPredicate(countingPredicate)
.schedule(Duration.ofMillis(100))
.build();
registry.dispatchAll();
// we have 5 * 100 mills to reach this line
assertTrue(calls.isEmpty());
snooze(2000);
assertThat(calls, equalTo(singletonList(asList("K1", "K2"))));
// reset our counter state
counter.set(0);
dlA.load("K3");
dlA.load("K4");
// no one has called dispatch - there is no rescheduling
snooze(2000);
assertThat(calls, equalTo(singletonList(asList("K1", "K2"))));
registry.dispatchAll();
// we have 5 * 100 mills to reach this line
assertThat(calls, equalTo(singletonList(asList("K1", "K2"))));
snooze(2000);
assertThat(calls, equalTo(asList(asList("K1", "K2"), asList("K3", "K4"))));
}
@ParameterizedTest
@MethodSource("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#get")
public void close_is_a_one_way_door(TestDataLoaderFactory factory) {
AtomicInteger counter = new AtomicInteger();
DispatchPredicate countingPredicate = (dataLoaderKey, dataLoader) -> {
counter.incrementAndGet();
return false;
};
DataLoader<String, String> dlA = factory.idLoader();
dlA.load("K1");
dlA.load("K2");
ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
.register("a", dlA)
.dispatchPredicate(countingPredicate)
.schedule(Duration.ofMillis(10))
.build();
registry.rescheduleNow();
snooze(200);
assertTrue(counter.get() > 0);
registry.close();
snooze(100);
int countThen = counter.get();
registry.rescheduleNow();
snooze(200);
assertEquals(counter.get(), countThen);
registry.rescheduleNow();
snooze(200);
assertEquals(counter.get(), countThen);
registry.dispatchAll();
snooze(200);
assertEquals(counter.get(), countThen + 1); // will have re-entered
snooze(200);
assertEquals(counter.get(), countThen + 1);
}
@ParameterizedTest
@MethodSource("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#get")
public void can_tick_after_first_dispatch_for_chain_data_loaders(TestDataLoaderFactory factory) {
// delays much bigger than the tick rate will mean multiple calls to dispatch
DataLoader<String, String> dlA = factory.idLoaderDelayed(Duration.ofMillis(100));
DataLoader<String, String> dlB = factory.idLoaderDelayed(Duration.ofMillis(200));
CompletableFuture<String> chainedCF = dlA.load("AK1").thenCompose(dlB::load);
AtomicBoolean done = new AtomicBoolean();
chainedCF.whenComplete((v, t) -> done.set(true));
ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
.register("a", dlA)
.register("b", dlB)
.dispatchPredicate(alwaysDispatch)
.schedule(Duration.ofMillis(10))
.tickerMode(true)
.build();
assertThat(registry.isTickerMode(), equalTo(true));
int count = registry.dispatchAllWithCount();
assertThat(count, equalTo(1));
await().atMost(TWO_SECONDS).untilAtomic(done, is(true));
registry.close();
}
@ParameterizedTest
@MethodSource("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#get")
public void chain_data_loaders_will_hang_if_not_in_ticker_mode(TestDataLoaderFactory factory) {
// delays much bigger than the tick rate will mean multiple calls to dispatch
DataLoader<String, String> dlA = factory.idLoaderDelayed(Duration.ofMillis(100));
DataLoader<String, String> dlB = factory.idLoaderDelayed(Duration.ofMillis(200));
CompletableFuture<String> chainedCF = dlA.load("AK1").thenCompose(dlB::load);
AtomicBoolean done = new AtomicBoolean();
chainedCF.whenComplete((v, t) -> done.set(true));
ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
.register("a", dlA)
.register("b", dlB)
.dispatchPredicate(alwaysDispatch)
.schedule(Duration.ofMillis(10))
.tickerMode(false)
.build();
assertThat(registry.isTickerMode(), equalTo(false));
int count = registry.dispatchAllWithCount();
assertThat(count, equalTo(1));
try {
await().atMost(TWO_SECONDS).untilAtomic(done, is(true));
fail("This should not have completed but rather timed out");
} catch (ConditionTimeoutException expected) {
}
registry.close();
}
@Test
public void executors_are_shutdown() {
ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry().build();
ScheduledExecutorService executorService = registry.getScheduledExecutorService();
assertThat(executorService.isShutdown(), equalTo(false));
registry.close();
assertThat(executorService.isShutdown(), equalTo(true));
executorService = Executors.newSingleThreadScheduledExecutor();
registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
.scheduledExecutorService(executorService).build();
executorService = registry.getScheduledExecutorService();
assertThat(executorService.isShutdown(), equalTo(false));
registry.close();
// if they provide the executor, we don't close it down
assertThat(executorService.isShutdown(), equalTo(false));
}
@Test
public void strictMode_works() {
DataLoader<Object, Object> dlA = newDataLoader(CompletableFuture::completedFuture);
DataLoader<Object, Object> dlB = newDataLoader(CompletableFuture::completedFuture);
assertThrows(StrictModeRegistryException.class, () -> {
ScheduledDataLoaderRegistry.newRegistry()
.strictMode(true)
.register("a", dlA)
.register("a", dlB)
.build();
});
assertThrows(StrictModeRegistryException.class, () -> {
ScheduledDataLoaderRegistry.newRegistry()
.strictMode(true)
.register("a", dlA)
.registerAll(ScheduledDataLoaderRegistry.newRegistry()
.register("a", dlB)
.build())
.build();
});
DataLoaderRegistry registry = ScheduledDataLoaderRegistry.newRegistry()
.strictMode(true)
.build();
registry.register("a", dlA);
assertThrows(StrictModeRegistryException.class, () -> {
registry.register("a", dlB);
});
assertThrows(StrictModeRegistryException.class, () -> {
registry.register(newDataLoader("a", CompletableFuture::completedFuture));
});
assertThrows(StrictModeRegistryException.class, () -> {
registry.registerAndGet("a", dlB);
});
assertThrows(StrictModeRegistryException.class, () -> {
registry.combine(ScheduledDataLoaderRegistry.newRegistry()
.register("a", dlB)
.build());
});
}
}