Path: blob/master/test/jdk/sun/net/www/protocol/http/B6518816.java
41159 views
/*1* Copyright (c) 2007, 2019, 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 651881626* @library /test/lib27* @modules jdk.httpserver28* @run main/othervm B651881629*/3031import java.net.*;32import java.util.*;33import java.io.*;34import com.sun.net.httpserver.*;35import java.util.concurrent.Executors;36import java.util.concurrent.ExecutorService;3738import jdk.test.lib.net.URIBuilder;3940public class B651881641{42com.sun.net.httpserver.HttpServer httpServer;43ExecutorService executorService;4445public static void main(String[] args)46{47new B6518816();48}4950public B6518816()51{52try {53startHttpServer();54doClient();55} catch (IOException ioe) {56System.err.println(ioe);57}58}5960final static int MAX_CONNS = 10;61final static int TEN_MB = 10 * 1024 * 1024;62static boolean stopped = false;6364/*65* we send approx 100MB and if more than 90MB is retained66* then the bug is still there67*/68void doClient() {6970try {71InetSocketAddress address = httpServer.getAddress();72List<HttpURLConnection> conns = new LinkedList<HttpURLConnection>();7374long totalmem1=0, totalmem2=0;75System.gc();76Runtime runtime = Runtime.getRuntime();77totalmem1 = runtime.totalMemory() - runtime.freeMemory();78System.out.println ("Sending " + (TEN_MB*MAX_CONNS/1024) + " KB");79System.out.println ("At start: " + totalmem1/1024 + " KB");8081byte [] buf = new byte [TEN_MB];8283for (int j=0; j<MAX_CONNS; j++) {84URL url = URIBuilder.newBuilder()85.scheme("http")86.loopback()87.port(address.getPort())88.path("/test/")89.toURLUnchecked();90HttpURLConnection uc = (HttpURLConnection)url.openConnection();91uc.setDoOutput (true);92OutputStream os = uc.getOutputStream ();93os.write (buf);94InputStream is = uc.getInputStream();95int resp = uc.getResponseCode();96if (resp != 200) {97throw new RuntimeException("Failed: Part 1, Response code is not 200");98}99while (is.read() != -1) ;100is.close();101102conns.add (uc);103}104httpServer.stop(1);105httpServer = null;106executorService.shutdown();107executorService = null;108stopped = true;109buf = null;110System.runFinalization();111try {Thread.sleep (1000); } catch (InterruptedException e){}112System.gc();113try {Thread.sleep (1000); } catch (InterruptedException e){}114System.gc();115totalmem2 = runtime.totalMemory() - runtime.freeMemory();116System.out.println ("At end: " + totalmem2/1024 + " KB");117long diff = (totalmem2 - totalmem1) ;;118System.out.println ("Diff " + diff/1024 + " kbytes ");119if (diff > (TEN_MB*MAX_CONNS*0.9)) {120/* if >= 90 MB retained, then problem still there */121throw new RuntimeException ("Excessive memory retained");122}123124} catch (IOException e) {125e.printStackTrace();126throw new RuntimeException ("IOException");127} finally {128if (!stopped) {129httpServer.stop(1);130executorService.shutdown();131}132}133}134135/**136* Http Server137*/138public void startHttpServer() throws IOException {139httpServer = com.sun.net.httpserver.HttpServer.create(140new InetSocketAddress(InetAddress.getLoopbackAddress(), 0),1410);142143// create HttpServer context for Part 1.144HttpContext ctx = httpServer.createContext("/test/", new MyHandler());145146executorService = Executors.newCachedThreadPool();147httpServer.setExecutor(executorService);148httpServer.start();149}150151class MyHandler implements HttpHandler {152public void handle(HttpExchange t) throws IOException {153InputStream is = t.getRequestBody();154Headers reqHeaders = t.getRequestHeaders();155Headers resHeaders = t.getResponseHeaders();156byte[] buf = new byte [16 * 1024];157while (is.read (buf) != -1) ;158t.sendResponseHeaders(200, -1);159t.close();160}161}162163}164165166