test/jdk/java/net/httpclient/http2/Timeout.java
author chegar
Sat, 03 Mar 2018 09:54:31 +0000
branchhttp-client-branch
changeset 56233 1753108d07b9
parent 56167 96fa4f49a9ff
child 56265 ec34ae013fbe
permissions -rw-r--r--
http-client-branch: setReuseAddress(false) for all test ServerSockets
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
/*
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
     2
 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
37906
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;
56233
1753108d07b9 http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents: 56167
diff changeset
    26
import java.net.InetSocketAddress;
37906
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    27
import java.net.URI;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55973
diff changeset
    28
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55973
diff changeset
    29
import java.net.http.HttpRequest;
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
    30
import java.net.http.HttpRequest.BodyPublishers;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55973
diff changeset
    31
import java.net.http.HttpResponse;
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
    32
import java.net.http.HttpResponse.BodyHandlers;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55973
diff changeset
    33
import java.net.http.HttpTimeoutException;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
    34
import java.time.Duration;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
    35
import java.util.concurrent.CompletionException;
37906
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    36
import javax.net.ssl.SSLServerSocket;
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 42460
diff changeset
    37
import javax.net.ssl.SSLParameters;
37906
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    38
import javax.net.ssl.SSLServerSocketFactory;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    39
import javax.net.ssl.SSLSocket;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    40
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
 * @test
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    43
 * @bug 8156710
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    44
 * @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
    45
 * @run main/othervm Timeout
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    46
 */
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    47
public class Timeout {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    48
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    49
    private static final int RANDOM_PORT = 0;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    50
    private static final int TIMEOUT = 3 * 1000; // in millis
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    51
    private static final String KEYSTORE = System.getProperty("test.src")
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    52
            + File.separator + "keystore.p12";
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    53
    private static final String PASSWORD = "password";
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    54
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    55
    // indicates if server is ready to accept connections
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    56
    private static volatile boolean ready = false;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    57
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    58
    public static void main(String[] args) throws Exception {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
    59
        test(false);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
    60
        test(true);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
    61
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
    62
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
    63
    public static void test(boolean async) throws Exception {
37906
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    64
        System.setProperty("javax.net.ssl.keyStore", KEYSTORE);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    65
        System.setProperty("javax.net.ssl.keyStorePassword", PASSWORD);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    66
        System.setProperty("javax.net.ssl.trustStore", KEYSTORE);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    67
        System.setProperty("javax.net.ssl.trustStorePassword", PASSWORD);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    68
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    69
        SSLServerSocketFactory factory =
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    70
                (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
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
        try (SSLServerSocket ssocket =
56233
1753108d07b9 http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents: 56167
diff changeset
    73
                (SSLServerSocket) factory.createServerSocket()) {
1753108d07b9 http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents: 56167
diff changeset
    74
            ssocket.setReuseAddress(false);
1753108d07b9 http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents: 56167
diff changeset
    75
            ssocket.bind(new InetSocketAddress(RANDOM_PORT));
37906
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
            // start server
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    78
            Thread server = new Thread(() -> {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    79
                while (true) {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    80
                    System.out.println("server: ready");
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 42460
diff changeset
    81
                    SSLParameters params = ssocket.getSSLParameters();
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 42460
diff changeset
    82
                    params.setApplicationProtocols(new String[]{"h2"});
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 42460
diff changeset
    83
                    ssocket.setSSLParameters(params);
37906
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    84
                    ready = true;
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    85
                    try (SSLSocket socket = (SSLSocket) ssocket.accept()) {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    86
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    87
                        // just read forever
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    88
                        System.out.println("server: accepted");
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    89
                        while (true) {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    90
                            socket.getInputStream().read();
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    91
                        }
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    92
                    } catch (IOException e) {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    93
                        // ignore exceptions on server side
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    94
                        System.out.println("server: exception: " + e);
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
                }
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    97
            });
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    98
            server.setDaemon(true);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
    99
            server.start();
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   100
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   101
            // wait for server is ready
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   102
            do {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   103
                Thread.sleep(1000);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   104
            } while (!ready);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   105
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   106
            String uri = "https://localhost:" + ssocket.getLocalPort();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   107
            if (async) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   108
                System.out.println(uri + ": Trying to connect asynchronously");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   109
                connectAsync(uri);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   110
            } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   111
                System.out.println(uri + ": Trying to connect synchronously");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   112
                connect(uri);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   113
            }
37906
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   114
        }
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   115
    }
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   116
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   117
    private static void connect(String server) throws Exception {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   118
        try {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   119
            HttpClient client = HttpClient.newBuilder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   120
                                          .version(HttpClient.Version.HTTP_2)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   121
                                          .build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   122
            HttpRequest request = HttpRequest.newBuilder(new URI(server))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   123
                                             .timeout(Duration.ofMillis(TIMEOUT))
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   124
                                             .POST(BodyPublishers.ofString("body"))
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   125
                                             .build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   126
            HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   127
            System.out.println("Received unexpected reply: " + response.statusCode());
37906
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   128
            throw new RuntimeException("unexpected successful connection");
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   129
        } catch (HttpTimeoutException e) {
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   130
            System.out.println("expected exception: " + e);
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   131
        }
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   132
    }
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   133
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   134
    private static void connectAsync(String server) throws Exception {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   135
        try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   136
            HttpClient client = HttpClient.newBuilder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   137
                    .version(HttpClient.Version.HTTP_2)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   138
                    .build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   139
            HttpRequest request = HttpRequest.newBuilder(new URI(server))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   140
                    .timeout(Duration.ofMillis(TIMEOUT))
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   141
                    .POST(BodyPublishers.ofString("body"))
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   142
                    .build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   143
            HttpResponse<String> response = client.sendAsync(request, BodyHandlers.ofString()).join();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   144
            System.out.println("Received unexpected reply: " + response.statusCode());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   145
            throw new RuntimeException("unexpected successful connection");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   146
        } catch (CompletionException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   147
            if (e.getCause() instanceof HttpTimeoutException) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   148
                System.out.println("expected exception: " + e.getCause());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   149
            } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   150
                throw new RuntimeException("Unexpected exception received: " + e.getCause(), e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   151
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   152
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   153
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37906
diff changeset
   154
37906
c2ea5f68c2ff 8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff changeset
   155
}