Skip to content

Commit 6bb19e2

Browse files
committed
wip
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent 62c66ca commit 6bb19e2

File tree

2 files changed

+90
-13
lines changed

2 files changed

+90
-13
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/controller/ControllerEventSource.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,26 @@ private boolean isAcceptedByFilters(ResourceAction action, T resource, T oldReso
140140

141141
@Override
142142
public synchronized void onAdd(T resource) {
143-
var handling = temporaryResourceCache.onAddOrUpdateEvent(ResourceAction.ADDED, resource, null);
144-
handleEvent(ResourceAction.ADDED, resource, null, null, handling != EventHandling.NEW);
143+
handleOnAddOrUpdate(ResourceAction.ADDED, null, resource);
145144
}
146145

147146
@Override
148147
public synchronized void onUpdate(T oldCustomResource, T newCustomResource) {
148+
handleOnAddOrUpdate(ResourceAction.UPDATED, oldCustomResource, newCustomResource);
149+
}
150+
151+
private void handleOnAddOrUpdate(
152+
ResourceAction action, T oldCustomResource, T newCustomResource) {
149153
var handling =
150-
temporaryResourceCache.onAddOrUpdateEvent(
151-
ResourceAction.UPDATED, newCustomResource, oldCustomResource);
152-
handleEvent(
153-
ResourceAction.UPDATED,
154-
newCustomResource,
155-
oldCustomResource,
156-
null,
157-
handling != EventHandling.NEW);
154+
temporaryResourceCache.onAddOrUpdateEvent(action, newCustomResource, oldCustomResource);
155+
if (handling != EventHandling.NEW) {
156+
handleEvent(
157+
ResourceAction.UPDATED,
158+
newCustomResource,
159+
oldCustomResource,
160+
null,
161+
handling != EventHandling.NEW);
162+
}
158163
}
159164

160165
@Override

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/controller/ControllerEventSourceTest.java

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.junit.jupiter.api.Disabled;
2424
import org.junit.jupiter.api.Test;
2525

26+
import io.fabric8.kubernetes.client.KubernetesClientException;
2627
import io.javaoperatorsdk.operator.MockKubernetesClient;
2728
import io.javaoperatorsdk.operator.ReconcilerUtilsInternal;
2829
import io.javaoperatorsdk.operator.TestUtils;
@@ -44,6 +45,8 @@
4445
import io.javaoperatorsdk.operator.sample.simple.TestCustomResource;
4546

4647
import static io.javaoperatorsdk.operator.processing.event.source.EventFilterTestUtils.withResourceVersion;
48+
import static org.assertj.core.api.Assertions.assertThat;
49+
import static org.awaitility.Awaitility.await;
4750
import static org.mockito.ArgumentMatchers.eq;
4851
import static org.mockito.Mockito.*;
4952

@@ -168,17 +171,86 @@ void genericFilterFiltersOutAddUpdateAndDeleteEvents() {
168171
verify(eventHandler, never()).handleEvent(any());
169172
}
170173

171-
@Disabled
172174
@Test
173-
void testEventFiltering() throws InterruptedException {
175+
void testEventFilteringBasicScenario() throws InterruptedException {
174176
source = spy(new ControllerEventSource<>(new TestController(null, null, null)));
175177
setUpSource(source, true, controllerConfig);
176178

177179
var latch = sendForEventFilteringUpdate(2);
178180
source.onUpdate(testResourceWithVersion(1), testResourceWithVersion(2));
179181
latch.countDown();
182+
180183
Thread.sleep(100);
181-
verify(source, never()).handleEvent(any(), any(), any(), any(), anyBoolean());
184+
verify(eventHandler, never()).handleEvent(any());
185+
}
186+
187+
@Test
188+
void eventFilteringNewEventDuringUpdate() {
189+
source = spy(new ControllerEventSource<>(new TestController(null, null, null)));
190+
setUpSource(source, true, controllerConfig);
191+
192+
var latch = sendForEventFilteringUpdate(2);
193+
source.onUpdate(testResourceWithVersion(2), testResourceWithVersion(3));
194+
latch.countDown();
195+
196+
await().untilAsserted(() -> expectHandleEvent(3, 2));
197+
}
198+
199+
@Disabled("todo")
200+
@Test
201+
void eventFilteringMoreNewEventsDuringUpdate() {
202+
source = spy(new ControllerEventSource<>(new TestController(null, null, null)));
203+
setUpSource(source, true, controllerConfig);
204+
205+
var latch = sendForEventFilteringUpdate(2);
206+
source.onUpdate(testResourceWithVersion(2), testResourceWithVersion(3));
207+
source.onUpdate(testResourceWithVersion(3), testResourceWithVersion(4));
208+
latch.countDown();
209+
210+
await().untilAsserted(() -> expectHandleEvent(4, 2));
211+
}
212+
213+
@Test
214+
void eventFilteringExceptionDuringUpdate() {
215+
source = spy(new ControllerEventSource<>(new TestController(null, null, null)));
216+
setUpSource(source, true, controllerConfig);
217+
218+
var latch =
219+
EventFilterTestUtils.sendForEventFilteringUpdate(
220+
source,
221+
TestUtils.testCustomResource1(),
222+
r -> {
223+
throw new KubernetesClientException("fake");
224+
});
225+
source.onUpdate(testResourceWithVersion(1), testResourceWithVersion(2));
226+
latch.countDown();
227+
228+
expectHandleEvent(2, 1);
229+
}
230+
231+
private void expectHandleEvent(int newResourceVersion, int oldResourceVersion) {
232+
await()
233+
.untilAsserted(
234+
() -> {
235+
verify(eventHandler, times(1)).handleEvent(any());
236+
verify(source, times(1))
237+
.handleEvent(
238+
eq(ResourceAction.UPDATED),
239+
argThat(
240+
r -> {
241+
assertThat(r.getMetadata().getResourceVersion())
242+
.isEqualTo("" + newResourceVersion);
243+
return true;
244+
}),
245+
argThat(
246+
r -> {
247+
assertThat(r.getMetadata().getResourceVersion())
248+
.isEqualTo("" + oldResourceVersion);
249+
return true;
250+
}),
251+
isNull(),
252+
eq(false));
253+
});
182254
}
183255

184256
private TestCustomResource testResourceWithVersion(int v) {

0 commit comments

Comments
 (0)