|
1 /* |
|
2 * Copyright (c) 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 8233185 |
|
27 * @summary HttpServer.stop() blocks indefinitely when called on dispatch thread |
|
28 * @modules java.base/sun.net.www |
|
29 * @library /test/lib |
|
30 * @run main/othervm HttpServerTest |
|
31 */ |
|
32 |
|
33 import java.io.IOException; |
|
34 import java.io.InputStream; |
|
35 import java.net.HttpURLConnection; |
|
36 import java.net.InetAddress; |
|
37 import java.net.InetSocketAddress; |
|
38 import java.net.Proxy; |
|
39 import java.net.URL; |
|
40 import java.util.concurrent.CountDownLatch; |
|
41 |
|
42 import com.sun.net.httpserver.HttpExchange; |
|
43 import com.sun.net.httpserver.HttpHandler; |
|
44 import com.sun.net.httpserver.HttpServer; |
|
45 import jdk.test.lib.net.URIBuilder; |
|
46 |
|
47 public class HttpServerTest implements HttpHandler { |
|
48 private static final int HTTP_STATUS_CODE_OK = 200; |
|
49 private static CountDownLatch serverStopped = new CountDownLatch(1); |
|
50 private final HttpServer server; |
|
51 |
|
52 |
|
53 public HttpServerTest(HttpServer server) { |
|
54 this.server = server; |
|
55 } |
|
56 |
|
57 @Override |
|
58 public void handle(HttpExchange ex) throws IOException { |
|
59 |
|
60 sendHttpStatusCode(HTTP_STATUS_CODE_OK, ex); |
|
61 |
|
62 System.out.println("Stopping server ..."); |
|
63 server.stop(1); |
|
64 serverStopped.countDown(); |
|
65 } |
|
66 |
|
67 private void sendHttpStatusCode(int httpCode, HttpExchange ex) { |
|
68 try { |
|
69 ex.sendResponseHeaders(httpCode, 0); |
|
70 ex.close(); |
|
71 } catch (IOException e) { |
|
72 throw new RuntimeException("Server couldn't send response: " + e, e); |
|
73 } |
|
74 } |
|
75 |
|
76 public static void main(String[] args) throws IOException, InterruptedException { |
|
77 HttpServer server = HttpServer.create( |
|
78 new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); |
|
79 |
|
80 try { |
|
81 server.createContext("/context", new HttpServerTest(server)); |
|
82 server.start(); |
|
83 |
|
84 URL url = URIBuilder.newBuilder() |
|
85 .scheme("http") |
|
86 .loopback() |
|
87 .port(server.getAddress().getPort()) |
|
88 .path("/context") |
|
89 .toURLUnchecked(); |
|
90 |
|
91 HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); |
|
92 System.out.println("Client: Response code received: " + urlc.getResponseCode()); |
|
93 InputStream is = urlc.getInputStream(); |
|
94 is.readAllBytes(); |
|
95 is.close(); |
|
96 } finally { |
|
97 serverStopped.await(); |
|
98 System.out.println("Server stopped as expected"); |
|
99 } |
|
100 } |
|
101 } |