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/AsyncDisconnect.java
38867 views
1
/*
2
* Copyright (c) 2006, 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 6358532
27
* @run main/othervm AsyncDisconnect
28
* @summary HttpURLConnection.disconnect doesn't really do the job
29
*/
30
31
import java.net.*;
32
import java.util.*;
33
import java.io.*;
34
import com.sun.net.httpserver.*;
35
import java.util.concurrent.Executors;
36
import java.util.concurrent.ExecutorService;
37
38
public class AsyncDisconnect implements Runnable
39
{
40
com.sun.net.httpserver.HttpServer httpServer;
41
MyHandler httpHandler;
42
ExecutorService executorService;
43
HttpURLConnection uc;
44
45
public static void main(String[] args) {
46
new AsyncDisconnect();
47
}
48
49
public AsyncDisconnect() {
50
try {
51
startHttpServer();
52
doClient();
53
} catch (IOException ioe) {
54
System.err.println(ioe);
55
}
56
}
57
58
void doClient() {
59
try {
60
InetSocketAddress address = httpServer.getAddress();
61
URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");
62
uc = (HttpURLConnection)url.openConnection();
63
64
// create a thread that will disconnect the connection
65
(new Thread(this)).start();
66
67
uc.getInputStream();
68
69
// if we reach here then we have failed
70
throw new RuntimeException("Failed: We Expect a SocketException to be thrown");
71
72
} catch (SocketException se) {
73
// this is what we expect to happen and is OK.
74
//System.out.println(se);
75
} catch (IOException e) {
76
e.printStackTrace();
77
} finally {
78
httpServer.stop(1);
79
executorService.shutdown();
80
}
81
}
82
83
public void run() {
84
// wait for the request to be sent to the server before calling disconnect
85
try { Thread.sleep(2000); }
86
catch (Exception e) {}
87
88
uc.disconnect();
89
}
90
91
/**
92
* Http Server
93
*/
94
public void startHttpServer() throws IOException {
95
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
96
httpHandler = new MyHandler();
97
98
HttpContext ctx = httpServer.createContext("/test/", httpHandler);
99
100
executorService = Executors.newCachedThreadPool();
101
httpServer.setExecutor(executorService);
102
httpServer.start();
103
}
104
105
class MyHandler implements HttpHandler {
106
public void handle(HttpExchange t) throws IOException {
107
// give the other thread a chance to close the connection
108
try { Thread.sleep(4000); }
109
catch (Exception e) {}
110
111
t.sendResponseHeaders(400, -1);
112
t.close();
113
}
114
}
115
116
}
117
118