blob: 3486e23284ca2f19e6b84e9cc8f9a6889e87ea2c [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4701299
27 * @summary Keep-Alive-Timer thread management in KeepAliveCache causes memory leak
28 */
29import java.net.*;
30import java.io.*;
31
32public class KeepAliveTimerThread {
33 static class Fetcher implements Runnable {
34 String url;
35
36 Fetcher(String url) {
37 this.url = url;
38 }
39
40 public void run() {
41 try {
42 InputStream in =
43 (new URL(url)).openConnection().getInputStream();
44 byte b[] = new byte[128];
45 int n;
46 do {
47 n = in.read(b);
48 } while (n > 0);
49 in.close();
50 } catch (Exception x) {
51 x.printStackTrace();
52 }
53 }
54 }
55
56 static class Server extends Thread {
57 ServerSocket server;
58 Server (ServerSocket server) {
59 super ();
60 this.server = server;
61 }
62 void readAll (Socket s) throws IOException {
63 byte[] buf = new byte [128];
64 InputStream is = s.getInputStream ();
65 s.setSoTimeout(1000);
66 try {
67 while (is.read(buf) > 0) ;
68 } catch (SocketTimeoutException x) { }
69 }
70 /*
71 * Our "http" server to return a 404
72 */
73 public void run() {
74 try {
75 Socket s = server.accept();
76 readAll(s);
77
78 PrintStream out = new PrintStream(
79 new BufferedOutputStream(
80 s.getOutputStream() ));
81
82 /* send the header */
83 out.print("HTTP/1.1 200 OK\r\n");
84 out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
85 out.print("Content-Length: 78\r\n");
86 out.print("\r\n");
87 out.print("<HTML>");
88 out.print("<HEAD><TITLE>File Content</TITLE></HEAD>");
89 out.print("<BODY>A dummy body.</BODY>");
90 out.print("</HTML>");
91 out.flush();
92
93 s.close();
94 server.close();
95 } catch (Exception e) {
96 e.printStackTrace();
97 }
98 }
99 }
100
101
102 public static void main(String args[]) throws Exception {
103 ServerSocket ss = new ServerSocket(0);
104 Server s = new Server (ss);
105 s.start();
106
107 String url = "http://127.0.0.1:"+ss.getLocalPort();
108
109 // start fetch in its own thread group
110 ThreadGroup grp = new ThreadGroup("MyGroup");
111
112 // http request in another thread group
113 Thread thr = new Thread(grp, new Fetcher(url));
114 thr.start();
115 thr.join();
116
117 // fetcher is done - the group should now be empty
118 if (grp.activeCount() > 0) {
119 throw new RuntimeException("Keep-alive thread started in wrong thread group");
120 }
121 }
122
123}