Skip to content

Commit f735dd2

Browse files
authored
Refactor jetty client test to use nginx in docker as http backend (#276)
1 parent 80e0868 commit f735dd2

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

tests/src/org.eclipse.jetty/jetty-client/11.0.12/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ gradlew.bat
22
gradlew
33
gradle/
44
build/
5+
nginx-stderr.txt
6+
nginx-stdout.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nginx:1-alpine-slim

tests/src/org.eclipse.jetty/jetty-client/11.0.12/src/test/java/org_eclipse_jetty/jetty_client/JettyClientTest.java

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,92 @@
88

99
import org.eclipse.jetty.client.HttpClient;
1010
import org.eclipse.jetty.client.api.ContentResponse;
11+
import org.junit.jupiter.api.AfterAll;
12+
import org.junit.jupiter.api.BeforeAll;
1113
import org.junit.jupiter.api.Test;
1214

15+
import java.io.File;
16+
import java.io.IOException;
17+
import java.net.InetSocketAddress;
18+
import java.net.Socket;
19+
import java.net.SocketTimeoutException;
20+
1321
import static org.assertj.core.api.Assertions.assertThat;
1422

1523
class JettyClientTest {
1624

17-
// This test communicates with external service so it should be disabled until it is refactored. See related issue: https://github.com/oracle/graalvm-reachability-metadata/issues/259
18-
private static final boolean DISABLE_TEST = true;
25+
private static final int HOST_PORT = 12345;
26+
27+
private static Process process;
28+
29+
@BeforeAll
30+
static void beforeAll() throws IOException {
31+
System.out.println("Starting nginx ...");
32+
process = new ProcessBuilder(
33+
"docker", "run", "--rm", "-p", HOST_PORT + ":80",
34+
"nginx:1-alpine-slim")
35+
.redirectOutput(new File("nginx-stdout.txt"))
36+
.redirectError(new File("nginx-stderr.txt"))
37+
.start();
38+
39+
// Wait until connection can be established
40+
waitUntilContainerStarted(60);
41+
42+
System.out.println("nginx started");
43+
}
44+
45+
private static void waitUntilContainerStarted(int startupTimeoutSeconds) {
46+
System.out.println("Waiting for nginx container to become available");
47+
48+
Exception lastConnectionException = null;
49+
50+
long end = System.currentTimeMillis() + startupTimeoutSeconds * 1000L;
51+
while (System.currentTimeMillis() < end) {
52+
try {
53+
Thread.sleep(100L);
54+
} catch (InterruptedException e) {
55+
// continue
56+
}
57+
try (Socket socket = new Socket()) {
58+
socket.setSoTimeout(100);
59+
socket.connect(new InetSocketAddress("localhost", HOST_PORT), 100);
60+
if (!check(socket)) {
61+
continue;
62+
}
63+
return;
64+
} catch (Exception e) {
65+
lastConnectionException = e;
66+
}
67+
}
68+
throw new IllegalStateException("nginx container cannot be accessed on localhost:" + HOST_PORT, lastConnectionException);
69+
}
70+
71+
private static boolean check(Socket socket) throws IOException {
72+
try {
73+
// -1 indicates the socket has been closed immediately
74+
// Other responses or a timeout are considered as success
75+
if (socket.getInputStream().read() == -1) {
76+
return false;
77+
}
78+
} catch (SocketTimeoutException ex) {
79+
}
80+
return true;
81+
}
82+
83+
@AfterAll
84+
static void tearDown() {
85+
if (process != null && process.isAlive()) {
86+
System.out.println("Shutting down nginx");
87+
process.destroy();
88+
}
89+
}
90+
1991
@Test
2092
void test() throws Exception {
21-
if (DISABLE_TEST) {
22-
return;
23-
}
2493
HttpClient client = new HttpClient();
2594
client.start();
2695
try {
27-
ContentResponse response = client.GET("https://httpbin.org/get");
96+
ContentResponse response = client.GET("http://localhost:%d/".formatted(HOST_PORT));
2897
assertThat(response.getStatus()).isEqualTo(200);
2998
} finally {
3099
client.stop();

0 commit comments

Comments
 (0)