test/jdk/java/net/URLConnection/RedirectLimit.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 54841 43439afaab4a
child 58679 9c3209ff7550
--- a/test/jdk/java/net/URLConnection/RedirectLimit.java	Thu Oct 17 20:27:44 2019 +0100
+++ b/test/jdk/java/net/URLConnection/RedirectLimit.java	Thu Oct 17 20:53:35 2019 +0100
@@ -35,11 +35,12 @@
 
 import java.io.*;
 import java.net.*;
+import java.util.concurrent.CountDownLatch;
 
 import jdk.test.lib.net.URIBuilder;
 
 class RedirLimitServer extends Thread {
-    static final int TIMEOUT = 10 * 1000;
+    static final int TIMEOUT = 20 * 1000;
     static final int NUM_REDIRECTS = 9;
 
     static final String reply1 = "HTTP/1.1 307 Temporary Redirect\r\n" +
@@ -59,6 +60,7 @@
 
     final ServerSocket ss;
     final int port;
+    final CountDownLatch readyToStart = new CountDownLatch(1);
 
     RedirLimitServer(ServerSocket ss) throws IOException {
         this.ss = ss;
@@ -71,7 +73,9 @@
     // Read until the end of a HTTP request
     void readOneRequest(InputStream is) throws IOException {
         int requestEndCount = 0, r;
+        StringBuilder sb = new StringBuilder();
         while ((r = is.read()) != -1) {
+            sb.append((char)r);
             if (r == requestEnd[requestEndCount]) {
                 requestEndCount++;
                 if (requestEndCount == 4) {
@@ -81,52 +85,57 @@
                 requestEndCount = 0;
             }
         }
+        System.out.println("Server got request: " + sb.toString());
     }
 
     public void run() {
         try {
+            readyToStart.countDown();
             for (int i=0; i<NUM_REDIRECTS; i++) {
                 try (Socket s = ss.accept()) {
+                    System.out.println("Server accepted socket: " + s);
                     s.setSoTimeout(TIMEOUT);
                     readOneRequest(s.getInputStream());
+                    System.out.println("Redirecting to: /redirect" + i);
                     String reply = reply1 + port + "/redirect" + i + reply2;
                     s.getOutputStream().write(reply.getBytes());
                 }
             }
             try (Socket s = ss.accept()) {
+                System.out.println("Server accepted socket: " + s);
                 s.setSoTimeout(TIMEOUT);
                 readOneRequest(s.getInputStream());
+                System.out.println("Replying...");
                 s.getOutputStream().write(reply3.getBytes());
             }
         } catch (Exception e) {
             e.printStackTrace();
-        } finally {
-            try { ss.close(); } catch (IOException unused) {}
         }
     }
 };
 
 public class RedirectLimit {
     public static void main(String[] args) throws Exception {
-        ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
-        int port = ss.getLocalPort();
-        RedirLimitServer server = new RedirLimitServer(ss);
-        server.start();
+        try (ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress())) {
+            int port = ss.getLocalPort();
+            RedirLimitServer server = new RedirLimitServer(ss);
+            server.start();
+            server.readyToStart.await();
+            URL url = URIBuilder.newBuilder()
+                    .scheme("http")
+                    .loopback()
+                    .port(port)
+                    .toURL();
+            URLConnection conURL = url.openConnection(Proxy.NO_PROXY);
 
-        URL url = URIBuilder.newBuilder()
-                .scheme("http")
-                .loopback()
-                .port(port)
-                .toURL();
-        URLConnection conURL =  url.openConnection();
+            conURL.setDoInput(true);
+            conURL.setAllowUserInteraction(false);
+            conURL.setUseCaches(false);
 
-        conURL.setDoInput(true);
-        conURL.setAllowUserInteraction(false);
-        conURL.setUseCaches(false);
-
-        try (InputStream in = conURL.getInputStream()) {
-            if ((in.read() != (int)'W') || (in.read()!=(int)'o')) {
-                throw new RuntimeException("Unexpected string read");
+            try (InputStream in = conURL.getInputStream()) {
+                if ((in.read() != (int) 'W') || (in.read() != (int) 'o')) {
+                    throw new RuntimeException("Unexpected string read");
+                }
             }
         }
     }