test/jdk/com/sun/net/httpserver/TaskRejectedTest.java
author dholmes
Wed, 06 Nov 2019 21:18:42 -0500
changeset 58956 9a0a5e70eeb2
parent 49153 5447851ff0f6
permissions -rw-r--r--
8233454: Test fails with assert(!is_init_completed(), "should only happen during init") after JDK-8229516 Reviewed-by: jiefu, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49153
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
     1
/*
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
     4
 *
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
     8
 *
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    13
 * accompanied this code).
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    14
 *
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    18
 *
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    21
 * questions.
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    22
 */
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    23
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    24
/**
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    25
 * @test
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    26
 * @bug 8169358
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    27
 * @summary  HttpServer does not close client connection when RejectedExecutionException occurs.
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    28
 */
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    29
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    30
import com.sun.net.httpserver.HttpServer;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    31
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    32
import java.io.IOException;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    33
import java.net.HttpURLConnection;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    34
import java.net.InetAddress;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    35
import java.net.InetSocketAddress;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    36
import java.net.MalformedURLException;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    37
import java.net.SocketException;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    38
import java.net.SocketTimeoutException;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    39
import java.net.URL;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    40
import java.util.concurrent.Executor;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    41
import java.util.concurrent.Executors;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    42
import java.util.concurrent.RejectedExecutionException;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    43
import java.util.logging.ConsoleHandler;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    44
import java.util.logging.Handler;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    45
import java.util.logging.Level;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    46
import java.util.logging.Logger;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    47
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    48
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    49
public class TaskRejectedTest {
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    50
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    51
    private static final int BACKLOG = 0;
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    52
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    53
    private static final String REQUEST_PATH = "/";
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    54
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    55
    private static final int TIMEOUT = 10000; // 10 sec
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    56
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    57
    private static void runClient(InetSocketAddress listenAddr)
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    58
                                     throws MalformedURLException, IOException {
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    59
        URL url = new URL("http", listenAddr.getHostString(),
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    60
                                            listenAddr.getPort(), REQUEST_PATH);
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    61
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    62
        con.setConnectTimeout(TIMEOUT);
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    63
        con.setReadTimeout(TIMEOUT);
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    64
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    65
        try {
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    66
            con.connect();
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    67
            con.getResponseCode();
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    68
        } catch (SocketTimeoutException e) {
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    69
            throw new RuntimeException("Connection was not closed by peer.", e);
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    70
        } catch (SocketException e) {
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    71
            // Expected (connection reset)
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    72
        } finally {
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    73
            con.disconnect();
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    74
        }
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    75
    }
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    76
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    77
    public static void main(String[] args) throws Exception {
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    78
        Logger logger = Logger.getLogger(
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    79
                            HttpServer.class.getPackage().getName());
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    80
        Handler consoleHandler = new ConsoleHandler();
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    81
        consoleHandler.setLevel(Level.FINEST);
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    82
        logger.setLevel(Level.FINEST);
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    83
        logger.addHandler(consoleHandler);
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    84
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    85
        Executor executor = Executors.newSingleThreadExecutor(r -> {
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    86
            throw new RejectedExecutionException("test");
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    87
        });
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    88
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    89
        InetSocketAddress addr = new InetSocketAddress(
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    90
                                         InetAddress.getLoopbackAddress(), 0);
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    91
        HttpServer httpServer = HttpServer.create(addr, BACKLOG);
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    92
        httpServer.setExecutor(executor);
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    93
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    94
        httpServer.createContext(REQUEST_PATH, exc -> {
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    95
            exc.sendResponseHeaders(200, 0);
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    96
            exc.close();
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    97
        });
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    98
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
    99
        try {
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
   100
            httpServer.start();
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
   101
            runClient(httpServer.getAddress());
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
   102
        } finally {
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
   103
            httpServer.stop(0);
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
   104
        }
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
   105
    }
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
   106
}
5447851ff0f6 8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
diff changeset
   107