Skip to content

Commit ecbe5f7

Browse files
committed
Improvements on Test Flakyness
1 parent 0494505 commit ecbe5f7

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/retry/GenericRetryExecution.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ public class GenericRetryExecution implements RetryExecution {
66

77
private final GenericRetry genericRetry;
88

9-
private int lastAttemptIndex = 0;
10-
private long currentInterval;
9+
private volatile int lastAttemptIndex = 0;
10+
private volatile long currentInterval;
1111

1212
public GenericRetryExecution(GenericRetry genericRetry) {
1313
this.genericRetry = genericRetry;

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/DefaultEventHandlerTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,14 @@ public void executesTheControllerInstantlyAfterErrorIfEventsBuffered() {
160160
PostExecutionControl postExecutionControl =
161161
PostExecutionControl.exceptionDuringExecution(new RuntimeException("test"));
162162

163+
when(eventDispatcherMock.handleExecution(any()))
164+
.thenReturn(postExecutionControl)
165+
.thenReturn(PostExecutionControl.defaultDispatch());
166+
163167
// start processing an event
164168
defaultEventHandlerWithRetry.handleEvent(event);
165-
// buffer an another event
169+
// buffer another event
166170
defaultEventHandlerWithRetry.handleEvent(event);
167-
verify(eventDispatcherMock, timeout(SEPARATE_EXECUTION_TIMEOUT).times(1))
168-
.handleExecution(any());
169-
170-
defaultEventHandlerWithRetry.eventProcessingFinished(executionScope, postExecutionControl);
171171

172172
ArgumentCaptor<ExecutionScope> executionScopeArgumentCaptor =
173173
ArgumentCaptor.forClass(ExecutionScope.class);
@@ -187,7 +187,7 @@ public void successfulExecutionResetsTheRetry() {
187187
Event event = prepareCREvent();
188188
TestCustomResource customResource = testCustomResource();
189189
customResource.getMetadata().setUid(event.getRelatedCustomResourceUid());
190-
ExecutionScope executionScope = new ExecutionScope(Arrays.asList(event), customResource, null);
190+
191191
PostExecutionControl postExecutionControlWithException =
192192
PostExecutionControl.exceptionDuringExecution(new RuntimeException("test"));
193193
PostExecutionControl defaultDispatchControl = PostExecutionControl.defaultDispatch();
@@ -200,15 +200,14 @@ public void successfulExecutionResetsTheRetry() {
200200
ArgumentCaptor.forClass(ExecutionScope.class);
201201

202202
defaultEventHandlerWithRetry.handleEvent(event);
203-
204203
verify(eventDispatcherMock, timeout(SEPARATE_EXECUTION_TIMEOUT).times(1))
205204
.handleExecution(any());
206-
defaultEventHandlerWithRetry.handleEvent(event);
207205

206+
defaultEventHandlerWithRetry.handleEvent(event);
208207
verify(eventDispatcherMock, timeout(SEPARATE_EXECUTION_TIMEOUT).times(2))
209208
.handleExecution(any());
210-
defaultEventHandlerWithRetry.handleEvent(event);
211209

210+
defaultEventHandlerWithRetry.handleEvent(event);
212211
verify(eventDispatcherMock, timeout(SEPARATE_EXECUTION_TIMEOUT).times(3))
213212
.handleExecution(executionScopeArgumentCaptor.capture());
214213
log.info("Finished successfulExecutionResetsTheRetry");

operator-framework-junit5/src/main/java/io/javaoperatorsdk/operator/junit/OperatorExtension.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ public <T extends HasMetadata> T replace(Class<T> type, T resource) {
135135
return kubernetesClient.resources(type).inNamespace(namespace).replace(resource);
136136
}
137137

138+
public <T extends HasMetadata> T get(Class<T> type, String name) {
139+
return kubernetesClient.resources(type).inNamespace(namespace).withName(name).get();
140+
}
141+
142+
138143
@SuppressWarnings("unchecked")
139144
protected void before(ExtensionContext context) {
140145
namespace = context.getRequiredTestClass().getSimpleName();

operator-framework/src/test/java/io/javaoperatorsdk/operator/SubResourceUpdateIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
import io.javaoperatorsdk.operator.sample.subresource.SubResourceTestCustomResource;
1313
import io.javaoperatorsdk.operator.sample.subresource.SubResourceTestCustomResourceController;
1414
import io.javaoperatorsdk.operator.sample.subresource.SubResourceTestCustomResourceSpec;
15-
import io.javaoperatorsdk.operator.sample.subresource.SubResourceTestCustomResourceStatus;
1615
import io.javaoperatorsdk.operator.support.TestUtils;
1716

17+
import static io.javaoperatorsdk.operator.sample.subresource.SubResourceTestCustomResourceStatus.State.SUCCESS;
1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.awaitility.Awaitility.await;
2020

@@ -87,7 +87,7 @@ public void updateCustomResourceAfterSubResourceChange() {
8787
awaitStatusUpdated(resource.getMetadata().getName());
8888

8989
// wait for sure, there are no more events
90-
waitXms(200);
90+
waitXms(500);
9191
// there is no event on status update processed
9292
assertThat(TestUtils.getNumberOfExecutions(operator))
9393
.isEqualTo(3);
@@ -104,7 +104,7 @@ void awaitStatusUpdated(String name) {
104104
assertThat(cr).isNotNull();
105105
assertThat(cr.getStatus()).isNotNull();
106106
assertThat(cr.getStatus().getState())
107-
.isEqualTo(SubResourceTestCustomResourceStatus.State.SUCCESS);
107+
.isEqualTo(SUCCESS);
108108
});
109109
}
110110

0 commit comments

Comments
 (0)