Skip to content

Commit

Permalink
Merge pull request #145 from graphql-java/specific-predicate-for-dl-i…
Browse files Browse the repository at this point in the history
…s-respected

If there is a specific predicate for a dataloader - its is the final say on whether to dispatch
  • Loading branch information
bbakerman authored Apr 13, 2024
2 parents 1cd8029 + 432733b commit bd101a2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ public void rescheduleNow() {
}

/**
* Returns true if the dataloader has a predicate which returned true, OR the overall
* registry predicate returned true.
* If a specific {@link DispatchPredicate} is registered for this dataloader then it uses it values
* otherwise the overall registry predicate is used.
*
* @param dataLoaderKey the key in the dataloader map
* @param dataLoader the dataloader
Expand All @@ -228,9 +228,7 @@ public void rescheduleNow() {
private boolean shouldDispatch(String dataLoaderKey, DataLoader<?, ?> dataLoader) {
DispatchPredicate dispatchPredicate = dataLoaderPredicates.get(dataLoader);
if (dispatchPredicate != null) {
if (dispatchPredicate.test(dataLoaderKey, dataLoader)) {
return true;
}
return dispatchPredicate.test(dataLoaderKey, dataLoader);
}
return this.dispatchPredicate.test(dataLoaderKey, dataLoader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ public void test_the_registry_overall_predicate_firing_works() {
DataLoader<Object, Object> dlB = newDataLoader(identityBatchLoader);
DataLoader<Object, Object> dlC = newDataLoader(identityBatchLoader);

DispatchPredicate predicateOnSix = new CountingDispatchPredicate(6);
DispatchPredicate predicateOnThree = new CountingDispatchPredicate(3);

ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
.register("a", dlA, DISPATCH_NEVER)
.register("b", dlB, DISPATCH_NEVER)
.register("c", dlC, DISPATCH_NEVER)
.dispatchPredicate(predicateOnSix)
.register("a", dlA, new CountingDispatchPredicate(99))
.register("b", dlB, new CountingDispatchPredicate(99))
.register("c", dlC) // has none
.dispatchPredicate(predicateOnThree)
.schedule(Duration.ofHours(1000))
.build();

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

count = registry.dispatchAllWithCount(); // second firing but the overall been asked 6 times already
count = registry.dispatchAllWithCount(); // second firing
assertThat(count, equalTo(0));
assertThat(cfA.isDone(), equalTo(false));
assertThat(cfB.isDone(), equalTo(false));
assertThat(cfC.isDone(), equalTo(false));

count = registry.dispatchAllWithCount(); // third firing but the overall been asked 9 times already
assertThat(count, equalTo(3));
assertThat(cfA.isDone(), equalTo(true));
assertThat(cfB.isDone(), equalTo(true));
count = registry.dispatchAllWithCount(); // third firing
assertThat(count, equalTo(0));
assertThat(cfA.isDone(), equalTo(false));
assertThat(cfB.isDone(), equalTo(false));
assertThat(cfC.isDone(), equalTo(false));

count = registry.dispatchAllWithCount(); // fourth firing
assertThat(count, equalTo(1));
assertThat(cfA.isDone(), equalTo(false));
assertThat(cfB.isDone(), equalTo(false)); // they wont ever finish until 99 calls
assertThat(cfC.isDone(), equalTo(true));
}

Expand Down Expand Up @@ -217,9 +223,9 @@ public void test_the_registry_overall_predicate_firing_works_when_on_schedule()
DispatchPredicate predicateOnTwenty = new CountingDispatchPredicate(20);

ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
.register("a", dlA, DISPATCH_NEVER)
.register("b", dlB, DISPATCH_NEVER)
.register("c", dlC, DISPATCH_NEVER)
.register("a", dlA)
.register("b", dlB)
.register("c", dlC)
.dispatchPredicate(predicateOnTwenty)
.schedule(Duration.ofMillis(5))
.build();
Expand Down

0 comments on commit bd101a2

Please sign in to comment.