Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/KeepAliveCache/B5045306.java
38868 views
/*1* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 5045306 6356004 699349026* @library ../../httptest/27* @build HttpCallback TestHttpServer HttpTransaction28* @run main/othervm B504530629* @summary Http keep-alive implementation is not efficient30*/3132import java.net.*;33import java.io.*;34import java.lang.management.*;3536/* Part 1:37* The http client makes a connection to a URL whos content contains a lot of38* data, more than can fit in the socket buffer. The client only reads39* 1 byte of the data from the InputStream leaving behind more data than can40* fit in the socket buffer. The client then makes a second call to the http41* server. If the connection port used by the client is the same as for the42* first call then that means that the connection is being reused.43*44* Part 2:45* Test buggy webserver that sends less data than it specifies in its46* Content-length header.47*/4849public class B504530650{51static SimpleHttpTransaction httpTrans;52static TestHttpServer server;5354public static void main(String[] args) throws Exception {55startHttpServer();56clientHttpCalls();57}5859public static void startHttpServer() {60try {61httpTrans = new SimpleHttpTransaction();62server = new TestHttpServer(httpTrans, 1, 10, 0);63} catch (IOException e) {64e.printStackTrace();65}66}6768public static void clientHttpCalls() {69try {70System.out.println("http server listen on: " + server.getLocalPort());71String baseURLStr = "http://" + InetAddress.getLocalHost().getHostAddress() + ":" +72server.getLocalPort() + "/";7374URL bigDataURL = new URL (baseURLStr + "firstCall");75URL smallDataURL = new URL (baseURLStr + "secondCall");7677HttpURLConnection uc = (HttpURLConnection)bigDataURL.openConnection();7879//Only read 1 byte of response data and close the stream80InputStream is = uc.getInputStream();81byte[] ba = new byte[1];82is.read(ba);83is.close();8485// Allow the KeepAliveStreamCleaner thread to read the data left behind and cache the connection.86try { Thread.sleep(2000); } catch (Exception e) {}8788uc = (HttpURLConnection)smallDataURL.openConnection();89uc.getResponseCode();9091if (SimpleHttpTransaction.failed)92throw new RuntimeException("Failed: Initial Keep Alive Connection is not being reused");9394// Part 295URL part2Url = new URL (baseURLStr + "part2");96uc = (HttpURLConnection)part2Url.openConnection();97is = uc.getInputStream();98is.close();99100// Allow the KeepAliveStreamCleaner thread to try and read the data left behind and cache the connection.101try { Thread.sleep(2000); } catch (Exception e) {}102103ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();104if (threadMXBean.isThreadCpuTimeSupported()) {105long[] threads = threadMXBean.getAllThreadIds();106ThreadInfo[] threadInfo = threadMXBean.getThreadInfo(threads);107for (int i=0; i<threadInfo.length; i++) {108if (threadInfo[i].getThreadName().equals("Keep-Alive-SocketCleaner")) {109System.out.println("Found Keep-Alive-SocketCleaner thread");110long threadID = threadInfo[i].getThreadId();111long before = threadMXBean.getThreadCpuTime(threadID);112try { Thread.sleep(2000); } catch (Exception e) {}113long after = threadMXBean.getThreadCpuTime(threadID);114115if (before ==-1 || after == -1)116break; // thread has died, OK117118// if Keep-Alive-SocketCleaner consumes more than 50% of cpu then we119// can assume a recursive loop.120long total = after - before;121if (total >= 1000000000) // 1 second, or 1 billion nanoseconds122throw new RuntimeException("Failed: possible recursive loop in Keep-Alive-SocketCleaner");123}124}125}126127} catch (IOException e) {128e.printStackTrace();129} finally {130server.terminate();131}132}133}134135class SimpleHttpTransaction implements HttpCallback136{137static boolean failed = false;138139// Need to have enough data here that is too large for the socket buffer to hold.140// Also http.KeepAlive.remainingData must be greater than this value, default is 256K.141static final int RESPONSE_DATA_LENGTH = 128 * 1024;142143int port1;144145public void request(HttpTransaction trans) {146try {147String path = trans.getRequestURI().getPath();148if (path.equals("/firstCall")) {149port1 = trans.channel().socket().getPort();150System.out.println("First connection on client port = " + port1);151152byte[] responseBody = new byte[RESPONSE_DATA_LENGTH];153for (int i=0; i<responseBody.length; i++)154responseBody[i] = 0x41;155trans.setResponseEntityBody (responseBody, responseBody.length);156trans.sendResponse(200, "OK");157} else if (path.equals("/secondCall")) {158int port2 = trans.channel().socket().getPort();159System.out.println("Second connection on client port = " + port2);160161if (port1 != port2)162failed = true;163164trans.setResponseHeader ("Content-length", Integer.toString(0));165166/* Force the server to not respond for more that the timeout167* set by the keepalive cleaner (5000 millis). This ensures the168* timeout is correctly resets the default read timeout,169* infinity. See 6993490. */170System.out.println("server sleeping...");171try {Thread.sleep(6000); } catch (InterruptedException e) {}172173trans.sendResponse(200, "OK");174} else if(path.equals("/part2")) {175System.out.println("Call to /part2");176byte[] responseBody = new byte[RESPONSE_DATA_LENGTH];177for (int i=0; i<responseBody.length; i++)178responseBody[i] = 0x41;179trans.setResponseEntityBody (responseBody, responseBody.length);180181// override the Content-length header to be greater than the actual response body182trans.setResponseHeader("Content-length", Integer.toString(responseBody.length+1));183trans.sendResponse(200, "OK");184185// now close the socket186trans.channel().socket().close();187}188} catch (Exception e) {189e.printStackTrace();190}191}192}193194195