|
8 | 8 |
|
9 | 9 | import org.eclipse.jetty.client.HttpClient; |
10 | 10 | import org.eclipse.jetty.client.api.ContentResponse; |
| 11 | +import org.junit.jupiter.api.AfterAll; |
| 12 | +import org.junit.jupiter.api.BeforeAll; |
11 | 13 | import org.junit.jupiter.api.Test; |
12 | 14 |
|
| 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 | + |
13 | 21 | import static org.assertj.core.api.Assertions.assertThat; |
14 | 22 |
|
15 | 23 | class JettyClientTest { |
16 | 24 |
|
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 | + |
19 | 91 | @Test |
20 | 92 | void test() throws Exception { |
21 | | - if (DISABLE_TEST) { |
22 | | - return; |
23 | | - } |
24 | 93 | HttpClient client = new HttpClient(); |
25 | 94 | client.start(); |
26 | 95 | try { |
27 | | - ContentResponse response = client.GET("https://httpbin.org/get"); |
| 96 | + ContentResponse response = client.GET("http://localhost:%d/".formatted(HOST_PORT)); |
28 | 97 | assertThat(response.getStatus()).isEqualTo(200); |
29 | 98 | } finally { |
30 | 99 | client.stop(); |
|
0 commit comments