Path: blob/master/test/jdk/com/sun/net/httpserver/HttpServerTest.java
41152 views
/*1* Copyright (c) 2019, 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 823318526* @summary HttpServer.stop() blocks indefinitely when called on dispatch thread27* @modules java.base/sun.net.www28* @library /test/lib29* @run main/othervm HttpServerTest30*/3132import java.io.IOException;33import java.io.InputStream;34import java.net.HttpURLConnection;35import java.net.InetAddress;36import java.net.InetSocketAddress;37import java.net.Proxy;38import java.net.URL;39import java.util.concurrent.CountDownLatch;4041import com.sun.net.httpserver.HttpExchange;42import com.sun.net.httpserver.HttpHandler;43import com.sun.net.httpserver.HttpServer;44import jdk.test.lib.net.URIBuilder;4546public class HttpServerTest implements HttpHandler {47private static final int HTTP_STATUS_CODE_OK = 200;48private static CountDownLatch serverStopped = new CountDownLatch(1);49private final HttpServer server;505152public HttpServerTest(HttpServer server) {53this.server = server;54}5556@Override57public void handle(HttpExchange ex) throws IOException {5859sendHttpStatusCode(HTTP_STATUS_CODE_OK, ex);6061System.out.println("Stopping server ...");62server.stop(1);63serverStopped.countDown();64}6566private void sendHttpStatusCode(int httpCode, HttpExchange ex) {67try {68ex.sendResponseHeaders(httpCode, 0);69ex.close();70} catch (IOException e) {71throw new RuntimeException("Server couldn't send response: " + e, e);72}73}7475public static void main(String[] args) throws IOException, InterruptedException {76HttpServer server = HttpServer.create(77new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);7879try {80server.createContext("/context", new HttpServerTest(server));81server.start();8283URL url = URIBuilder.newBuilder()84.scheme("http")85.loopback()86.port(server.getAddress().getPort())87.path("/context")88.toURLUnchecked();8990HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);91System.out.println("Client: Response code received: " + urlc.getResponseCode());92InputStream is = urlc.getInputStream();93is.readAllBytes();94is.close();95} finally {96serverStopped.await();97System.out.println("Server stopped as expected");98}99}100}101102103