B6518816.java revision 14606:bc3775e25b52
1/* 2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 6518816 27 * @modules jdk.httpserver 28 * @run main/othervm B6518816 29 */ 30 31import java.net.*; 32import java.util.*; 33import java.io.*; 34import com.sun.net.httpserver.*; 35import java.util.concurrent.Executors; 36import java.util.concurrent.ExecutorService; 37 38public class B6518816 39{ 40 com.sun.net.httpserver.HttpServer httpServer; 41 ExecutorService executorService; 42 43 public static void main(String[] args) 44 { 45 new B6518816(); 46 } 47 48 public B6518816() 49 { 50 try { 51 startHttpServer(); 52 doClient(); 53 } catch (IOException ioe) { 54 System.err.println(ioe); 55 } 56 } 57 58 final static int MAX_CONNS = 10; 59 final static int TEN_MB = 10 * 1024 * 1024; 60 static boolean stopped = false; 61 62 /* 63 * we send approx 100MB and if more than 90MB is retained 64 * then the bug is still there 65 */ 66 void doClient() { 67 68 try { 69 InetSocketAddress address = httpServer.getAddress(); 70 List<HttpURLConnection> conns = new LinkedList<HttpURLConnection>(); 71 72 long totalmem1=0, totalmem2=0; 73 System.gc(); 74 Runtime runtime = Runtime.getRuntime(); 75 totalmem1 = runtime.totalMemory() - runtime.freeMemory(); 76 System.out.println ("Sending " + (TEN_MB*MAX_CONNS/1024) + " KB"); 77 System.out.println ("At start: " + totalmem1/1024 + " KB"); 78 79 byte [] buf = new byte [TEN_MB]; 80 81 for (int j=0; j<MAX_CONNS; j++) { 82 83 URL url = new URL("http://localhost:"+address.getPort()+"/test/"); 84 HttpURLConnection uc = (HttpURLConnection)url.openConnection(); 85 uc.setDoOutput (true); 86 OutputStream os = uc.getOutputStream (); 87 os.write (buf); 88 InputStream is = uc.getInputStream(); 89 int resp = uc.getResponseCode(); 90 if (resp != 200) { 91 throw new RuntimeException("Failed: Part 1, Response code is not 200"); 92 } 93 while (is.read() != -1) ; 94 is.close(); 95 96 conns.add (uc); 97 } 98 httpServer.stop(1); 99 httpServer = null; 100 executorService.shutdown(); 101 executorService = null; 102 stopped = true; 103 buf = null; 104 System.runFinalization(); 105 try {Thread.sleep (1000); } catch (InterruptedException e){} 106 System.gc(); 107 try {Thread.sleep (1000); } catch (InterruptedException e){} 108 System.gc(); 109 totalmem2 = runtime.totalMemory() - runtime.freeMemory(); 110 System.out.println ("At end: " + totalmem2/1024 + " KB"); 111 long diff = (totalmem2 - totalmem1) ;; 112 System.out.println ("Diff " + diff/1024 + " kbytes "); 113 if (diff > (TEN_MB*MAX_CONNS*0.9)) { 114 /* if >= 90 MB retained, then problem still there */ 115 throw new RuntimeException ("Excessive memory retained"); 116 } 117 118 } catch (IOException e) { 119 e.printStackTrace(); 120 throw new RuntimeException ("IOException"); 121 } finally { 122 if (!stopped) { 123 httpServer.stop(1); 124 executorService.shutdown(); 125 } 126 } 127 } 128 129 /** 130 * Http Server 131 */ 132 public void startHttpServer() throws IOException { 133 httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0); 134 135 // create HttpServer context for Part 1. 136 HttpContext ctx = httpServer.createContext("/test/", new MyHandler()); 137 138 executorService = Executors.newCachedThreadPool(); 139 httpServer.setExecutor(executorService); 140 httpServer.start(); 141 } 142 143 class MyHandler implements HttpHandler { 144 public void handle(HttpExchange t) throws IOException { 145 InputStream is = t.getRequestBody(); 146 Headers reqHeaders = t.getRequestHeaders(); 147 Headers resHeaders = t.getResponseHeaders(); 148 byte[] buf = new byte [16 * 1024]; 149 while (is.read (buf) != -1) ; 150 t.sendResponseHeaders(200, -1); 151 t.close(); 152 } 153 } 154 155} 156