Path: blob/master/test/jdk/sun/net/www/http/KeepAliveCache/KeepAliveTimerThread.java
41154 views
/*1* Copyright (c) 2002, 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* @library /test/lib26* @bug 470129927* @summary Keep-Alive-Timer thread management in KeepAliveCache causes memory leak28* @run main KeepAliveTimerThread29* @run main/othervm -Djava.net.preferIPv6Addresses=true KeepAliveTimerThread30*/3132import java.net.*;33import java.io.*;34import jdk.test.lib.net.URIBuilder;35import static java.net.Proxy.NO_PROXY;3637public class KeepAliveTimerThread {38static class Fetcher implements Runnable {39URL url;4041Fetcher(URL url) {42this.url = url;43}4445public void run() {46try {47InputStream in = url.openConnection(NO_PROXY).getInputStream();48byte b[] = new byte[128];49int n;50do {51n = in.read(b);52} while (n > 0);53in.close();54} catch (Exception x) {55x.printStackTrace();56}57}58}5960static class Server extends Thread {61ServerSocket server;62Server (ServerSocket server) {63super ();64this.server = server;65}66void readAll (Socket s) throws IOException {67byte[] buf = new byte [128];68InputStream is = s.getInputStream ();69s.setSoTimeout(1000);70try {71while (is.read(buf) > 0) ;72} catch (SocketTimeoutException x) { }73}74/*75* Our "http" server to return a 40476*/77public void run() {78try {79Socket s = server.accept();80readAll(s);8182PrintStream out = new PrintStream(83new BufferedOutputStream(84s.getOutputStream() ));8586/* send the header */87out.print("HTTP/1.1 200 OK\r\n");88out.print("Content-Type: text/html; charset=iso-8859-1\r\n");89out.print("Content-Length: 78\r\n");90out.print("\r\n");91out.print("<HTML>");92out.print("<HEAD><TITLE>File Content</TITLE></HEAD>");93out.print("<BODY>A dummy body.</BODY>");94out.print("</HTML>");95out.flush();9697s.close();98} catch (Exception e) {99e.printStackTrace();100} finally {101try { server.close(); } catch (IOException unused) {}102}103}104}105106107public static void main(String args[]) throws Exception {108InetAddress loopback = InetAddress.getLoopbackAddress();109ServerSocket ss = new ServerSocket();110ss.bind(new InetSocketAddress(loopback, 0));111Server s = new Server(ss);112s.start();113114URL url = URIBuilder.newBuilder()115.scheme("http")116.loopback()117.port(ss.getLocalPort())118.toURL();119System.out.println("URL: " + url);120121// start fetch in its own thread group122ThreadGroup grp = new ThreadGroup("MyGroup");123124// http request in another thread group125Thread thr = new Thread(grp, new Fetcher(url));126thr.start();127thr.join();128129// fetcher is done - the group should now be empty130if (grp.activeCount() > 0) {131throw new RuntimeException("Keep-alive thread started in wrong thread group");132}133134grp.destroy();135}136137}138139140