Skip to content

Commit 5210dfe

Browse files
committed
Switch to Countdown latch and actually use it for test.
1 parent a3ff699 commit 5210dfe

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

util/src/test/java/io/kubernetes/client/ExecTest.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
import static org.mockito.Mockito.times;
2626
import static org.mockito.Mockito.verify;
2727

28+
import com.github.tomakehurst.wiremock.core.Admin;
2829
import com.github.tomakehurst.wiremock.extension.Parameters;
30+
import com.github.tomakehurst.wiremock.extension.PostServeAction;
2931
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
32+
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
3033
import io.kubernetes.client.Exec.ExecProcess;
3134
import io.kubernetes.client.openapi.ApiClient;
3235
import io.kubernetes.client.openapi.ApiException;
@@ -41,7 +44,6 @@
4144
import java.io.OutputStream;
4245
import java.nio.charset.StandardCharsets;
4346
import java.util.concurrent.CountDownLatch;
44-
import java.util.concurrent.Semaphore;
4547
import java.util.function.Consumer;
4648
import org.junit.jupiter.api.BeforeEach;
4749
import org.junit.jupiter.api.Test;
@@ -50,6 +52,19 @@
5052
/** Tests for the Exec helper class */
5153
class ExecTest {
5254

55+
public static class CountDownLatchAction extends PostServeAction {
56+
@Override
57+
public String getName() {
58+
return "countdown";
59+
}
60+
61+
@Override
62+
public void doAction(ServeEvent serveEvent, Admin admin, Parameters parameters) {
63+
CountDownLatch latch = (CountDownLatch) parameters.get("latch");
64+
latch.countDown();
65+
}
66+
}
67+
5368
private static final String OUTPUT_EXIT0 = "{\"metadata\":{},\"status\":\"Success\"}";
5469
private static final String OUTPUT_EXIT1 =
5570
"{\"metadata\":{},\"status\":\"Failure\",\"message\":\"command terminated with non-zero exit code: Error executing in Docker Container: 1\",\"reason\":\"NonZeroExitCode\",\"details\":{\"causes\":[{\"reason\":\"ExitCode\",\"message\":\"1\"}]}}";
@@ -68,7 +83,9 @@ class ExecTest {
6883

6984
@RegisterExtension
7085
static WireMockExtension apiServer =
71-
WireMockExtension.newInstance().options(options().dynamicPort()).build();
86+
WireMockExtension.newInstance()
87+
.options(options().dynamicPort().extensions(new CountDownLatchAction()))
88+
.build();
7289

7390
@BeforeEach
7491
void setup() {
@@ -145,15 +162,10 @@ void terminalResize() throws IOException, InterruptedException {
145162
final ExecProcess process = new ExecProcess(client);
146163
ByteArrayOutputStream bos = new ByteArrayOutputStream();
147164

148-
System.out.println("Injecting output stream");
149165
process.getHandler().injectOutputStream(4, bos);
150-
System.out.println("Resizing output stream");
151166
process.resize(100, 100);
152-
System.out.println("Resizing output stream");
153167
process.destroy();
154168

155-
System.out.println("Going to tests.");
156-
157169
String out = bos.toString("UTF-8");
158170
assertThat(out).isEqualTo("{ \"width\": 100, \"height\": 100 }\n");
159171
}
@@ -196,13 +208,13 @@ void url() throws IOException, ApiException, InterruptedException {
196208

197209
V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name(podName).namespace(namespace));
198210

199-
Semaphore getCount = new Semaphore(2);
200-
Parameters getParams = new Parameters();
201-
getParams.put("semaphore", getCount);
211+
CountDownLatch latch = new CountDownLatch(2);
212+
Parameters params = new Parameters();
213+
params.put("latch", latch);
202214

203215
apiServer.stubFor(
204216
get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec"))
205-
.withPostServeAction("semaphore", getParams)
217+
.withPostServeAction("countdown", params)
206218
.willReturn(
207219
aResponse()
208220
.withStatus(404)
@@ -218,11 +230,7 @@ void url() throws IOException, ApiException, InterruptedException {
218230
.setStderr(false)
219231
.execute()
220232
.waitFor();
221-
222-
// These will be released for each web call above.
223-
// There is a race between the above waitFor() and the request actually being recorded
224-
// by WireMock. This fixes it.
225-
getCount.acquire(2);
233+
latch.await();
226234

227235
apiServer.verify(
228236
getRequestedFor(

0 commit comments

Comments
 (0)