< prev index next >

test/jdk/java/net/httpclient/SplitResponse.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -19,42 +19,53 @@
  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
-
 import java.io.IOException;
+import java.net.URI;
+import java.util.concurrent.CompletableFuture;
+import javax.net.ssl.SSLContext;
+import javax.net.ServerSocketFactory;
+import javax.net.ssl.SSLServerSocketFactory;
 import jdk.incubator.http.HttpClient;
+import jdk.incubator.http.HttpClient.Version;
 import jdk.incubator.http.HttpRequest;
 import jdk.incubator.http.HttpResponse;
-import java.net.URI;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.Executor;
-import java.util.concurrent.ExecutorService;
+import jdk.testlibrary.SimpleSSLContext;
+import static java.lang.System.out;
+import static java.lang.String.format;
 import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
 
 /**
  * @test
  * @bug 8087112
- * @key intermittent
- * @build Server
- * @run main/othervm -Djava.net.HttpClient.log=all SplitResponse
+ * @library /lib/testlibrary
+ * @build jdk.testlibrary.SimpleSSLContext
+ * @build MockServer
+ * @run main/othervm -Djdk.internal.httpclient.debug=true -Djdk.httpclient.HttpClient.log=all SplitResponse
  */
 
 /**
  * Similar test to QuickResponses except that each byte of the response
  * is sent in a separate packet, which tests the stability of the implementation
- * for receiving unusual packet sizes.
+ * for receiving unusual packet sizes. Additionally, tests scenarios there
+ * connections that are retrieved from the connection pool may reach EOF before
+ * being reused.
  */
 public class SplitResponse {
 
-    static Server server;
-
-    static String response(String body) {
-        return "HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-length: "
-                + Integer.toString(body.length())
-                + "\r\n\r\n" + body;
+    static String response(String body, boolean serverKeepalive) {
+        StringBuilder sb = new StringBuilder();
+        sb.append("HTTP/1.1 200 OK\r\n");
+        if (!serverKeepalive)
+            sb.append("Connection: Close\r\n");
+
+        sb.append("Content-length: ").append(body.length()).append("\r\n");
+        sb.append("\r\n");
+        sb.append(body);
+        return sb.toString();
     }
 
     static final String responses[] = {
         "Lorem ipsum",
         "dolor sit amet",

@@ -66,61 +77,125 @@
         "Duis aute irure dolor in reprehenderit in voluptate velit esse" +
         "cillum dolore eu fugiat nulla pariatur.",
         "Excepteur sint occaecat cupidatat non proident."
     };
 
+    final ServerSocketFactory factory;
+    final SSLContext context;
+    final boolean useSSL;
+    SplitResponse(boolean useSSL) throws IOException {
+        this.useSSL = useSSL;
+        context = new SimpleSSLContext().get();
+        SSLContext.setDefault(context);
+        factory = useSSL ? SSLServerSocketFactory.getDefault()
+                         : ServerSocketFactory.getDefault();
+    }
+
+    public HttpClient newHttpClient() {
+        HttpClient client;
+        if (useSSL) {
+            client = HttpClient.newBuilder()
+                               .sslContext(context)
+                               .build();
+        } else {
+            client = HttpClient.newHttpClient();
+        }
+        return client;
+    }
+
     public static void main(String[] args) throws Exception {
-        server = new Server(0);
+        boolean useSSL = false;
+        if (args != null && args.length == 1) {
+            useSSL = "SSL".equals(args[0]);
+        }
+        SplitResponse sp = new SplitResponse(useSSL);
+
+        for (Version version : Version.values()) {
+            for (boolean serverKeepalive : new boolean[]{ true, false }) {
+                // Note: the mock server doesn't support Keep-Alive, but
+                // pretending that it might exercises code paths in and out of
+                // the connection pool, and retry logic
+                for (boolean async : new boolean[]{ true, false }) {
+                    sp.test(version, serverKeepalive, async);
+                }
+            }
+        }
+    }
+
+    // @Test
+    void test(Version version, boolean serverKeepalive, boolean async)
+        throws Exception
+    {
+        out.println(format("*** version %s, serverKeepAlive: %s, async: %s ***",
+                           version, serverKeepalive, async));
+        MockServer server = new MockServer(0, factory);
         URI uri = new URI(server.getURL());
+        out.println("server is: " + uri);
         server.start();
 
-        HttpClient client = HttpClient.newHttpClient();
-        HttpRequest request = HttpRequest.newBuilder(uri).build();
+        HttpClient client = newHttpClient();
+        HttpRequest request = HttpRequest.newBuilder(uri).version(version).build();
         HttpResponse<String> r;
         CompletableFuture<HttpResponse<String>> cf1;
 
         try {
             for (int i=0; i<responses.length; i++) {
-                cf1 = client.sendAsync(request, asString());
+                out.println("----- iteration " + i + " -----");
                 String body = responses[i];
+                Thread t = sendSplitResponse(response(body, serverKeepalive), server);
 
-                Server.Connection c = server.activity();
-                sendSplitResponse(response(body), c);
+                if (async) {
+                    out.println("send async: " + request);
+                    cf1 = client.sendAsync(request, asString());
                 r = cf1.get();
-                if (r.statusCode()!= 200)
+                } else { // sync
+                    out.println("send sync: " + request);
+                    r = client.send(request, asString());
+                }
+
+                if (r.statusCode() != 200)
                     throw new RuntimeException("Failed");
 
                 String rxbody = r.body();
-                System.out.println("received " + rxbody);
+                out.println("received " + rxbody);
                 if (!rxbody.equals(body))
-                    throw new RuntimeException("Failed");
-                c.close();
+                    throw new RuntimeException(format("Expected:%s, got:%s", body, rxbody));
+
+                t.join();
+                conn.close();
             }
         } finally {
-            Executor def = client.executor();
-            if (def instanceof ExecutorService) {
-                ((ExecutorService)def).shutdownNow();
-            }
+            server.close();
         }
         System.out.println("OK");
     }
 
-    // send the response one byte at a time with a small delay between bytes
-    // to ensure that each byte is read in a separate read
-    static void sendSplitResponse(String s, Server.Connection conn) {
+    // required for cleanup
+    volatile MockServer.Connection conn;
+
+    // Sends the response, mostly, one byte at a time with a small delay
+    // between bytes, to encourage that each byte is read in a separate read
+    Thread sendSplitResponse(String s, MockServer server) {
         System.out.println("Sending: ");
         Thread t = new Thread(() -> {
+            System.out.println("Waiting for server to receive headers");
+            conn = server.activity();
+            System.out.println("Start sending response");
+
             try {
                 int len = s.length();
+                out.println("sending " + s);
                 for (int i = 0; i < len; i++) {
                     String onechar = s.substring(i, i + 1);
                     conn.send(onechar);
-                    Thread.sleep(30);
+                    Thread.sleep(10);
                 }
-                System.out.println("sent");
+                out.println("sent " + s);
             } catch (IOException | InterruptedException e) {
+                throw new RuntimeException(e);
             }
         });
         t.setDaemon(true);
         t.start();
+        return t;
     }
 }
< prev index next >