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/http/KeepAliveStream/InfiniteLoop.java
38868 views
1
/*
2
* Copyright (c) 2012, 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
* @summary Checks for proper close code in KeepAliveStream
28
*/
29
30
import com.sun.net.httpserver.HttpExchange;
31
import com.sun.net.httpserver.HttpHandler;
32
import com.sun.net.httpserver.HttpServer;
33
import java.io.*;
34
import java.net.*;
35
import java.util.concurrent.Phaser;
36
37
// Racey test, will not always fail, but if it does then we have a problem.
38
39
public class InfiniteLoop {
40
41
public static void main(String[] args) throws Exception {
42
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
43
server.createContext("/test/InfiniteLoop", new RespHandler());
44
server.start();
45
try {
46
InetSocketAddress address = server.getAddress();
47
URL url = new URL("http://localhost:" + address.getPort()
48
+ "/test/InfiniteLoop");
49
final Phaser phaser = new Phaser(2);
50
for (int i=0; i<10; i++) {
51
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
52
final InputStream is = uc.getInputStream();
53
final Thread thread = new Thread() {
54
public void run() {
55
try {
56
phaser.arriveAndAwaitAdvance();
57
while (is.read() != -1)
58
Thread.sleep(50);
59
} catch (Exception x) { x.printStackTrace(); }
60
}};
61
thread.start();
62
phaser.arriveAndAwaitAdvance();
63
is.close();
64
System.out.println("returned from close");
65
thread.join();
66
}
67
} finally {
68
server.stop(0);
69
}
70
}
71
72
static class RespHandler implements HttpHandler {
73
static final int RESP_LENGTH = 32 * 1024;
74
@Override
75
public void handle(HttpExchange t) throws IOException {
76
InputStream is = t.getRequestBody();
77
byte[] ba = new byte[8192];
78
while(is.read(ba) != -1);
79
80
t.sendResponseHeaders(200, RESP_LENGTH);
81
try (OutputStream os = t.getResponseBody()) {
82
os.write(new byte[RESP_LENGTH]);
83
}
84
t.close();
85
}
86
}
87
}
88
89