Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/B6518816.java
38867 views
1
/*
2
* Copyright (c) 2007, 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
* @run main/othervm B6518816
28
*/
29
30
import java.net.*;
31
import java.util.*;
32
import java.io.*;
33
import com.sun.net.httpserver.*;
34
import java.util.concurrent.Executors;
35
import java.util.concurrent.ExecutorService;
36
37
public class B6518816
38
{
39
com.sun.net.httpserver.HttpServer httpServer;
40
ExecutorService executorService;
41
42
public static void main(String[] args)
43
{
44
new B6518816();
45
}
46
47
public B6518816()
48
{
49
try {
50
startHttpServer();
51
doClient();
52
} catch (IOException ioe) {
53
System.err.println(ioe);
54
}
55
}
56
57
final static int MAX_CONNS = 10;
58
final static int TEN_MB = 10 * 1024 * 1024;
59
static boolean stopped = false;
60
61
/*
62
* we send approx 100MB and if more than 90MB is retained
63
* then the bug is still there
64
*/
65
void doClient() {
66
67
try {
68
InetSocketAddress address = httpServer.getAddress();
69
List<HttpURLConnection> conns = new LinkedList<HttpURLConnection>();
70
71
long totalmem1=0, totalmem2=0;
72
System.gc();
73
Runtime runtime = Runtime.getRuntime();
74
totalmem1 = runtime.totalMemory() - runtime.freeMemory();
75
System.out.println ("Sending " + (TEN_MB*MAX_CONNS/1024) + " KB");
76
System.out.println ("At start: " + totalmem1/1024 + " KB");
77
78
byte [] buf = new byte [TEN_MB];
79
80
for (int j=0; j<MAX_CONNS; j++) {
81
82
URL url = new URL("http://localhost:"+address.getPort()+"/test/");
83
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
84
uc.setDoOutput (true);
85
OutputStream os = uc.getOutputStream ();
86
os.write (buf);
87
InputStream is = uc.getInputStream();
88
int resp = uc.getResponseCode();
89
if (resp != 200) {
90
throw new RuntimeException("Failed: Part 1, Response code is not 200");
91
}
92
while (is.read() != -1) ;
93
is.close();
94
95
conns.add (uc);
96
}
97
httpServer.stop(1);
98
httpServer = null;
99
executorService.shutdown();
100
executorService = null;
101
stopped = true;
102
buf = null;
103
System.runFinalization();
104
try {Thread.sleep (1000); } catch (InterruptedException e){}
105
System.gc();
106
try {Thread.sleep (1000); } catch (InterruptedException e){}
107
System.gc();
108
totalmem2 = runtime.totalMemory() - runtime.freeMemory();
109
System.out.println ("At end: " + totalmem2/1024 + " KB");
110
long diff = (totalmem2 - totalmem1) ;;
111
System.out.println ("Diff " + diff/1024 + " kbytes ");
112
if (diff > (TEN_MB*MAX_CONNS*0.9)) {
113
/* if >= 90 MB retained, then problem still there */
114
throw new RuntimeException ("Excessive memory retained");
115
}
116
117
} catch (IOException e) {
118
e.printStackTrace();
119
throw new RuntimeException ("IOException");
120
} finally {
121
if (!stopped) {
122
httpServer.stop(1);
123
executorService.shutdown();
124
}
125
}
126
}
127
128
/**
129
* Http Server
130
*/
131
public void startHttpServer() throws IOException {
132
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
133
134
// create HttpServer context for Part 1.
135
HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
136
137
executorService = Executors.newCachedThreadPool();
138
httpServer.setExecutor(executorService);
139
httpServer.start();
140
}
141
142
class MyHandler implements HttpHandler {
143
public void handle(HttpExchange t) throws IOException {
144
InputStream is = t.getRequestBody();
145
Headers reqHeaders = t.getRequestHeaders();
146
Headers resHeaders = t.getResponseHeaders();
147
byte[] buf = new byte [16 * 1024];
148
while (is.read (buf) != -1) ;
149
t.sendResponseHeaders(200, -1);
150
t.close();
151
}
152
}
153
154
}
155
156