test/jdk/com/sun/net/httpserver/HttpServerTest.java
author jboes
Thu, 28 Nov 2019 13:02:30 +0000
changeset 59311 b42eaca7d234
permissions -rw-r--r--
8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread Summary: Skip dispatcherThread.join() if current thread is the dispatcherThread Reviewed-by: dfuchs, msheppar, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59311
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
     1
/*
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
     4
 *
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
     8
 *
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    13
 * accompanied this code).
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    14
 *
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    18
 *
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    21
 * questions.
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    22
 */
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    23
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    24
/**
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    25
 * @test
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    26
 * @bug 8233185
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    27
 * @summary HttpServer.stop() blocks indefinitely when called on dispatch thread
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    28
 * @modules java.base/sun.net.www
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    29
 * @library /test/lib
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    30
 * @run main/othervm HttpServerTest
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    31
 */
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    32
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    33
import java.io.IOException;
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    34
import java.io.InputStream;
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    35
import java.net.HttpURLConnection;
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    36
import java.net.InetAddress;
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    37
import java.net.InetSocketAddress;
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    38
import java.net.Proxy;
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    39
import java.net.URL;
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    40
import java.util.concurrent.CountDownLatch;
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    41
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    42
import com.sun.net.httpserver.HttpExchange;
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    43
import com.sun.net.httpserver.HttpHandler;
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    44
import com.sun.net.httpserver.HttpServer;
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    45
import jdk.test.lib.net.URIBuilder;
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    46
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    47
public class HttpServerTest implements HttpHandler {
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    48
    private static final int HTTP_STATUS_CODE_OK = 200;
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    49
    private static CountDownLatch serverStopped = new CountDownLatch(1);
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    50
    private final HttpServer server;
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    51
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    52
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    53
    public HttpServerTest(HttpServer server) {
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    54
        this.server = server;
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    55
    }
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    56
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    57
    @Override
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    58
    public void handle(HttpExchange ex) throws IOException {
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    59
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    60
        sendHttpStatusCode(HTTP_STATUS_CODE_OK, ex);
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    61
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    62
        System.out.println("Stopping server ...");
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    63
        server.stop(1);
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    64
        serverStopped.countDown();
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    65
    }
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    66
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    67
    private void sendHttpStatusCode(int httpCode, HttpExchange ex) {
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    68
        try {
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    69
            ex.sendResponseHeaders(httpCode, 0);
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    70
            ex.close();
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    71
        } catch (IOException e) {
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    72
            throw new RuntimeException("Server couldn't send response: " + e, e);
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    73
        }
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    74
    }
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    75
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    76
    public static void main(String[] args) throws IOException, InterruptedException {
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    77
        HttpServer server = HttpServer.create(
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    78
                new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    79
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    80
        try {
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    81
            server.createContext("/context", new HttpServerTest(server));
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    82
            server.start();
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    83
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    84
            URL url = URIBuilder.newBuilder()
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    85
                    .scheme("http")
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    86
                    .loopback()
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    87
                    .port(server.getAddress().getPort())
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    88
                    .path("/context")
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    89
                    .toURLUnchecked();
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    90
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    91
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    92
            System.out.println("Client: Response code received: " + urlc.getResponseCode());
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    93
            InputStream is = urlc.getInputStream();
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    94
            is.readAllBytes();
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    95
            is.close();
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    96
        } finally {
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    97
            serverStopped.await();
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    98
            System.out.println("Server stopped as expected");
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
    99
        }
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
   100
    }
b42eaca7d234 8233185: HttpServer.stop() blocks indefinitely when called on dispatch thread
jboes
parents:
diff changeset
   101
}