|
1 | 1 | package io.javaoperatorsdk.operator.processing.event.internal; |
2 | 2 |
|
| 3 | +import java.io.IOException; |
3 | 4 | import java.time.LocalDateTime; |
4 | 5 | import java.util.List; |
5 | 6 |
|
6 | 7 | import org.junit.jupiter.api.BeforeEach; |
7 | 8 | import org.junit.jupiter.api.Test; |
8 | 9 |
|
9 | 10 | import io.fabric8.kubernetes.api.model.KubernetesResourceList; |
| 11 | +import io.fabric8.kubernetes.api.model.ListOptions; |
| 12 | +import io.fabric8.kubernetes.client.Watch; |
10 | 13 | import io.fabric8.kubernetes.client.Watcher; |
| 14 | +import io.fabric8.kubernetes.client.dsl.FilterWatchListMultiDeletable; |
11 | 15 | import io.fabric8.kubernetes.client.dsl.MixedOperation; |
12 | 16 | import io.fabric8.kubernetes.client.dsl.Resource; |
13 | 17 | import io.javaoperatorsdk.operator.Metrics; |
14 | 18 | import io.javaoperatorsdk.operator.TestUtils; |
15 | 19 | import io.javaoperatorsdk.operator.api.config.ConfigurationService; |
16 | 20 | import io.javaoperatorsdk.operator.api.config.DefaultControllerConfiguration; |
17 | 21 | import io.javaoperatorsdk.operator.processing.ConfiguredController; |
| 22 | +import io.javaoperatorsdk.operator.processing.CustomResourceCache; |
| 23 | +import io.javaoperatorsdk.operator.processing.DefaultEventHandler; |
| 24 | +import io.javaoperatorsdk.operator.processing.EventDispatcher; |
| 25 | +import io.javaoperatorsdk.operator.processing.event.DefaultEventSourceManager; |
18 | 26 | import io.javaoperatorsdk.operator.processing.event.EventHandler; |
19 | 27 | import io.javaoperatorsdk.operator.sample.simple.TestCustomResource; |
20 | 28 |
|
| 29 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
21 | 31 | import static org.mockito.Mockito.any; |
| 32 | +import static org.mockito.Mockito.doCallRealMethod; |
22 | 33 | import static org.mockito.Mockito.mock; |
| 34 | +import static org.mockito.Mockito.timeout; |
23 | 35 | import static org.mockito.Mockito.times; |
24 | 36 | import static org.mockito.Mockito.verify; |
25 | 37 | import static org.mockito.Mockito.when; |
@@ -102,6 +114,46 @@ public void eventNotMarkedForLastGenerationIfNoFinalizer() { |
102 | 114 | verify(eventHandler, times(2)).handleEvent(any()); |
103 | 115 | } |
104 | 116 |
|
| 117 | + @Test |
| 118 | + public void restartingShouldResumeEventHandling() throws IOException { |
| 119 | + final var cr = TestUtils.testCustomResource(); |
| 120 | + |
| 121 | + CustomResourceCache customResourceCache = new CustomResourceCache(); |
| 122 | + customResourceCache.cacheResource(cr); |
| 123 | + DefaultEventSourceManager defaultEventSourceManagerMock = |
| 124 | + mock(DefaultEventSourceManager.class); |
| 125 | + EventDispatcher eventDispatcherMock = mock(EventDispatcher.class); |
| 126 | + DefaultEventHandler local = new DefaultEventHandler(eventDispatcherMock, "Test", |
| 127 | + null); |
| 128 | + local.setEventSourceManager(defaultEventSourceManagerMock); |
| 129 | + when(defaultEventSourceManagerMock.getCache()).thenReturn(customResourceCache); |
| 130 | + doCallRealMethod().when(defaultEventSourceManagerMock).getLatestResource(any()); |
| 131 | + doCallRealMethod().when(defaultEventSourceManagerMock).getLatestResources(any()); |
| 132 | + doCallRealMethod().when(defaultEventSourceManagerMock).getLatestResourceUids(any()); |
| 133 | + doCallRealMethod().when(defaultEventSourceManagerMock).cacheResource(any(), any()); |
| 134 | + |
| 135 | + final var mock = mock(FilterWatchListMultiDeletable.class); |
| 136 | + when(mock.watch((ListOptions) any(), any())).thenReturn(mock(Watch.class)); |
| 137 | + when(client.inAnyNamespace()).thenReturn(mock); |
| 138 | + |
| 139 | + customResourceEventSource.setEventHandler(local); |
| 140 | + |
| 141 | + customResourceEventSource.eventReceived(Watcher.Action.MODIFIED, cr); |
| 142 | + verify(eventDispatcherMock, timeout(50).times(1)).handleExecution(any()); |
| 143 | + |
| 144 | + customResourceEventSource.close(); |
| 145 | + assertFalse(local.isRunning()); |
| 146 | + customResourceEventSource.eventReceived(Watcher.Action.MODIFIED, cr); |
| 147 | + // mockito times method is not reset and keeps increasing so here we stay at 1 call |
| 148 | + verify(eventDispatcherMock, timeout(50).times(1)).handleExecution(any()); |
| 149 | + |
| 150 | + customResourceEventSource.start(); |
| 151 | + assertTrue(local.isRunning()); |
| 152 | + customResourceEventSource.eventReceived(Watcher.Action.MODIFIED, cr); |
| 153 | + // we're expecting another call to the dispatcher, so total number of calls should now be 2 |
| 154 | + verify(eventDispatcherMock, timeout(50).times(2)).handleExecution(any()); |
| 155 | + } |
| 156 | + |
105 | 157 | private static class TestConfiguredController extends ConfiguredController<TestCustomResource> { |
106 | 158 |
|
107 | 159 | public TestConfiguredController(boolean generationAware) { |
|
0 commit comments