Skip to content

Commit 3b19eac

Browse files
committed
additional dispatcher tests
1 parent 68d76a1 commit 3b19eac

File tree

2 files changed

+52
-22
lines changed

2 files changed

+52
-22
lines changed

operator-framework/src/main/java/io/javaoperatorsdk/operator/processing/DefaultEventHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private void executeBufferedEvents(String customResourceUid) {
6969
log.debug("Executing events for custom resource. Scope: {}", executionScope);
7070
executor.execute(new ExecutionConsumer(executionScope, eventDispatcher, this));
7171
} else {
72-
log.debug("Not executing controller for {}, since currently under execution.", customResourceUid);
72+
log.debug("Skipping executing controller for {}, since currently under execution.", customResourceUid);
7373
}
7474
}
7575

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

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
import io.javaoperatorsdk.operator.api.UpdateControl;
88
import io.javaoperatorsdk.operator.processing.EventDispatcher;
99
import io.javaoperatorsdk.operator.processing.ExecutionScope;
10+
import io.javaoperatorsdk.operator.processing.ProcessingUtils;
11+
import io.javaoperatorsdk.operator.processing.event.Event;
1012
import io.javaoperatorsdk.operator.processing.event.internal.CustomResourceEvent;
13+
import io.javaoperatorsdk.operator.processing.event.internal.TimerEvent;
1114
import org.junit.jupiter.api.BeforeEach;
1215
import org.junit.jupiter.api.Test;
1316
import org.mockito.ArgumentMatchers;
1417

1518
import java.util.ArrayList;
1619
import java.util.Arrays;
1720
import java.util.Collections;
21+
import java.util.List;
1822

1923
import static org.junit.jupiter.api.Assertions.assertEquals;
2024
import static org.mockito.Mockito.*;
@@ -42,7 +46,7 @@ void setup() {
4246

4347
@Test
4448
void callCreateOrUpdateOnNewResource() {
45-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.ADDED, testCustomResource));
49+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.ADDED, testCustomResource));
4650
verify(controller, times(1)).createOrUpdateResource(ArgumentMatchers.eq(testCustomResource), any());
4751
}
4852

@@ -51,7 +55,7 @@ void updatesOnlyStatusSubResource() {
5155
when(controller.createOrUpdateResource(eq(testCustomResource), any()))
5256
.thenReturn(UpdateControl.updateStatusSubResource(testCustomResource));
5357

54-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.ADDED, testCustomResource));
58+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.ADDED, testCustomResource));
5559

5660
verify(customResourceFacade, times(1)).updateStatus(testCustomResource);
5761
verify(customResourceFacade, never()).replaceWithLock(any());
@@ -60,13 +64,13 @@ void updatesOnlyStatusSubResource() {
6064

6165
@Test
6266
void callCreateOrUpdateOnModifiedResource() {
63-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
67+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
6468
verify(controller, times(1)).createOrUpdateResource(ArgumentMatchers.eq(testCustomResource), any());
6569
}
6670

6771
@Test
6872
void adsDefaultFinalizerOnCreateIfNotThere() {
69-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
73+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
7074
verify(controller, times(1))
7175
.createOrUpdateResource(argThat(testCustomResource ->
7276
testCustomResource.getMetadata().getFinalizers().contains(DEFAULT_FINALIZER)), any());
@@ -77,7 +81,7 @@ void callsDeleteIfObjectHasFinalizerAndMarkedForDelete() {
7781
testCustomResource.getMetadata().setDeletionTimestamp("2019-8-10");
7882
testCustomResource.getMetadata().getFinalizers().add(DEFAULT_FINALIZER);
7983

80-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
84+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
8185

8286
verify(controller, times(1)).deleteResource(eq(testCustomResource), any());
8387
}
@@ -89,7 +93,7 @@ void callsDeleteIfObjectHasFinalizerAndMarkedForDelete() {
8993
void callDeleteOnControllerIfMarkedForDeletionButThereIsNoDefaultFinalizer() {
9094
markForDeletion(testCustomResource);
9195

92-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
96+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
9397

9498
verify(controller).deleteResource(eq(testCustomResource), any());
9599
}
@@ -98,7 +102,7 @@ void callDeleteOnControllerIfMarkedForDeletionButThereIsNoDefaultFinalizer() {
98102
void removesDefaultFinalizerOnDelete() {
99103
markForDeletion(testCustomResource);
100104

101-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
105+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
102106

103107
assertEquals(0, testCustomResource.getMetadata().getFinalizers().size());
104108
verify(customResourceFacade, times(1)).replaceWithLock(any());
@@ -109,7 +113,7 @@ void doesNotRemovesTheFinalizerIfTheDeleteNotMethodInstructsIt() {
109113
when(controller.deleteResource(eq(testCustomResource), any())).thenReturn(DeleteControl.NO_FINALIZER_REMOVAL);
110114
markForDeletion(testCustomResource);
111115

112-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
116+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
113117

114118
assertEquals(1, testCustomResource.getMetadata().getFinalizers().size());
115119
verify(customResourceFacade, never()).replaceWithLock(any());
@@ -119,7 +123,7 @@ void doesNotRemovesTheFinalizerIfTheDeleteNotMethodInstructsIt() {
119123
void doesNotUpdateTheResourceIfNoUpdateUpdateControl() {
120124
when(controller.createOrUpdateResource(eq(testCustomResource), any())).thenReturn(UpdateControl.noUpdate());
121125

122-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
126+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
123127
verify(customResourceFacade, never()).replaceWithLock(any());
124128
verify(customResourceFacade, never()).updateStatus(testCustomResource);
125129
}
@@ -129,7 +133,7 @@ void addsFinalizerIfNotMarkedForDeletionAndEmptyCustomResourceReturned() {
129133
removeFinalizers(testCustomResource);
130134
when(controller.createOrUpdateResource(eq(testCustomResource), any())).thenReturn(UpdateControl.noUpdate());
131135

132-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
136+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
133137

134138
assertEquals(1, testCustomResource.getMetadata().getFinalizers().size());
135139
verify(customResourceFacade, times(1)).replaceWithLock(any());
@@ -140,29 +144,48 @@ void doesNotCallDeleteIfMarkedForDeletionButNotOurFinalizer() {
140144
removeFinalizers(testCustomResource);
141145
markForDeletion(testCustomResource);
142146

143-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
147+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
144148

145149
verify(customResourceFacade, never()).replaceWithLock(any());
146150
verify(controller, never()).deleteResource(eq(testCustomResource), any());
147151
}
148152

153+
@Test
154+
void executeControllerRegardlessGenerationInNonGenerationAwareMode() {
155+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
156+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
157+
158+
verify(controller, times(2)).createOrUpdateResource(eq(testCustomResource), any());
159+
}
160+
149161
@Test
150162
void skipsControllerExecutionOnIfGenerationAwareModeIfNotLargerGeneration() {
151163
generationAwareMode();
152164

153-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
154-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
165+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
166+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
155167

156168
verify(controller, times(1)).createOrUpdateResource(eq(testCustomResource), any());
157169
}
158170

159171
@Test
160-
void skipsExecutesControllerOnGenerationIncrease() {
172+
void doNotSkipGenerationIfThereAreAdditionalEvents() {
173+
generationAwareMode();
174+
175+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
176+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource,
177+
nonCREvent(testCustomResource)));
178+
179+
verify(controller, times(2)).createOrUpdateResource(eq(testCustomResource), any());
180+
}
181+
182+
@Test
183+
void notSkipsExecutionOnGenerationIncrease() {
161184
generationAwareMode();
162185

163-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
186+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
164187
testCustomResource.getMetadata().setGeneration(testCustomResource.getMetadata().getGeneration() + 1);
165-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
188+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
166189

167190
verify(controller, times(2)).createOrUpdateResource(eq(testCustomResource), any());
168191
}
@@ -174,8 +197,8 @@ void doesNotMarkNewGenerationInCaseOfException() {
174197
.thenThrow(new IllegalStateException("Exception for testing purposes"))
175198
.thenReturn(UpdateControl.noUpdate());
176199

177-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
178-
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
200+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
201+
eventDispatcher.handleEvent(executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
179202

180203
verify(controller, times(2)).createOrUpdateResource(eq(testCustomResource), any());
181204

@@ -194,9 +217,16 @@ private void removeFinalizers(CustomResource customResource) {
194217
customResource.getMetadata().getFinalizers().clear();
195218
}
196219

197-
198-
public ExecutionScope customResourceEvent(Watcher.Action action, CustomResource resource) {
220+
public ExecutionScope executionScopeWithCREvent(Watcher.Action action, CustomResource resource, Event... otherEvents) {
199221
CustomResourceEvent event = new CustomResourceEvent(action, resource, null);
200-
return new ExecutionScope(Arrays.asList(event), resource);
222+
List<Event> eventList = new ArrayList<>(1 + otherEvents.length);
223+
eventList.add(event);
224+
eventList.addAll(Arrays.asList(otherEvents));
225+
return new ExecutionScope(eventList, resource);
226+
}
227+
228+
public Event nonCREvent(CustomResource resource) {
229+
return new TimerEvent(ProcessingUtils.getUID(resource), null);
201230
}
231+
202232
}

0 commit comments

Comments
 (0)