jdk/test/java/net/httpclient/http2/Timeout.java
author asmotrak
Fri, 13 May 2016 11:32:41 -0700
changeset 37906 c2ea5f68c2ff
child 42460 7133f144981a
permissions -rw-r--r--
8156710: HttpTimeoutException should be thrown if server doesn't respond Reviewed-by: michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37906
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
     1
/*
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
     4
 *
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
     8
 *
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    13
 * accompanied this code).
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    14
 *
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    18
 *
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    21
 * questions.
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    22
 */
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    23
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    24
import java.io.File;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    25
import java.io.IOException;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    26
import java.net.URI;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    27
import java.net.http.HttpClient;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    28
import java.net.http.HttpRequest;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    29
import java.net.http.HttpResponse;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    30
import java.net.http.HttpTimeoutException;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    31
import java.util.concurrent.TimeUnit;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    32
import javax.net.ssl.SSLServerSocket;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    33
import javax.net.ssl.SSLServerSocketFactory;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    34
import javax.net.ssl.SSLSocket;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    35
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    36
/*
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    37
 * @test
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    38
 * @bug 8156710
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    39
 * @summary Check if HttpTimeoutException is thrown if a server doesn't reply
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    40
 * @run main/othervm Timeout
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    41
 */
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    42
public class Timeout {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    43
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    44
    private static final int RANDOM_PORT = 0;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    45
    private static final int TIMEOUT = 3 * 1000; // in millis
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    46
    private static final String KEYSTORE = System.getProperty("test.src")
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    47
            + File.separator + "keystore.p12";
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    48
    private static final String PASSWORD = "password";
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    49
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    50
    // indicates if server is ready to accept connections
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    51
    private static volatile boolean ready = false;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    52
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    53
    public static void main(String[] args) throws Exception {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    54
        System.setProperty("javax.net.ssl.keyStore", KEYSTORE);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    55
        System.setProperty("javax.net.ssl.keyStorePassword", PASSWORD);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    56
        System.setProperty("javax.net.ssl.trustStore", KEYSTORE);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    57
        System.setProperty("javax.net.ssl.trustStorePassword", PASSWORD);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    58
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    59
        SSLServerSocketFactory factory =
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    60
                (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    61
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    62
        try (SSLServerSocket ssocket =
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    63
                (SSLServerSocket) factory.createServerSocket(RANDOM_PORT)) {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    64
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    65
            // start server
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    66
            Thread server = new Thread(() -> {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    67
                while (true) {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    68
                    System.out.println("server: ready");
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    69
                    ready = true;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    70
                    try (SSLSocket socket = (SSLSocket) ssocket.accept()) {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    71
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    72
                        // just read forever
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    73
                        System.out.println("server: accepted");
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    74
                        while (true) {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    75
                            socket.getInputStream().read();
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    76
                        }
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    77
                    } catch (IOException e) {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    78
                        // ignore exceptions on server side
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    79
                        System.out.println("server: exception: " + e);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    80
                    }
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    81
                }
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    82
            });
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    83
            server.setDaemon(true);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    84
            server.start();
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    85
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    86
            // wait for server is ready
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    87
            do {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    88
                Thread.sleep(1000);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    89
            } while (!ready);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    90
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    91
            String uri = "https://localhost:" + ssocket.getLocalPort();
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    92
            connect(uri);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    93
        }
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    94
    }
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    95
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    96
    private static void connect(String server) throws Exception {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    97
        try {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    98
            HttpClient.create()
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    99
                    .version(HttpClient.Version.HTTP_2)
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   100
                    .build()
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   101
                    .request(new URI(server))
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   102
                    .timeout(TimeUnit.MILLISECONDS, TIMEOUT)
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   103
                    .body(HttpRequest.fromString("body"))
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   104
                    .GET()
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   105
                    .response()
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   106
                    .body(HttpResponse.asString());
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   107
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   108
            throw new RuntimeException("unexpected successful connection");
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   109
        } catch (HttpTimeoutException e) {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   110
            System.out.println("expected exception: " + e);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   111
        }
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   112
    }
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   113
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   114
}