Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/http/KeepAliveStream/InfiniteLoop.java
41154 views
1
/*
2
* Copyright (c) 2012, 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 8004863
27
* @modules jdk.httpserver
28
* @summary Checks for proper close code in KeepAliveStream
29
* @library /test/lib
30
* @run main InfiniteLoop
31
* @run main/othervm -Djava.net.preferIPv6Addresses=true InfiniteLoop
32
*/
33
34
import com.sun.net.httpserver.HttpExchange;
35
import com.sun.net.httpserver.HttpHandler;
36
import com.sun.net.httpserver.HttpServer;
37
import java.io.InputStream;
38
import java.io.IOException;
39
import java.io.OutputStream;
40
import java.net.HttpURLConnection;
41
import java.net.InetAddress;
42
import java.net.InetSocketAddress;
43
import java.net.Proxy;
44
import java.net.URL;
45
import java.util.concurrent.Phaser;
46
47
import jdk.test.lib.net.URIBuilder;
48
49
// Racey test, will not always fail, but if it does then we have a problem.
50
51
public class InfiniteLoop {
52
53
public static void main(String[] args) throws Exception {
54
InetAddress loopback = InetAddress.getLoopbackAddress();
55
HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
56
server.createContext("/test/InfiniteLoop", new RespHandler());
57
server.start();
58
try {
59
InetSocketAddress address = server.getAddress();
60
URL url = URIBuilder.newBuilder()
61
.scheme("http")
62
.host(server.getAddress().getAddress())
63
.port(server.getAddress().getPort())
64
.path("/test/InfiniteLoop")
65
.toURL();
66
final Phaser phaser = new Phaser(2);
67
for (int i=0; i<10; i++) {
68
HttpURLConnection uc = (HttpURLConnection)
69
url.openConnection(Proxy.NO_PROXY);
70
final InputStream is = uc.getInputStream();
71
final Thread thread = new Thread() {
72
public void run() {
73
try {
74
phaser.arriveAndAwaitAdvance();
75
while (is.read() != -1)
76
Thread.sleep(50);
77
} catch (Exception x) { x.printStackTrace(); }
78
}};
79
thread.start();
80
phaser.arriveAndAwaitAdvance();
81
is.close();
82
System.out.println("returned from close");
83
thread.join();
84
}
85
} finally {
86
server.stop(0);
87
}
88
}
89
90
static class RespHandler implements HttpHandler {
91
static final int RESP_LENGTH = 32 * 1024;
92
@Override
93
public void handle(HttpExchange t) throws IOException {
94
InputStream is = t.getRequestBody();
95
byte[] ba = new byte[8192];
96
while(is.read(ba) != -1);
97
98
t.sendResponseHeaders(200, RESP_LENGTH);
99
try (OutputStream os = t.getResponseBody()) {
100
os.write(new byte[RESP_LENGTH]);
101
}
102
t.close();
103
}
104
}
105
}
106
107