Skip to content

fix: isLastAttempt on error handler correct if max attempt 0 #1595

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 5 commits into from
Nov 11, 2022
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 @@ -38,11 +38,17 @@ class ReconciliationDispatcher<P extends HasMetadata> {

private final Controller<P> controller;
private final CustomResourceFacade<P> customResourceFacade;
// this is to handle corner case, when there is a retry, but it is actually limited to 0.
// Usually for testing purposes.
private final boolean retryConfigurationHasZeroAttempts;

ReconciliationDispatcher(Controller<P> controller,
CustomResourceFacade<P> customResourceFacade) {
this.controller = controller;
this.customResourceFacade = customResourceFacade;

var retry = controller.getConfiguration().getRetry();
retryConfigurationHasZeroAttempts = retry == null || retry.initExecution().isLastAttempt();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. Didn't think of calling it in the ctor. I was convince that it would only work in a reconciliation context. LGTM

}

public ReconciliationDispatcher(Controller<P> controller) {
Expand Down Expand Up @@ -173,7 +179,9 @@ public int getAttemptCount() {

@Override
public boolean isLastAttempt() {
return controller.getConfiguration().getRetry() == null;
// check also if the retry is limited to 0
return retryConfigurationHasZeroAttempts ||
controller.getConfiguration().getRetry() == null;
}
});
((DefaultContext<P>) context).setRetryInfo(retryInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatcher;
import org.mockito.ArgumentMatchers;
import org.mockito.stubbing.Answer;

Expand Down Expand Up @@ -110,7 +111,10 @@ private <R extends HasMetadata> ReconciliationDispatcher<R> init(R customResourc
when(configuration.getFinalizerName()).thenReturn(DEFAULT_FINALIZER);
when(configuration.getName()).thenReturn("EventDispatcherTestController");
when(configuration.getResourceClass()).thenReturn(resourceClass);
when(configuration.getRetry()).thenReturn(new GenericRetry());
// needed so the retry can be predefined
if (configuration.getRetry() == null) {
when(configuration.getRetry()).thenReturn(new GenericRetry());
}
when(configuration.maxReconciliationInterval())
.thenReturn(Optional.of(Duration.ofHours(RECONCILIATION_MAX_INTERVAL)));

Expand Down Expand Up @@ -600,6 +604,34 @@ void errorStatusHandlerCanPatchResource() {
any(), any());
}

@Test
void ifRetryLimitedToZeroMaxAttemptsErrorHandlerGetsCorrectLastAttempt() {
var configuration =
MockControllerConfiguration
.forResource((Class<TestCustomResource>) testCustomResource.getClass());
when(configuration.getRetry()).thenReturn(new GenericRetry().setMaxAttempts(0));
reconciliationDispatcher =
init(testCustomResource, reconciler, configuration, customResourceFacade, false);

reconciler.reconcile = (r, c) -> {
throw new IllegalStateException("Error Status Test");
};
var mockErrorHandler = mock(ErrorStatusHandler.class);
when(mockErrorHandler.updateErrorStatus(any(), any(), any()))
.thenReturn(ErrorStatusUpdateControl.noStatusUpdate());
reconciler.errorHandler = mockErrorHandler;

reconciliationDispatcher.handleExecution(
new ExecutionScope(
testCustomResource, null));

verify(mockErrorHandler, times(1)).updateErrorStatus(any(),
ArgumentMatchers.argThat((ArgumentMatcher<Context<TestCustomResource>>) context -> {
var retryInfo = context.getRetryInfo().orElseThrow();
return retryInfo.isLastAttempt();
}), any());
}

@Test
void canSkipSchedulingMaxDelayIf() {
testCustomResource.addFinalizer(DEFAULT_FINALIZER);
Expand Down