Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/B6518816.java
38867 views
/*1* Copyright (c) 2007, 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* @run main/othervm B651881627*/2829import java.net.*;30import java.util.*;31import java.io.*;32import com.sun.net.httpserver.*;33import java.util.concurrent.Executors;34import java.util.concurrent.ExecutorService;3536public class B651881637{38com.sun.net.httpserver.HttpServer httpServer;39ExecutorService executorService;4041public static void main(String[] args)42{43new B6518816();44}4546public B6518816()47{48try {49startHttpServer();50doClient();51} catch (IOException ioe) {52System.err.println(ioe);53}54}5556final static int MAX_CONNS = 10;57final static int TEN_MB = 10 * 1024 * 1024;58static boolean stopped = false;5960/*61* we send approx 100MB and if more than 90MB is retained62* then the bug is still there63*/64void doClient() {6566try {67InetSocketAddress address = httpServer.getAddress();68List<HttpURLConnection> conns = new LinkedList<HttpURLConnection>();6970long totalmem1=0, totalmem2=0;71System.gc();72Runtime runtime = Runtime.getRuntime();73totalmem1 = runtime.totalMemory() - runtime.freeMemory();74System.out.println ("Sending " + (TEN_MB*MAX_CONNS/1024) + " KB");75System.out.println ("At start: " + totalmem1/1024 + " KB");7677byte [] buf = new byte [TEN_MB];7879for (int j=0; j<MAX_CONNS; j++) {8081URL url = new URL("http://localhost:"+address.getPort()+"/test/");82HttpURLConnection uc = (HttpURLConnection)url.openConnection();83uc.setDoOutput (true);84OutputStream os = uc.getOutputStream ();85os.write (buf);86InputStream is = uc.getInputStream();87int resp = uc.getResponseCode();88if (resp != 200) {89throw new RuntimeException("Failed: Part 1, Response code is not 200");90}91while (is.read() != -1) ;92is.close();9394conns.add (uc);95}96httpServer.stop(1);97httpServer = null;98executorService.shutdown();99executorService = null;100stopped = true;101buf = null;102System.runFinalization();103try {Thread.sleep (1000); } catch (InterruptedException e){}104System.gc();105try {Thread.sleep (1000); } catch (InterruptedException e){}106System.gc();107totalmem2 = runtime.totalMemory() - runtime.freeMemory();108System.out.println ("At end: " + totalmem2/1024 + " KB");109long diff = (totalmem2 - totalmem1) ;;110System.out.println ("Diff " + diff/1024 + " kbytes ");111if (diff > (TEN_MB*MAX_CONNS*0.9)) {112/* if >= 90 MB retained, then problem still there */113throw new RuntimeException ("Excessive memory retained");114}115116} catch (IOException e) {117e.printStackTrace();118throw new RuntimeException ("IOException");119} finally {120if (!stopped) {121httpServer.stop(1);122executorService.shutdown();123}124}125}126127/**128* Http Server129*/130public void startHttpServer() throws IOException {131httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);132133// create HttpServer context for Part 1.134HttpContext ctx = httpServer.createContext("/test/", new MyHandler());135136executorService = Executors.newCachedThreadPool();137httpServer.setExecutor(executorService);138httpServer.start();139}140141class MyHandler implements HttpHandler {142public void handle(HttpExchange t) throws IOException {143InputStream is = t.getRequestBody();144Headers reqHeaders = t.getRequestHeaders();145Headers resHeaders = t.getResponseHeaders();146byte[] buf = new byte [16 * 1024];147while (is.read (buf) != -1) ;148t.sendResponseHeaders(200, -1);149t.close();150}151}152153}154155156