Skip to content

Commit bc75c42

Browse files
committed
unit test
1 parent a3f9719 commit bc75c42

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/EventProcessorTest.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.javaoperatorsdk.operator.api.config.RetryConfiguration;
1919
import io.javaoperatorsdk.operator.api.monitoring.Metrics;
2020
import io.javaoperatorsdk.operator.processing.event.rate.PeriodRateLimiter;
21+
import io.javaoperatorsdk.operator.processing.event.rate.RateLimiter;
2122
import io.javaoperatorsdk.operator.processing.event.source.controller.ControllerResourceEventSource;
2223
import io.javaoperatorsdk.operator.processing.event.source.controller.ResourceAction;
2324
import io.javaoperatorsdk.operator.processing.event.source.controller.ResourceEvent;
@@ -50,21 +51,23 @@ class EventProcessorTest {
5051
private Metrics metricsMock = mock(Metrics.class);
5152
private EventProcessor eventProcessor;
5253
private EventProcessor eventProcessorWithRetry;
54+
private RateLimiter rateLimiterMock = mock(RateLimiter.class);
5355

5456
@BeforeEach
5557
void setup() {
5658
when(eventSourceManagerMock.getControllerResourceEventSource())
5759
.thenReturn(controllerResourceEventSourceMock);
5860
eventProcessor =
5961
spy(new EventProcessor(reconciliationDispatcherMock, eventSourceManagerMock, "Test", null,
60-
new PeriodRateLimiter(), null));
62+
rateLimiterMock, null));
6163
eventProcessor.start();
6264
eventProcessorWithRetry =
6365
spy(new EventProcessor(reconciliationDispatcherMock, eventSourceManagerMock, "Test",
64-
GenericRetry.defaultLimitedExponentialRetry(), new PeriodRateLimiter(), null));
66+
GenericRetry.defaultLimitedExponentialRetry(), rateLimiterMock, null));
6567
eventProcessorWithRetry.start();
6668
when(eventProcessor.retryEventSource()).thenReturn(retryTimerEventSourceMock);
6769
when(eventProcessorWithRetry.retryEventSource()).thenReturn(retryTimerEventSourceMock);
70+
when(rateLimiterMock.acquirePermission(any())).thenReturn(Optional.empty());
6871
}
6972

7073
@Test
@@ -331,6 +334,25 @@ void newResourceAfterMissedDeleteEvent() {
331334
verify(reconciliationDispatcherMock, timeout(50).times(1)).handleExecution(any());
332335
}
333336

337+
@Test
338+
void rateLimitsReconciliationSubmission() throws InterruptedException {
339+
// the refresh period value does not matter here
340+
var refreshPeriod = Duration.ofMillis(100);
341+
var event = prepareCREvent();
342+
343+
when(rateLimiterMock.acquirePermission(event.getRelatedCustomResourceID()))
344+
.thenReturn(Optional.empty())
345+
.thenReturn(Optional.of(refreshPeriod));
346+
347+
eventProcessor.handleEvent(event);
348+
verify(reconciliationDispatcherMock, after(FAKE_CONTROLLER_EXECUTION_DURATION).times(1))
349+
.handleExecution(any());
350+
verify(retryTimerEventSourceMock, times(0)).scheduleOnce(any(), anyLong());
351+
352+
eventProcessor.handleEvent(event);
353+
verify(retryTimerEventSourceMock, times(1)).scheduleOnce(any(), anyLong());
354+
}
355+
334356
private ResourceID eventAlreadyUnderProcessing() {
335357
when(reconciliationDispatcherMock.handleExecution(any()))
336358
.then(

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ void setUpResources() {
5252
configurationService = spy(ConfigurationService.class);
5353
when(configurationService.checkCRDAndValidateLocalModel()).thenReturn(false);
5454
when(configurationService.getVersion()).thenReturn(new Version("1", "1", new Date()));
55+
// make sure not the same config instance is used for the controller, so rate limiter is not
56+
// shared
5557
when(configurationService.getConfigurationFor(any(MyController.class)))
58+
.thenReturn(new MyConfiguration())
5659
.thenReturn(new MyConfiguration());
5760
}
5861

@@ -138,8 +141,7 @@ public MyConfiguration() {
138141
}
139142
}
140143

141-
@ControllerConfiguration(namespaces = NAMESPACE,
142-
rateLimiter = @RateLimiter(limitForPeriod = PeriodRateLimiter.DEFAULT_LIMIT_FOR_PERIOD))
144+
@ControllerConfiguration(namespaces = NAMESPACE)
143145
public static class MyController implements Reconciler<TestCustomResource> {
144146

145147
private final Consumer<TestCustomResource> consumer;

0 commit comments

Comments
 (0)