|
23 | 23 | import org.junit.jupiter.api.Disabled; |
24 | 24 | import org.junit.jupiter.api.Test; |
25 | 25 |
|
| 26 | +import io.fabric8.kubernetes.client.KubernetesClientException; |
26 | 27 | import io.javaoperatorsdk.operator.MockKubernetesClient; |
27 | 28 | import io.javaoperatorsdk.operator.ReconcilerUtilsInternal; |
28 | 29 | import io.javaoperatorsdk.operator.TestUtils; |
|
44 | 45 | import io.javaoperatorsdk.operator.sample.simple.TestCustomResource; |
45 | 46 |
|
46 | 47 | 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; |
47 | 50 | import static org.mockito.ArgumentMatchers.eq; |
48 | 51 | import static org.mockito.Mockito.*; |
49 | 52 |
|
@@ -168,17 +171,86 @@ void genericFilterFiltersOutAddUpdateAndDeleteEvents() { |
168 | 171 | verify(eventHandler, never()).handleEvent(any()); |
169 | 172 | } |
170 | 173 |
|
171 | | - @Disabled |
172 | 174 | @Test |
173 | | - void testEventFiltering() throws InterruptedException { |
| 175 | + void testEventFilteringBasicScenario() throws InterruptedException { |
174 | 176 | source = spy(new ControllerEventSource<>(new TestController(null, null, null))); |
175 | 177 | setUpSource(source, true, controllerConfig); |
176 | 178 |
|
177 | 179 | var latch = sendForEventFilteringUpdate(2); |
178 | 180 | source.onUpdate(testResourceWithVersion(1), testResourceWithVersion(2)); |
179 | 181 | latch.countDown(); |
| 182 | + |
180 | 183 | 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 | + }); |
182 | 254 | } |
183 | 255 |
|
184 | 256 | private TestCustomResource testResourceWithVersion(int v) { |
|
0 commit comments