Skip to content

If there is a specific predicate for a dataloader - its is the final say on whether to dispatch #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading