Skip to content

Commit ef81c22

Browse files
committed
fixed unit test
1 parent 25da840 commit ef81c22

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ExecutorServiceManager.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private ExecutorServiceManager(ExecutorService executor, ExecutorService workflo
2929

3030
}
3131

32-
public static void init() {
32+
public static synchronized void init() {
3333
if (instance == null) {
3434
final var configuration = ConfigurationServiceProvider.instance();
3535
final var executorService = configuration.getExecutorService();
@@ -45,6 +45,13 @@ public static void init() {
4545
}
4646
}
4747

48+
/** For testing purposes only */
49+
public static synchronized void reset() {
50+
instance().doStop(Duration.ZERO);
51+
instance = null;
52+
init();
53+
}
54+
4855
public static synchronized void stop(Duration gracefulShutdownTimeout) {
4956
if (instance != null) {
5057
instance.doStop(gracefulShutdownTimeout);

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import org.slf4j.LoggerFactory;
1616

1717
import io.fabric8.kubernetes.api.model.HasMetadata;
18+
import io.javaoperatorsdk.operator.api.config.ConfigurationServiceProvider;
1819
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
20+
import io.javaoperatorsdk.operator.api.config.ExecutorServiceManager;
1921
import io.javaoperatorsdk.operator.api.config.RetryConfiguration;
2022
import io.javaoperatorsdk.operator.api.monitoring.Metrics;
2123
import io.javaoperatorsdk.operator.processing.event.rate.LinearRateLimiter;
@@ -36,17 +38,7 @@
3638
import static org.awaitility.Awaitility.await;
3739
import static org.mockito.ArgumentMatchers.eq;
3840
import static org.mockito.ArgumentMatchers.isNull;
39-
import static org.mockito.Mockito.after;
40-
import static org.mockito.Mockito.any;
41-
import static org.mockito.Mockito.anyLong;
42-
import static org.mockito.Mockito.doAnswer;
43-
import static org.mockito.Mockito.mock;
44-
import static org.mockito.Mockito.never;
45-
import static org.mockito.Mockito.spy;
46-
import static org.mockito.Mockito.timeout;
47-
import static org.mockito.Mockito.times;
48-
import static org.mockito.Mockito.verify;
49-
import static org.mockito.Mockito.when;
41+
import static org.mockito.Mockito.*;
5042

5143
@SuppressWarnings({"rawtypes", "unchecked"})
5244
class EventProcessorTest {
@@ -56,6 +48,8 @@ class EventProcessorTest {
5648
public static final int FAKE_CONTROLLER_EXECUTION_DURATION = 250;
5749
public static final int SEPARATE_EXECUTION_TIMEOUT = 450;
5850
public static final String TEST_NAMESPACE = "default-event-handler-test";
51+
public static final int TIME_TO_WAIT_AFTER_SUBMISSION_BEFORE_EXECUTION = 150;
52+
public static final int DISPATCHING_DELAY = 250;
5953

6054
private final ReconciliationDispatcher reconciliationDispatcherMock =
6155
mock(ReconciliationDispatcher.class);
@@ -418,14 +412,24 @@ void schedulesRetryForMarReconciliationIntervalIfRetryExhausted() {
418412
}
419413

420414
@Test
421-
void executionOfReconciliationNotStartIfProcessorStopped() {
415+
void executionOfReconciliationNotStartIfProcessorStopped() throws InterruptedException {
416+
when(reconciliationDispatcherMock.handleExecution(any()))
417+
.then((Answer<PostExecutionControl>) invocationOnMock -> {
418+
Thread.sleep(DISPATCHING_DELAY);
419+
return PostExecutionControl.defaultDispatch();
420+
});
421+
// one event will lock the thread / executor
422+
ConfigurationServiceProvider.overrideCurrent(o -> o.withConcurrentReconciliationThreads(1));
423+
ExecutorServiceManager.reset();
424+
eventProcessor.start();
425+
426+
eventProcessor.handleEvent(prepareCREvent());
422427
eventProcessor.handleEvent(prepareCREvent());
423-
// note that there could be race condition in this test, however it is very unlikely that it
424-
// will happen the stop is called after submission (that could be theoretically executed before
425-
// stop)
426428
eventProcessor.stop();
427429

428-
verify(reconciliationDispatcherMock, timeout(SEPARATE_EXECUTION_TIMEOUT).times(0))
430+
// wait until both event should be handled
431+
Thread.sleep(TIME_TO_WAIT_AFTER_SUBMISSION_BEFORE_EXECUTION + 2 * DISPATCHING_DELAY);
432+
verify(reconciliationDispatcherMock, atMostOnce())
429433
.handleExecution(any());
430434
}
431435

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ static void classSetup() {
7575
* equals will fail on the two equal but NOT identical TestCustomResources because equals is not
7676
* implemented on TestCustomResourceSpec or TestCustomResourceStatus
7777
*/
78+
ConfigurationServiceProvider.reset();
7879
ConfigurationServiceProvider.overrideCurrent(overrider -> overrider
7980
.checkingCRDAndValidateLocalModel(false).withResourceCloner(new Cloner() {
8081
@Override

0 commit comments

Comments
 (0)