|
20 | 20 |
|
21 | 21 | import io.fabric8.kubernetes.client.KubernetesClient; |
22 | 22 | import io.javaoperatorsdk.operator.MockKubernetesClient; |
| 23 | +import io.javaoperatorsdk.operator.OperatorException; |
23 | 24 | import io.javaoperatorsdk.operator.api.config.BaseConfigurationService; |
24 | 25 | import io.javaoperatorsdk.operator.sample.simple.TestCustomResource; |
25 | 26 |
|
26 | 27 | import static org.assertj.core.api.Assertions.assertThat; |
| 28 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
27 | 29 | import static org.mockito.Mockito.never; |
28 | 30 | import static org.mockito.Mockito.times; |
29 | 31 | import static org.mockito.Mockito.verify; |
@@ -70,13 +72,33 @@ void createsSeparateInformerForDifferentEventSourceNames() { |
70 | 72 | } |
71 | 73 |
|
72 | 74 | @Test |
73 | | - void createsNewInformerEvenForAnIdenticalKey() { |
| 75 | + void throwsWhenRequestingAnInformerForAnAlreadyRegisteredKey() { |
74 | 76 | var classifier = classifier("default"); |
75 | 77 |
|
76 | 78 | pool.getInformer(CONTROLLER, ES_NAME, classifier, client); |
| 79 | + |
| 80 | + // requesting the same controller+event source+classifier combination again without releasing |
| 81 | + // first would otherwise silently overwrite the map entry and leak the earlier informer |
| 82 | + assertThatThrownBy(() -> pool.getInformer(CONTROLLER, ES_NAME, classifier, client)) |
| 83 | + .isInstanceOf(OperatorException.class) |
| 84 | + .hasMessageContaining(CONTROLLER) |
| 85 | + .hasMessageContaining(ES_NAME) |
| 86 | + .hasMessageContaining(classifier.toString()); |
| 87 | + |
| 88 | + // the earlier informer is left untouched, still registered exactly once |
| 89 | + assertThat(pool.size()).isEqualTo(1); |
| 90 | + verify(client, times(1)).resources(TestCustomResource.class); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void allowsReRequestingAnInformerAfterItWasReleased() { |
| 95 | + var classifier = classifier("default"); |
| 96 | + pool.getInformer(CONTROLLER, ES_NAME, classifier, client); |
| 97 | + pool.releaseInformer(CONTROLLER, ES_NAME, classifier); |
| 98 | + |
| 99 | + // must not throw: releasing frees up the key for reuse |
77 | 100 | pool.getInformer(CONTROLLER, ES_NAME, classifier, client); |
78 | 101 |
|
79 | | - // the same key overwrites the map entry, but a fresh informer is created on every call |
80 | 102 | assertThat(pool.size()).isEqualTo(1); |
81 | 103 | verify(client, times(2)).resources(TestCustomResource.class); |
82 | 104 | } |
|
0 commit comments