Skip to content

Commit 442b0cb

Browse files
test: reset ExternalIDGenServiceMock between tests to fix flaky external-state ITs (#3387)
Signed-off-by: Dennis-Mircea Ciupitu <dennis.mircea.ciupitu@gmail.com>
1 parent c86eeec commit 442b0cb

4 files changed

Lines changed: 47 additions & 0 deletions

File tree

operator-framework/src/test/java/io/javaoperatorsdk/operator/dependent/externalstate/ExternalStateTestBase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616
package io.javaoperatorsdk.operator.dependent.externalstate;
1717

1818
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.extension.ExtendWith;
1920

2021
import io.fabric8.kubernetes.api.model.ConfigMap;
2122
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
2223
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;
2324
import io.javaoperatorsdk.operator.support.ExternalIDGenServiceMock;
25+
import io.javaoperatorsdk.operator.support.ExternalServiceResetExtension;
2426

2527
import static io.javaoperatorsdk.operator.dependent.externalstate.ExternalStateReconciler.ID_KEY;
2628
import static org.assertj.core.api.Assertions.assertThat;
2729
import static org.awaitility.Awaitility.await;
2830

31+
@ExtendWith(ExternalServiceResetExtension.class)
2932
public abstract class ExternalStateTestBase {
3033

3134
private static final String TEST_RESOURCE_NAME = "test1";

operator-framework/src/test/java/io/javaoperatorsdk/operator/dependent/externalstate/externalstatebulkdependent/ExternalStateBulkIT.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
import java.time.Duration;
1919

2020
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.extension.ExtendWith;
2122
import org.junit.jupiter.api.extension.RegisterExtension;
2223

2324
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
2425
import io.javaoperatorsdk.annotation.Sample;
2526
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;
2627
import io.javaoperatorsdk.operator.support.ExternalIDGenServiceMock;
28+
import io.javaoperatorsdk.operator.support.ExternalServiceResetExtension;
2729

2830
import static org.assertj.core.api.Assertions.assertThat;
2931
import static org.awaitility.Awaitility.await;
@@ -37,6 +39,7 @@
3739
allowing operators to track and reconcile a variable number of external resources with \
3840
persistent state that survives operator restarts.
3941
""")
42+
@ExtendWith(ExternalServiceResetExtension.class)
4043
class ExternalStateBulkIT {
4144

4245
private static final String TEST_RESOURCE_NAME = "test1";

operator-framework/src/test/java/io/javaoperatorsdk/operator/support/ExternalIDGenServiceMock.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public List<ExternalResource> listResources() {
5050
return new ArrayList<>(resourceMap.values());
5151
}
5252

53+
/**
54+
* Clears the internal state. Intended to be called between tests so that state from one test does
55+
* not leak into the next via this JVM-lifetime singleton.
56+
*/
57+
public void reset() {
58+
resourceMap.clear();
59+
}
60+
5361
public static ExternalIDGenServiceMock getInstance() {
5462
return serviceMock;
5563
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright Java Operator SDK Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.javaoperatorsdk.operator.support;
17+
18+
import org.jspecify.annotations.NonNull;
19+
import org.junit.jupiter.api.extension.BeforeEachCallback;
20+
import org.junit.jupiter.api.extension.ExtensionContext;
21+
22+
/**
23+
* Resets the {@link ExternalIDGenServiceMock} singleton before each test method so that state from
24+
* one test does not leak into the next. Apply via
25+
* {@code @ExtendWith(ExternalServiceResetExtension.class)}.
26+
*/
27+
public class ExternalServiceResetExtension implements BeforeEachCallback {
28+
29+
@Override
30+
public void beforeEach(@NonNull ExtensionContext context) {
31+
ExternalIDGenServiceMock.getInstance().reset();
32+
}
33+
}

0 commit comments

Comments
 (0)