17
17
import io .javaoperatorsdk .operator .processing .event .DefaultEventSourceManager ;
18
18
import io .javaoperatorsdk .operator .processing .event .Event ;
19
19
import io .javaoperatorsdk .operator .processing .event .internal .CustomResourceEvent ;
20
+ import io .javaoperatorsdk .operator .processing .event .internal .CustomResourceEventSource ;
20
21
import io .javaoperatorsdk .operator .processing .event .internal .ResourceAction ;
21
22
import io .javaoperatorsdk .operator .processing .event .internal .TimerEventSource ;
22
23
import io .javaoperatorsdk .operator .processing .retry .GenericRetry ;
@@ -39,15 +40,15 @@ class DefaultEventHandlerTest {
39
40
private EventDispatcher eventDispatcherMock = mock (EventDispatcher .class );
40
41
private DefaultEventSourceManager defaultEventSourceManagerMock =
41
42
mock (DefaultEventSourceManager .class );
42
- private ResourceCache resourceCache = mock (ResourceCache .class );
43
+ private ResourceCache resourceCacheMock = mock (ResourceCache .class );
43
44
44
45
private TimerEventSource retryTimerEventSourceMock = mock (TimerEventSource .class );
45
46
46
47
private DefaultEventHandler defaultEventHandler =
47
- new DefaultEventHandler (eventDispatcherMock , resourceCache , "Test" , null , eventMarker );
48
+ new DefaultEventHandler (eventDispatcherMock , resourceCacheMock , "Test" , null , eventMarker );
48
49
49
50
private DefaultEventHandler defaultEventHandlerWithRetry =
50
- new DefaultEventHandler (eventDispatcherMock , resourceCache , "Test" ,
51
+ new DefaultEventHandler (eventDispatcherMock , resourceCacheMock , "Test" ,
51
52
GenericRetry .defaultLimitedExponentialRetry (), eventMarker );
52
53
53
54
@ BeforeEach
@@ -68,7 +69,7 @@ public void dispatchesEventsIfNoExecutionInProgress() {
68
69
@ Test
69
70
public void skipProcessingIfLatestCustomResourceNotInCache () {
70
71
Event event = prepareCREvent ();
71
- when (resourceCache .getCustomResource (event .getRelatedCustomResourceID ()))
72
+ when (resourceCacheMock .getCustomResource (event .getRelatedCustomResourceID ()))
72
73
.thenReturn (Optional .empty ());
73
74
74
75
defaultEventHandler .handleEvent (event );
@@ -213,7 +214,7 @@ public void cleansUpWhenDeleteEventReceivedAndNoEventPresent() {
213
214
214
215
@ Test
215
216
public void cleansUpAfterExecutionIfOnlyDeleteEventMarkLeft () {
216
- var cr = testCustomResource (new CustomResourceID ( UUID . randomUUID (). toString ()) );
217
+ var cr = testCustomResource ();
217
218
var crEvent = prepareCREvent (CustomResourceID .fromResource (cr ));
218
219
eventMarker .markDeleteEventReceived (crEvent .getRelatedCustomResourceID ());
219
220
var executionScope = new ExecutionScope (cr , null );
@@ -225,6 +226,60 @@ public void cleansUpAfterExecutionIfOnlyDeleteEventMarkLeft() {
225
226
.cleanupForCustomResource (eq (crEvent .getRelatedCustomResourceID ()));
226
227
}
227
228
229
+ @ Test
230
+ public void whitelistNextEventIfTheCacheIsNotPropagatedAfterAnUpdate () {
231
+ var crID = new CustomResourceID ("test-cr" , TEST_NAMESPACE );
232
+ var cr = testCustomResource (crID );
233
+ var updatedCr = testCustomResource (crID );
234
+ updatedCr .getMetadata ().setResourceVersion ("2" );
235
+ var mockCREventSource = mock (CustomResourceEventSource .class );
236
+ eventMarker .markEventReceived (crID );
237
+ when (resourceCacheMock .getCustomResource (eq (crID ))).thenReturn (Optional .of (cr ));
238
+ when (defaultEventSourceManagerMock .getCustomResourceEventSource ())
239
+ .thenReturn (mockCREventSource );
240
+
241
+ defaultEventHandler .eventProcessingFinished (new ExecutionScope (cr , null ),
242
+ PostExecutionControl .customResourceUpdated (updatedCr ));
243
+
244
+ verify (mockCREventSource , times (1 )).whitelistNextEvent (eq (crID ));
245
+ }
246
+
247
+ @ Test
248
+ public void dontWhitelistsEventWhenOtherChangeDuringExecution () {
249
+ var crID = new CustomResourceID ("test-cr" , TEST_NAMESPACE );
250
+ var cr = testCustomResource (crID );
251
+ var updatedCr = testCustomResource (crID );
252
+ updatedCr .getMetadata ().setResourceVersion ("2" );
253
+ var otherChangeCR = testCustomResource (crID );
254
+ otherChangeCR .getMetadata ().setResourceVersion ("3" );
255
+ var mockCREventSource = mock (CustomResourceEventSource .class );
256
+ eventMarker .markEventReceived (crID );
257
+ when (resourceCacheMock .getCustomResource (eq (crID ))).thenReturn (Optional .of (otherChangeCR ));
258
+ when (defaultEventSourceManagerMock .getCustomResourceEventSource ())
259
+ .thenReturn (mockCREventSource );
260
+
261
+ defaultEventHandler .eventProcessingFinished (new ExecutionScope (cr , null ),
262
+ PostExecutionControl .customResourceUpdated (updatedCr ));
263
+
264
+ verify (mockCREventSource , times (0 )).whitelistNextEvent (eq (crID ));
265
+ }
266
+
267
+ @ Test
268
+ public void dontWhitelistsEventIfUpdatedEventInCache () {
269
+ var crID = new CustomResourceID ("test-cr" , TEST_NAMESPACE );
270
+ var cr = testCustomResource (crID );
271
+ var mockCREventSource = mock (CustomResourceEventSource .class );
272
+ eventMarker .markEventReceived (crID );
273
+ when (resourceCacheMock .getCustomResource (eq (crID ))).thenReturn (Optional .of (cr ));
274
+ when (defaultEventSourceManagerMock .getCustomResourceEventSource ())
275
+ .thenReturn (mockCREventSource );
276
+
277
+ defaultEventHandler .eventProcessingFinished (new ExecutionScope (cr , null ),
278
+ PostExecutionControl .customResourceUpdated (cr ));
279
+
280
+ verify (mockCREventSource , times (0 )).whitelistNextEvent (eq (crID ));
281
+ }
282
+
228
283
private CustomResourceID eventAlreadyUnderProcessing () {
229
284
when (eventDispatcherMock .handleExecution (any ()))
230
285
.then (
@@ -243,7 +298,7 @@ private CustomResourceEvent prepareCREvent() {
243
298
244
299
private CustomResourceEvent prepareCREvent (CustomResourceID uid ) {
245
300
TestCustomResource customResource = testCustomResource (uid );
246
- when (resourceCache .getCustomResource (eq (uid ))).thenReturn (Optional .of (customResource ));
301
+ when (resourceCacheMock .getCustomResource (eq (uid ))).thenReturn (Optional .of (customResource ));
247
302
return new CustomResourceEvent (ResourceAction .UPDATED ,
248
303
CustomResourceID .fromResource (customResource ));
249
304
}
0 commit comments