Path: blob/master/test/jdk/com/sun/net/httpserver/TaskRejectedTest.java
41152 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 816935826* @summary HttpServer does not close client connection when RejectedExecutionException occurs.27*/2829import com.sun.net.httpserver.HttpServer;3031import java.io.IOException;32import java.net.HttpURLConnection;33import java.net.InetAddress;34import java.net.InetSocketAddress;35import java.net.MalformedURLException;36import java.net.SocketException;37import java.net.SocketTimeoutException;38import java.net.URL;39import java.util.concurrent.Executor;40import java.util.concurrent.Executors;41import java.util.concurrent.RejectedExecutionException;42import java.util.logging.ConsoleHandler;43import java.util.logging.Handler;44import java.util.logging.Level;45import java.util.logging.Logger;464748public class TaskRejectedTest {4950private static final int BACKLOG = 0;5152private static final String REQUEST_PATH = "/";5354private static final int TIMEOUT = 10000; // 10 sec5556private static void runClient(InetSocketAddress listenAddr)57throws MalformedURLException, IOException {58URL url = new URL("http", listenAddr.getHostString(),59listenAddr.getPort(), REQUEST_PATH);60HttpURLConnection con = (HttpURLConnection)url.openConnection();61con.setConnectTimeout(TIMEOUT);62con.setReadTimeout(TIMEOUT);6364try {65con.connect();66con.getResponseCode();67} catch (SocketTimeoutException e) {68throw new RuntimeException("Connection was not closed by peer.", e);69} catch (SocketException e) {70// Expected (connection reset)71} finally {72con.disconnect();73}74}7576public static void main(String[] args) throws Exception {77Logger logger = Logger.getLogger(78HttpServer.class.getPackage().getName());79Handler consoleHandler = new ConsoleHandler();80consoleHandler.setLevel(Level.FINEST);81logger.setLevel(Level.FINEST);82logger.addHandler(consoleHandler);8384Executor executor = Executors.newSingleThreadExecutor(r -> {85throw new RejectedExecutionException("test");86});8788InetSocketAddress addr = new InetSocketAddress(89InetAddress.getLoopbackAddress(), 0);90HttpServer httpServer = HttpServer.create(addr, BACKLOG);91httpServer.setExecutor(executor);9293httpServer.createContext(REQUEST_PATH, exc -> {94exc.sendResponseHeaders(200, 0);95exc.close();96});9798try {99httpServer.start();100runClient(httpServer.getAddress());101} finally {102httpServer.stop(0);103}104}105}106107108109