Skip to content

Commit 80c0e79

Browse files
committed
Use synchronization classes instead of Thread.sleep and fix flaky test mentoned in kubernetes-client#2436
1 parent 3c6fb49 commit 80c0e79

1 file changed

Lines changed: 50 additions & 10 deletions

File tree

util/src/test/java/io/kubernetes/client/informer/impl/DefaultSharedIndexInformerWireMockTest.java

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,21 @@ public void setup() throws IOException {
9393
public void testNamespacedPodInformerNormalBehavior() throws InterruptedException {
9494

9595
CoreV1Api coreV1Api = new CoreV1Api(client);
96-
96+
Semaphore getCount = new Semaphore(1);
97+
Semaphore watchCount = new Semaphore(2);
98+
Parameters getParams = new Parameters();
99+
Parameters watchParams = new Parameters();
100+
getParams.put("semaphore", getCount);
101+
watchParams.put("semaphore", watchCount);
97102
String startRV = "1000";
98103
String endRV = "1001";
99104

100105
V1PodList podList =
101106
new V1PodList().metadata(new V1ListMeta().resourceVersion(startRV)).items(Arrays.asList());
102107

103-
stubFor(
108+
wireMockRule.stubFor(
104109
get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods"))
110+
.withPostServeAction("semaphore", getParams)
105111
.withQueryParam("watch", equalTo("false"))
106112
.willReturn(
107113
aResponse()
@@ -114,8 +120,9 @@ public void testNamespacedPodInformerNormalBehavior() throws InterruptedExceptio
114120
new V1Pod()
115121
.metadata(
116122
new V1ObjectMeta().namespace(namespace).name(podName).resourceVersion(endRV)));
117-
stubFor(
123+
wireMockRule.stubFor(
118124
get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods"))
125+
.withPostServeAction("semaphore", watchParams)
119126
.withQueryParam("watch", equalTo("true"))
120127
.willReturn(
121128
aResponse()
@@ -167,9 +174,15 @@ public void onUpdate(V1Pod oldObj, V1Pod newObj) {}
167174
@Override
168175
public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) {}
169176
});
177+
178+
getCount.acquire(1);
179+
watchCount.acquire(2);
180+
170181
factory.startAllRegisteredInformers();
171182

172183
latch.await();
184+
getCount.acquire(1);
185+
watchCount.acquire(2);
173186

174187
assertEquals(true, foundExistingPod.get());
175188
assertEquals(endRV, podInformer.lastSyncResourceVersion());
@@ -190,15 +203,21 @@ public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) {}
190203
public void testAllNamespacedPodInformerNormalBehavior() throws InterruptedException {
191204

192205
CoreV1Api coreV1Api = new CoreV1Api(client);
193-
206+
Semaphore getCount = new Semaphore(1);
207+
Semaphore watchCount = new Semaphore(2);
208+
Parameters getParams = new Parameters();
209+
Parameters watchParams = new Parameters();
210+
getParams.put("semaphore", getCount);
211+
watchParams.put("semaphore", watchCount);
194212
String startRV = "1000";
195213
String endRV = "1001";
196214

197215
V1PodList podList =
198216
new V1PodList().metadata(new V1ListMeta().resourceVersion(startRV)).items(Arrays.asList());
199217

200-
stubFor(
218+
wireMockRule.stubFor(
201219
get(urlPathEqualTo("/api/v1/pods"))
220+
.withPostServeAction("semaphore", getParams)
202221
.withQueryParam("watch", equalTo("false"))
203222
.willReturn(
204223
aResponse()
@@ -218,8 +237,9 @@ public void testAllNamespacedPodInformerNormalBehavior() throws InterruptedExcep
218237
.labels(Collections.singletonMap("foo", "bar"))
219238
.annotations(Collections.singletonMap("foo", "bar"))));
220239

221-
stubFor(
240+
wireMockRule.stubFor(
222241
get(urlPathEqualTo("/api/v1/pods"))
242+
.withPostServeAction("semaphore", watchParams)
223243
.withQueryParam("watch", equalTo("true"))
224244
.willReturn(
225245
aResponse()
@@ -289,9 +309,15 @@ public void onUpdate(V1Pod oldObj, V1Pod newObj) {}
289309
@Override
290310
public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) {}
291311
});
312+
313+
getCount.acquire(1);
314+
watchCount.acquire(2);
315+
292316
factory.startAllRegisteredInformers();
293-
latch.await();
294317

318+
latch.await();
319+
getCount.acquire(1);
320+
watchCount.acquire(2);
295321
// can not set transform func if the informer has started
296322
try {
297323
podInformer.setTransform((obj) -> new V1Pod());
@@ -317,15 +343,21 @@ public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) {}
317343
public void testAllNamespacedPodInformerTransformFailure() throws InterruptedException {
318344

319345
CoreV1Api coreV1Api = new CoreV1Api(client);
320-
346+
Semaphore getCount = new Semaphore(1);
347+
Semaphore watchCount = new Semaphore(2);
348+
Parameters getParams = new Parameters();
349+
Parameters watchParams = new Parameters();
350+
getParams.put("semaphore", getCount);
351+
watchParams.put("semaphore", watchCount);
321352
String startRV = "1000";
322353
String endRV = "1001";
323354

324355
V1PodList podList =
325356
new V1PodList().metadata(new V1ListMeta().resourceVersion(startRV)).items(Arrays.asList());
326357

327-
stubFor(
358+
wireMockRule.stubFor(
328359
get(urlPathEqualTo("/api/v1/pods"))
360+
.withPostServeAction("semaphore", getParams)
329361
.withQueryParam("watch", equalTo("false"))
330362
.willReturn(
331363
aResponse()
@@ -340,8 +372,9 @@ public void testAllNamespacedPodInformerTransformFailure() throws InterruptedExc
340372
.metadata(
341373
new V1ObjectMeta().namespace(namespace).name(podName).resourceVersion(endRV)));
342374

343-
stubFor(
375+
wireMockRule.stubFor(
344376
get(urlPathEqualTo("/api/v1/pods"))
377+
.withPostServeAction("semaphore", watchParams)
345378
.withQueryParam("watch", equalTo("true"))
346379
.willReturn(
347380
aResponse()
@@ -397,8 +430,15 @@ public void onUpdate(V1Pod oldObj, V1Pod newObj) {}
397430
@Override
398431
public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) {}
399432
});
433+
434+
getCount.acquire(1);
435+
watchCount.acquire(2);
436+
400437
factory.startAllRegisteredInformers();
438+
401439
latch.await();
440+
getCount.acquire(1);
441+
watchCount.acquire(2);
402442

403443
// cannot find the pod due to transform failure
404444
assertFalse(foundExistingPod.get());

0 commit comments

Comments
 (0)