|
1 /* |
|
2 * Copyright (c) 2018, 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 8169358 |
|
27 * @summary HttpServer does not close client connection when RejectedExecutionException occurs. |
|
28 */ |
|
29 |
|
30 import com.sun.net.httpserver.HttpServer; |
|
31 |
|
32 import java.io.IOException; |
|
33 import java.net.HttpURLConnection; |
|
34 import java.net.InetAddress; |
|
35 import java.net.InetSocketAddress; |
|
36 import java.net.MalformedURLException; |
|
37 import java.net.SocketException; |
|
38 import java.net.SocketTimeoutException; |
|
39 import java.net.URL; |
|
40 import java.util.concurrent.Executor; |
|
41 import java.util.concurrent.Executors; |
|
42 import java.util.concurrent.RejectedExecutionException; |
|
43 import java.util.logging.ConsoleHandler; |
|
44 import java.util.logging.Handler; |
|
45 import java.util.logging.Level; |
|
46 import java.util.logging.Logger; |
|
47 |
|
48 |
|
49 public class TaskRejectedTest { |
|
50 |
|
51 private static final int BACKLOG = 0; |
|
52 |
|
53 private static final String REQUEST_PATH = "/"; |
|
54 |
|
55 private static final int TIMEOUT = 10000; // 10 sec |
|
56 |
|
57 private static void runClient(InetSocketAddress listenAddr) |
|
58 throws MalformedURLException, IOException { |
|
59 URL url = new URL("http", listenAddr.getHostString(), |
|
60 listenAddr.getPort(), REQUEST_PATH); |
|
61 HttpURLConnection con = (HttpURLConnection)url.openConnection(); |
|
62 con.setConnectTimeout(TIMEOUT); |
|
63 con.setReadTimeout(TIMEOUT); |
|
64 |
|
65 try { |
|
66 con.connect(); |
|
67 con.getResponseCode(); |
|
68 } catch (SocketTimeoutException e) { |
|
69 throw new RuntimeException("Connection was not closed by peer.", e); |
|
70 } catch (SocketException e) { |
|
71 // Expected (connection reset) |
|
72 } finally { |
|
73 con.disconnect(); |
|
74 } |
|
75 } |
|
76 |
|
77 public static void main(String[] args) throws Exception { |
|
78 Logger logger = Logger.getLogger( |
|
79 HttpServer.class.getPackage().getName()); |
|
80 Handler consoleHandler = new ConsoleHandler(); |
|
81 consoleHandler.setLevel(Level.FINEST); |
|
82 logger.setLevel(Level.FINEST); |
|
83 logger.addHandler(consoleHandler); |
|
84 |
|
85 Executor executor = Executors.newSingleThreadExecutor(r -> { |
|
86 throw new RejectedExecutionException("test"); |
|
87 }); |
|
88 |
|
89 InetSocketAddress addr = new InetSocketAddress( |
|
90 InetAddress.getLoopbackAddress(), 0); |
|
91 HttpServer httpServer = HttpServer.create(addr, BACKLOG); |
|
92 httpServer.setExecutor(executor); |
|
93 |
|
94 httpServer.createContext(REQUEST_PATH, exc -> { |
|
95 exc.sendResponseHeaders(200, 0); |
|
96 exc.close(); |
|
97 }); |
|
98 |
|
99 try { |
|
100 httpServer.start(); |
|
101 runClient(httpServer.getAddress()); |
|
102 } finally { |
|
103 httpServer.stop(0); |
|
104 } |
|
105 } |
|
106 } |
|
107 |