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