Skip to content

Commit bd101a2

Browse files
authored
Merge pull request #145 from graphql-java/specific-predicate-for-dl-is-respected
If there is a specific predicate for a dataloader - its is the final say on whether to dispatch
2 parents 1cd8029 + 432733b commit bd101a2

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

src/main/java/org/dataloader/registries/ScheduledDataLoaderRegistry.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ public void rescheduleNow() {
217217
}
218218

219219
/**
220-
* Returns true if the dataloader has a predicate which returned true, OR the overall
221-
* registry predicate returned true.
220+
* If a specific {@link DispatchPredicate} is registered for this dataloader then it uses it values
221+
* otherwise the overall registry predicate is used.
222222
*
223223
* @param dataLoaderKey the key in the dataloader map
224224
* @param dataLoader the dataloader
@@ -228,9 +228,7 @@ public void rescheduleNow() {
228228
private boolean shouldDispatch(String dataLoaderKey, DataLoader<?, ?> dataLoader) {
229229
DispatchPredicate dispatchPredicate = dataLoaderPredicates.get(dataLoader);
230230
if (dispatchPredicate != null) {
231-
if (dispatchPredicate.test(dataLoaderKey, dataLoader)) {
232-
return true;
233-
}
231+
return dispatchPredicate.test(dataLoaderKey, dataLoader);
234232
}
235233
return this.dispatchPredicate.test(dataLoaderKey, dataLoader);
236234
}

src/test/java/org/dataloader/registries/ScheduledDataLoaderRegistryPredicateTest.java

+19-13
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ public void test_the_registry_overall_predicate_firing_works() {
139139
DataLoader<Object, Object> dlB = newDataLoader(identityBatchLoader);
140140
DataLoader<Object, Object> dlC = newDataLoader(identityBatchLoader);
141141

142-
DispatchPredicate predicateOnSix = new CountingDispatchPredicate(6);
142+
DispatchPredicate predicateOnThree = new CountingDispatchPredicate(3);
143143

144144
ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
145-
.register("a", dlA, DISPATCH_NEVER)
146-
.register("b", dlB, DISPATCH_NEVER)
147-
.register("c", dlC, DISPATCH_NEVER)
148-
.dispatchPredicate(predicateOnSix)
145+
.register("a", dlA, new CountingDispatchPredicate(99))
146+
.register("b", dlB, new CountingDispatchPredicate(99))
147+
.register("c", dlC) // has none
148+
.dispatchPredicate(predicateOnThree)
149149
.schedule(Duration.ofHours(1000))
150150
.build();
151151

@@ -160,16 +160,22 @@ public void test_the_registry_overall_predicate_firing_works() {
160160
assertThat(cfB.isDone(), equalTo(false));
161161
assertThat(cfC.isDone(), equalTo(false));
162162

163-
count = registry.dispatchAllWithCount(); // second firing but the overall been asked 6 times already
163+
count = registry.dispatchAllWithCount(); // second firing
164164
assertThat(count, equalTo(0));
165165
assertThat(cfA.isDone(), equalTo(false));
166166
assertThat(cfB.isDone(), equalTo(false));
167167
assertThat(cfC.isDone(), equalTo(false));
168168

169-
count = registry.dispatchAllWithCount(); // third firing but the overall been asked 9 times already
170-
assertThat(count, equalTo(3));
171-
assertThat(cfA.isDone(), equalTo(true));
172-
assertThat(cfB.isDone(), equalTo(true));
169+
count = registry.dispatchAllWithCount(); // third firing
170+
assertThat(count, equalTo(0));
171+
assertThat(cfA.isDone(), equalTo(false));
172+
assertThat(cfB.isDone(), equalTo(false));
173+
assertThat(cfC.isDone(), equalTo(false));
174+
175+
count = registry.dispatchAllWithCount(); // fourth firing
176+
assertThat(count, equalTo(1));
177+
assertThat(cfA.isDone(), equalTo(false));
178+
assertThat(cfB.isDone(), equalTo(false)); // they wont ever finish until 99 calls
173179
assertThat(cfC.isDone(), equalTo(true));
174180
}
175181

@@ -217,9 +223,9 @@ public void test_the_registry_overall_predicate_firing_works_when_on_schedule()
217223
DispatchPredicate predicateOnTwenty = new CountingDispatchPredicate(20);
218224

219225
ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
220-
.register("a", dlA, DISPATCH_NEVER)
221-
.register("b", dlB, DISPATCH_NEVER)
222-
.register("c", dlC, DISPATCH_NEVER)
226+
.register("a", dlA)
227+
.register("b", dlB)
228+
.register("c", dlC)
223229
.dispatchPredicate(predicateOnTwenty)
224230
.schedule(Duration.ofMillis(5))
225231
.build();

0 commit comments

Comments
 (0)