Skip to content

Commit df9a56b

Browse files
committed
Deflake another test.
1 parent c05b99e commit df9a56b

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

spring/src/test/java/io/kubernetes/client/spring/extended/controller/KubernetesInformerCreatorTest.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@
1919
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
2020
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
2121
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
22+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
2223
import static org.junit.Assert.assertEquals;
2324
import static org.junit.Assert.assertNotNull;
2425

26+
import com.github.tomakehurst.wiremock.core.Admin;
27+
import com.github.tomakehurst.wiremock.extension.Parameters;
28+
import com.github.tomakehurst.wiremock.extension.PostServeAction;
2529
import com.github.tomakehurst.wiremock.junit.WireMockRule;
30+
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
31+
2632
import com.google.gson.Gson;
2733
import io.kubernetes.client.informer.SharedIndexInformer;
2834
import io.kubernetes.client.informer.SharedInformerFactory;
@@ -37,7 +43,8 @@
3743
import io.kubernetes.client.util.ClientBuilder;
3844
import io.kubernetes.client.util.generic.GenericKubernetesApi;
3945
import java.util.Arrays;
40-
import org.junit.Rule;
46+
import java.util.concurrent.Semaphore;
47+
import org.junit.ClassRule;
4148
import org.junit.Test;
4249
import org.junit.runner.RunWith;
4350
import org.springframework.beans.factory.annotation.Autowired;
@@ -50,14 +57,29 @@
5057
@SpringBootTest
5158
public class KubernetesInformerCreatorTest {
5259

53-
@Rule public WireMockRule wireMockRule = new WireMockRule(8188);
60+
public static class CountRequestAction extends PostServeAction {
61+
@Override
62+
public String getName() {
63+
return "semaphore";
64+
}
65+
66+
@Override
67+
public void doAction(ServeEvent serveEvent, Admin admin, Parameters parameters) {
68+
Semaphore count = (Semaphore) parameters.get("semaphore");
69+
count.release();
70+
}
71+
}
72+
73+
74+
75+
@ClassRule public static WireMockRule wireMockRule = new WireMockRule(options().dynamicPort().extensions(new CountRequestAction()));
5476

5577
@SpringBootApplication
5678
static class App {
5779

5880
@Bean
5981
public ApiClient testingApiClient() {
60-
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + 8188).build();
82+
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
6183
return apiClient;
6284
}
6385

@@ -91,6 +113,13 @@ public void testInformerInjection() throws InterruptedException {
91113
assertNotNull(podInformer);
92114
assertNotNull(configMapInformer);
93115

116+
Semaphore getCount = new Semaphore(2);
117+
Semaphore watchCount = new Semaphore(2);
118+
Parameters getParams = new Parameters();
119+
Parameters watchParams = new Parameters();
120+
getParams.put("semaphore", getCount);
121+
watchParams.put("semaphore", watchCount);
122+
94123
V1Pod foo1 =
95124
new V1Pod().kind("Pod").metadata(new V1ObjectMeta().namespace("default").name("foo1"));
96125
V1ConfigMap bar1 =
@@ -100,6 +129,7 @@ public void testInformerInjection() throws InterruptedException {
100129

101130
wireMockRule.stubFor(
102131
get(urlMatching("^/api/v1/pods.*"))
132+
.withPostServeAction("semaphore", getParams)
103133
.withQueryParam("watch", equalTo("false"))
104134
.willReturn(
105135
aResponse()
@@ -112,11 +142,13 @@ public void testInformerInjection() throws InterruptedException {
112142
.items(Arrays.asList(foo1))))));
113143
wireMockRule.stubFor(
114144
get(urlMatching("^/api/v1/pods.*"))
145+
.withPostServeAction("semaphore", watchParams)
115146
.withQueryParam("watch", equalTo("true"))
116147
.willReturn(aResponse().withStatus(200).withBody("{}")));
117148

118149
wireMockRule.stubFor(
119150
get(urlMatching("^/api/v1/namespaces/default/configmaps.*"))
151+
.withPostServeAction("semaphore", getParams)
120152
.withQueryParam("watch", equalTo("false"))
121153
.willReturn(
122154
aResponse()
@@ -129,12 +161,19 @@ public void testInformerInjection() throws InterruptedException {
129161
.items(Arrays.asList(bar1))))));
130162
wireMockRule.stubFor(
131163
get(urlMatching("^/api/v1/namespaces/default/configmaps.*"))
164+
.withPostServeAction("semaphore", watchParams)
132165
.withQueryParam("watch", equalTo("true"))
133166
.willReturn(aResponse().withStatus(200).withBody("{}")));
134167

168+
// These will be released for each web call above.
169+
getCount.acquire(2);
170+
watchCount.acquire(2);
171+
135172
informerFactory.startAllRegisteredInformers();
136173

137-
Thread.sleep(200);
174+
// Wait for the GETs to complete and the watches to start.
175+
getCount.acquire(2);
176+
watchCount.acquire(2);
138177

139178
verify(
140179
1,

0 commit comments

Comments
 (0)