< prev index next >

test/jdk/java/net/httpclient/http2/BasicTest.java

Print this page

        

*** 24,77 **** /* * @test * @bug 8087112 * @library /lib/testlibrary server * @build jdk.testlibrary.SimpleSSLContext ! * @modules jdk.incubator.httpclient/jdk.incubator.http.internal.common * jdk.incubator.httpclient/jdk.incubator.http.internal.frame * jdk.incubator.httpclient/jdk.incubator.http.internal.hpack * @run testng/othervm -Djdk.httpclient.HttpClient.log=ssl,requests,responses,errors BasicTest */ import java.net.*; import jdk.incubator.http.*; import static jdk.incubator.http.HttpClient.Version.HTTP_2; import javax.net.ssl.*; import java.nio.file.*; import java.util.concurrent.*; import jdk.testlibrary.SimpleSSLContext; ! import static jdk.incubator.http.HttpRequest.BodyProcessor.fromFile; ! import static jdk.incubator.http.HttpRequest.BodyProcessor.fromString; import static jdk.incubator.http.HttpResponse.BodyHandler.asFile; import static jdk.incubator.http.HttpResponse.BodyHandler.asString; import org.testng.annotations.Test; @Test public class BasicTest { static int httpPort, httpsPort; static Http2TestServer httpServer, httpsServer; static HttpClient client = null; ! static ExecutorService exec; static SSLContext sslContext; ! static String httpURIString, httpsURIString; static void initialize() throws Exception { try { SimpleSSLContext sslct = new SimpleSSLContext(); sslContext = sslct.get(); client = getClient(); ! httpServer = new Http2TestServer(false, 0, exec, sslContext); httpServer.addHandler(new Http2EchoHandler(), "/"); httpPort = httpServer.getAddress().getPort(); ! httpsServer = new Http2TestServer(true, 0, exec, sslContext); httpsServer.addHandler(new Http2EchoHandler(), "/"); httpsPort = httpsServer.getAddress().getPort(); httpURIString = "http://127.0.0.1:" + httpPort + "/foo/"; httpsURIString = "https://127.0.0.1:" + httpsPort + "/bar/"; httpServer.start(); httpsServer.start(); } catch (Throwable e) { --- 24,86 ---- /* * @test * @bug 8087112 * @library /lib/testlibrary server * @build jdk.testlibrary.SimpleSSLContext ! * @modules java.base/sun.net.www.http ! * jdk.incubator.httpclient/jdk.incubator.http.internal.common * jdk.incubator.httpclient/jdk.incubator.http.internal.frame * jdk.incubator.httpclient/jdk.incubator.http.internal.hpack * @run testng/othervm -Djdk.httpclient.HttpClient.log=ssl,requests,responses,errors BasicTest */ + import java.io.IOException; import java.net.*; import jdk.incubator.http.*; import static jdk.incubator.http.HttpClient.Version.HTTP_2; import javax.net.ssl.*; import java.nio.file.*; import java.util.concurrent.*; + import java.util.concurrent.atomic.AtomicReference; + import java.util.Collections; + import java.util.LinkedList; + import java.util.List; import jdk.testlibrary.SimpleSSLContext; ! import static jdk.incubator.http.HttpRequest.BodyPublisher.fromFile; ! import static jdk.incubator.http.HttpRequest.BodyPublisher.fromString; import static jdk.incubator.http.HttpResponse.BodyHandler.asFile; import static jdk.incubator.http.HttpResponse.BodyHandler.asString; import org.testng.annotations.Test; @Test public class BasicTest { static int httpPort, httpsPort; static Http2TestServer httpServer, httpsServer; static HttpClient client = null; ! static ExecutorService clientExec; ! static ExecutorService serverExec; static SSLContext sslContext; ! static String pingURIString, httpURIString, httpsURIString; static void initialize() throws Exception { try { SimpleSSLContext sslct = new SimpleSSLContext(); sslContext = sslct.get(); client = getClient(); ! httpServer = new Http2TestServer(false, 0, serverExec, sslContext); httpServer.addHandler(new Http2EchoHandler(), "/"); + httpServer.addHandler(new EchoWithPingHandler(), "/ping"); httpPort = httpServer.getAddress().getPort(); ! httpsServer = new Http2TestServer(true, 0, serverExec, sslContext); httpsServer.addHandler(new Http2EchoHandler(), "/"); httpsPort = httpsServer.getAddress().getPort(); httpURIString = "http://127.0.0.1:" + httpPort + "/foo/"; + pingURIString = "http://127.0.0.1:" + httpPort + "/ping/"; httpsURIString = "https://127.0.0.1:" + httpsPort + "/bar/"; httpServer.start(); httpsServer.start(); } catch (Throwable e) {
*** 79,126 **** e.printStackTrace(); throw e; } } ! @Test(timeOut=3000000) public static void test() throws Exception { try { initialize(); ! simpleTest(false); ! simpleTest(true); streamTest(false); streamTest(true); paramsTest(); ! Thread.sleep(1000 * 4); } catch (Throwable tt) { System.err.println("tt caught"); tt.printStackTrace(); throw tt; } finally { httpServer.stop(); httpsServer.stop(); ! exec.shutdownNow(); } } static HttpClient getClient() { if (client == null) { ! exec = Executors.newCachedThreadPool(); client = HttpClient.newBuilder() ! .executor(exec) .sslContext(sslContext) .version(HTTP_2) .build(); } return client; } static URI getURI(boolean secure) { if (secure) return URI.create(httpsURIString); else ! return URI.create(httpURIString); } static void checkStatus(int expected, int found) throws Exception { if (expected != found) { System.err.printf ("Test failed: wrong status code %d/%d\n", --- 88,170 ---- e.printStackTrace(); throw e; } } ! static List<CompletableFuture<Long>> cfs = Collections ! .synchronizedList( new LinkedList<>()); ! ! static CompletableFuture<Long> currentCF; ! ! static class EchoWithPingHandler extends Http2EchoHandler { ! private final Object lock = new Object(); ! ! @Override ! public void handle(Http2TestExchange exchange) throws IOException { ! // for now only one ping active at a time. don't want to saturate ! synchronized(lock) { ! CompletableFuture<Long> cf = currentCF; ! if (cf == null || cf.isDone()) { ! cf = exchange.sendPing(); ! assert cf != null; ! cfs.add(cf); ! currentCF = cf; ! } ! } ! super.handle(exchange); ! } ! } ! ! @Test public static void test() throws Exception { try { initialize(); ! simpleTest(false, false); ! simpleTest(false, true); ! simpleTest(true, false); streamTest(false); streamTest(true); paramsTest(); ! CompletableFuture.allOf(cfs.toArray(new CompletableFuture[0])).join(); ! synchronized (cfs) { ! for (CompletableFuture<Long> cf : cfs) { ! System.out.printf("Ping ack received in %d millisec\n", cf.get()); ! } ! } } catch (Throwable tt) { System.err.println("tt caught"); tt.printStackTrace(); throw tt; } finally { httpServer.stop(); httpsServer.stop(); ! //clientExec.shutdown(); } } static HttpClient getClient() { if (client == null) { ! serverExec = Executors.newCachedThreadPool(); ! clientExec = Executors.newCachedThreadPool(); client = HttpClient.newBuilder() ! .executor(clientExec) .sslContext(sslContext) .version(HTTP_2) .build(); } return client; } static URI getURI(boolean secure) { + return getURI(secure, false); + } + + static URI getURI(boolean secure, boolean ping) { if (secure) return URI.create(httpsURIString); else ! return URI.create(ping ? pingURIString: httpURIString); } static void checkStatus(int expected, int found) throws Exception { if (expected != found) { System.err.printf ("Test failed: wrong status code %d/%d\n",
*** 168,182 **** throw new RuntimeException(); return resp.body(); }); response.join(); compareFiles(src, dest); ! System.err.println("DONE"); } static void paramsTest() throws Exception { ! Http2TestServer server = new Http2TestServer(true, 0, exec, sslContext); server.addHandler((t -> { SSLSession s = t.getSSLSession(); String prot = s.getProtocol(); if (prot.equals("TLSv1.2")) { t.sendResponseHeaders(200, -1); --- 212,226 ---- throw new RuntimeException(); return resp.body(); }); response.join(); compareFiles(src, dest); ! System.err.println("streamTest: DONE"); } static void paramsTest() throws Exception { ! Http2TestServer server = new Http2TestServer(true, 0, serverExec, sslContext); server.addHandler((t -> { SSLSession s = t.getSSLSession(); String prot = s.getProtocol(); if (prot.equals("TLSv1.2")) { t.sendResponseHeaders(200, -1);
*** 194,207 **** int stat = resp.statusCode(); if (stat != 200) { throw new RuntimeException("paramsTest failed " + Integer.toString(stat)); } } ! static void simpleTest(boolean secure) throws Exception { ! URI uri = getURI(secure); System.err.println("Request to " + uri); // Do a simple warmup request HttpClient client = getClient(); --- 238,252 ---- int stat = resp.statusCode(); if (stat != 200) { throw new RuntimeException("paramsTest failed " + Integer.toString(stat)); } + System.err.println("paramsTest: DONE"); } ! static void simpleTest(boolean secure, boolean ping) throws Exception { ! URI uri = getURI(secure, ping); System.err.println("Request to " + uri); // Do a simple warmup request HttpClient client = getClient();
*** 235,242 **** return compareFiles(resp.body(), source); }); Thread.sleep(100); } CompletableFuture.allOf(responses).join(); ! System.err.println("DONE"); } } --- 280,287 ---- return compareFiles(resp.body(), source); }); Thread.sleep(100); } CompletableFuture.allOf(responses).join(); ! System.err.println("simpleTest: DONE"); } }
< prev index next >