Skip to content

Commit dd81aa7

Browse files
committed
Unit-test resiliency
1 parent 23192b8 commit dd81aa7

File tree

9 files changed

+128
-111
lines changed

9 files changed

+128
-111
lines changed

extended/src/test/java/io/kubernetes/client/extended/kubectl/KubectlAnnotateTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package io.kubernetes.client.extended.kubectl;
1414

1515
import static com.github.tomakehurst.wiremock.client.WireMock.*;
16+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
1617
import static org.junit.Assert.assertNotNull;
1718
import static org.junit.Assert.assertThrows;
1819

@@ -32,13 +33,13 @@ public class KubectlAnnotateTest {
3233

3334
private ApiClient apiClient;
3435

35-
@Rule public WireMockRule wireMockRule = new WireMockRule(8384);
36+
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
3637

3738
@Before
3839
public void setup() throws IOException {
3940
ModelMapper.addModelMap("", "v1", "Pod", "pods", true, V1Pod.class);
4041
ModelMapper.addModelMap("", "v1", "Node", "nodes", false, V1Node.class);
41-
apiClient = new ClientBuilder().setBasePath("http://localhost:" + 8384).build();
42+
apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
4243
}
4344

4445
@Test

extended/src/test/java/io/kubernetes/client/extended/kubectl/KubectlApplyTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static com.github.tomakehurst.wiremock.client.WireMock.patch;
1818
import static com.github.tomakehurst.wiremock.client.WireMock.patchRequestedFor;
1919
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
20+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
2021
import static org.junit.Assert.assertNotNull;
2122

2223
import com.github.tomakehurst.wiremock.junit.WireMockRule;
@@ -59,11 +60,11 @@ public class KubectlApplyTest {
5960

6061
private ApiClient apiClient;
6162

62-
@Rule public WireMockRule wireMockRule = new WireMockRule(8384);
63+
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
6364

6465
@Before
6566
public void setup() throws IOException {
66-
apiClient = new ClientBuilder().setBasePath("http://localhost:" + 8384).build();
67+
apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
6768
}
6869

6970
@Test

extended/src/test/java/io/kubernetes/client/extended/kubectl/KubectlCreateTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package io.kubernetes.client.extended.kubectl;
1414

1515
import static com.github.tomakehurst.wiremock.client.WireMock.*;
16+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
1617
import static org.junit.Assert.assertNotNull;
1718

1819
import com.github.tomakehurst.wiremock.junit.WireMockRule;
@@ -51,11 +52,11 @@ public class KubectlCreateTest {
5152

5253
private ApiClient apiClient;
5354

54-
@Rule public WireMockRule wireMockRule = new WireMockRule(8384);
55+
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
5556

5657
@Before
5758
public void setup() throws IOException {
58-
apiClient = new ClientBuilder().setBasePath("http://localhost:" + 8384).build();
59+
apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
5960
}
6061

6162
@Test

extended/src/test/java/io/kubernetes/client/extended/kubectl/KubectlLabelTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.github.tomakehurst.wiremock.client.WireMock.put;
2121
import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor;
2222
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
23+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
2324
import static org.junit.Assert.assertNotNull;
2425
import static org.junit.Assert.assertThrows;
2526

@@ -39,13 +40,13 @@ public class KubectlLabelTest {
3940

4041
private ApiClient apiClient;
4142

42-
@Rule public WireMockRule wireMockRule = new WireMockRule(8384);
43+
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
4344

4445
@Before
4546
public void setup() throws IOException {
4647
ModelMapper.addModelMap("", "v1", "Pod", "pods", true, V1Pod.class);
4748
ModelMapper.addModelMap("", "v1", "Node", "nodes", false, V1Node.class);
48-
apiClient = new ClientBuilder().setBasePath("http://localhost:" + 8384).build();
49+
apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
4950
}
5051

5152
@Test

util/src/main/java/io/kubernetes/client/util/generic/GenericKubernetesApi.java

Lines changed: 55 additions & 49 deletions
Large diffs are not rendered by default.

util/src/test/java/io/kubernetes/client/util/generic/GenericKubernetesApiForCoreApiTest.java

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import java.io.IOException;
3636
import java.net.SocketTimeoutException;
37+
import java.util.concurrent.CancellationException;
3738
import java.util.concurrent.CountDownLatch;
3839
import java.util.concurrent.ExecutionException;
3940
import java.util.concurrent.Future;
@@ -47,6 +48,7 @@
4748
import static com.github.tomakehurst.wiremock.client.WireMock.delete;
4849
import static com.github.tomakehurst.wiremock.client.WireMock.deleteRequestedFor;
4950
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
51+
import static com.github.tomakehurst.wiremock.client.WireMock.exactly;
5052
import static com.github.tomakehurst.wiremock.client.WireMock.get;
5153
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
5254
import static com.github.tomakehurst.wiremock.client.WireMock.patch;
@@ -59,6 +61,7 @@
5961
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
6062
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
6163
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
64+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
6265
import static org.junit.Assert.assertEquals;
6366
import static org.junit.Assert.assertFalse;
6467
import static org.junit.Assert.assertNull;
@@ -68,14 +71,14 @@
6871

6972
public class GenericKubernetesApiForCoreApiTest {
7073

71-
@Rule public WireMockRule wireMockRule = new WireMockRule(8181);
74+
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
7275

73-
private JSON json = new JSON();
76+
private final JSON json = new JSON();
7477
private GenericKubernetesApi<V1Pod, V1PodList> podClient;
7578

7679
@Before
7780
public void setup() throws IOException {
78-
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + 8181).build();
81+
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
7982
apiClient.setHttpClient(apiClient.getHttpClient().newBuilder().addInterceptor(new TestInterceptor()).build());
8083
podClient =
8184
new GenericKubernetesApi<>(V1Pod.class, V1PodList.class, "", "v1", "pods", apiClient);
@@ -174,7 +177,7 @@ public void listClusterPodAsyncReturningObject() throws InterruptedException, Ex
174177
assertFalse(podListFuture.isDone());
175178
assertFalse(podListFuture.isCancelled());
176179

177-
assertThrows(TimeoutException.class, () -> podListFuture.get(1, TimeUnit.SECONDS));
180+
assertThrows(TimeoutException.class, () -> podListFuture.get(10, TimeUnit.MILLISECONDS));
178181

179182
waitForRequest.proceed();
180183

@@ -190,6 +193,42 @@ public void listClusterPodAsyncReturningObject() throws InterruptedException, Ex
190193
getRequestedFor(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("false")));
191194
}
192195

196+
@Test
197+
public void listClusterPodAsyncCanceled() {
198+
V1PodList podList = new V1PodList().kind("PodList").metadata(new V1ListMeta());
199+
200+
stubFor(
201+
get(urlPathEqualTo("/api/v1/pods"))
202+
.willReturn(aResponse().withStatus(200).withBody(json.serialize(podList))));
203+
TestCallback<V1PodList> callback = new TestCallback<>(podClient.getApiClient());
204+
205+
// request will be blocked until proceed()
206+
WaitForRequest waitForRequest = callback.awaitBeforeRequest();
207+
208+
Future<KubernetesApiResponse<V1PodList>> podListFuture = podClient.listAsync(callback);
209+
210+
assertFalse(podListFuture.isDone());
211+
assertFalse(podListFuture.isCancelled());
212+
213+
// cancel request
214+
assertTrue(podListFuture.cancel(true));
215+
216+
assertTrue(podListFuture.isCancelled());
217+
assertTrue(podListFuture.isDone());
218+
219+
// unblock thread to clean up
220+
waitForRequest.proceed();
221+
222+
assertThrows(CancellationException.class, podListFuture::get);
223+
assertThrows(CancellationException.class, () -> podListFuture.get(10, TimeUnit.MILLISECONDS));
224+
225+
assertFalse(callback.hasBeenCalled());
226+
227+
verify(
228+
exactly(0),
229+
getRequestedFor(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("false")));
230+
}
231+
193232
@Test
194233
public void createNamespacedPodReturningObject() {
195234
V1Pod foo1 =
@@ -257,7 +296,7 @@ public void patchNamespacedPodWithApiPrefix() {
257296
"",
258297
"v1",
259298
"pods",
260-
new ClientBuilder().setBasePath("http://localhost:" + 8181 + prefix).build());
299+
new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port() + prefix).build());
261300
KubernetesApiResponse<V1Pod> podPatchResp =
262301
rancherPodClient.patch(
263302
"default", "foo1", V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, v1Patch);
@@ -270,7 +309,7 @@ public void patchNamespacedPodWithApiPrefix() {
270309

271310
@Test
272311
public void testReadTimeoutShouldThrowException() {
273-
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + 8181).build();
312+
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
274313
apiClient.setHttpClient(
275314
apiClient
276315
.getHttpClient()
@@ -299,11 +338,6 @@ static class TestCallback<ApiType extends KubernetesType>
299338
final ApiClient apiClient;
300339

301340
WaitForRequest waitForRequest = null;
302-
WaitForResponse waitForResponse = null;
303-
304-
TestCallback() {
305-
this(null);
306-
}
307341

308342
TestCallback(ApiClient apiClient) {
309343
this.apiClient = apiClient;
@@ -320,26 +354,21 @@ public KubernetesApiResponse<ApiType> waitForAndGetResponse() throws Interrupted
320354
return result.get();
321355
}
322356

357+
public boolean hasBeenCalled() {
358+
return latch.getCount() == 0;
359+
}
360+
323361
public WaitForRequest awaitBeforeRequest() {
324362
waitForRequest = new WaitForRequest();
325363
return waitForRequest;
326364
}
327365

328-
public WaitForResponse awaitBeforeResponse() {
329-
waitForResponse = new WaitForResponse();
330-
return waitForResponse;
331-
}
332-
333366
@Override
334367
public Call apply(Call call) {
335368
if (waitForRequest != null) {
336369
call = apiClient.getHttpClient().newCall(
337370
call.request().newBuilder().tag(WaitForRequest.class, waitForRequest).build());
338371
}
339-
if (waitForResponse != null) {
340-
call = apiClient.getHttpClient().newCall(
341-
call.request().newBuilder().tag(WaitForResponse.class, waitForResponse).build());
342-
}
343372
return call;
344373
}
345374
}
@@ -351,9 +380,6 @@ static class TestInterceptor implements Interceptor {
351380
public Response intercept(@NotNull Chain chain) throws IOException {
352381
final Request request = chain.request();
353382

354-
// TEST
355-
System.out.println("Before proceed");
356-
357383
WaitForRequest waitForRequest = request.tag(WaitForRequest.class);
358384
if (waitForRequest != null) {
359385
try {
@@ -363,25 +389,11 @@ public Response intercept(@NotNull Chain chain) throws IOException {
363389
}
364390
}
365391

366-
final Response response = chain.proceed(request);
367-
368-
// TEST
369-
System.out.println("After proceed");
370-
371-
WaitForResponse waitForResponse = request.tag(WaitForResponse.class);
372-
if (waitForResponse != null) {
373-
try {
374-
waitForResponse.await();
375-
} catch (InterruptedException e) {
376-
throw new IOException(e);
377-
}
378-
}
379-
380-
return response;
392+
return chain.proceed(request);
381393
}
382394
}
383395

384-
static class WaitSignal {
396+
static class WaitForRequest {
385397
final CountDownLatch latch = new CountDownLatch(1);
386398

387399
public void await() throws InterruptedException {
@@ -392,12 +404,4 @@ public void proceed() {
392404
latch.countDown();
393405
}
394406
}
395-
396-
static class WaitForRequest extends WaitSignal {
397-
398-
}
399-
400-
static class WaitForResponse extends WaitSignal {
401-
402-
}
403407
}

util/src/test/java/io/kubernetes/client/util/generic/GenericKubernetesApiTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package io.kubernetes.client.util.generic;
1414

1515
import static com.github.tomakehurst.wiremock.client.WireMock.*;
16+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
1617
import static org.junit.Assert.*;
1718

1819
import com.github.tomakehurst.wiremock.junit.WireMockRule;
@@ -38,14 +39,14 @@
3839

3940
public class GenericKubernetesApiTest {
4041

41-
@Rule public WireMockRule wireMockRule = new WireMockRule(8181);
42+
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
4243

4344
private JSON json = new JSON();
4445
private GenericKubernetesApi<V1Job, V1JobList> jobClient;
4546

4647
@Before
4748
public void setup() throws IOException {
48-
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + 8181).build();
49+
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
4950
jobClient =
5051
new GenericKubernetesApi<>(V1Job.class, V1JobList.class, "batch", "v1", "jobs", apiClient);
5152
}
@@ -205,7 +206,7 @@ public void watchNamespacedJobReturningObject() throws ApiException {
205206

206207
@Test
207208
public void testReadTimeoutShouldThrowException() {
208-
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + 8181).build();
209+
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
209210
apiClient.setHttpClient(
210211
apiClient
211212
.getHttpClient()

util/src/test/java/io/kubernetes/client/util/generic/GenericKubernetesGetApiTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package io.kubernetes.client.util.generic;
1414

1515
import static com.github.tomakehurst.wiremock.client.WireMock.*;
16+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
1617
import static org.junit.Assert.assertEquals;
1718
import static org.junit.Assert.assertNull;
1819

@@ -29,7 +30,7 @@
2930

3031
public class GenericKubernetesGetApiTest {
3132

32-
@Rule public WireMockRule wireMockRule = new WireMockRule(8181);
33+
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
3334

3435
private JSON json = new JSON();
3536
private GenericKubernetesApi<V1Job, V1JobList> jobClient; // non-core built-in resource
@@ -38,7 +39,7 @@ public class GenericKubernetesGetApiTest {
3839

3940
@Before
4041
public void setup() {
41-
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + 8181).build();
42+
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
4243
jobClient =
4344
new GenericKubernetesApi<>(V1Job.class, V1JobList.class, "batch", "v1", "jobs", apiClient);
4445
fooClient =

util/src/test/java/io/kubernetes/client/util/generic/KubernetesApiResponseTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import static com.github.tomakehurst.wiremock.client.WireMock.delete;
1717
import static com.github.tomakehurst.wiremock.client.WireMock.get;
1818
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
19+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
1920
import static org.junit.Assert.*;
2021

2122
import com.github.tomakehurst.wiremock.junit.WireMockRule;
@@ -33,13 +34,13 @@
3334
import org.junit.Test;
3435

3536
public class KubernetesApiResponseTest {
36-
@Rule public WireMockRule wireMockRule = new WireMockRule(8485);
37+
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
3738

3839
private GenericKubernetesApi<V1Pod, V1PodList> podClient;
3940

4041
@Before
4142
public void setup() throws IOException {
42-
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + 8485).build();
43+
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
4344
podClient =
4445
new GenericKubernetesApi<>(V1Pod.class, V1PodList.class, "", "v1", "pods", apiClient);
4546
}

0 commit comments

Comments
 (0)