test/jdk/java/net/httpclient/AbstractConnectTimeoutHandshake.java
author chegar
Thu, 09 Aug 2018 11:23:12 +0100
changeset 51364 31d9e82b2e64
permissions -rw-r--r--
8208391: Differentiate response and connect timeouts in HTTP Client API Reviewed-by: michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51364
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
     1
/*
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
     4
 *
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
     8
 *
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    13
 * accompanied this code).
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    14
 *
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    18
 *
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    21
 * questions.
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    22
 */
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    23
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    24
import java.io.BufferedInputStream;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    25
import java.io.IOException;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    26
import java.io.InputStream;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    27
import java.io.UncheckedIOException;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    28
import java.net.ConnectException;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    29
import java.net.InetAddress;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    30
import java.net.InetSocketAddress;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    31
import java.net.ServerSocket;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    32
import java.net.Socket;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    33
import java.net.URI;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    34
import java.time.Duration;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    35
import java.util.ArrayList;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    36
import java.util.Arrays;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    37
import java.util.List;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    38
import java.util.concurrent.CompletableFuture;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    39
import java.util.concurrent.CompletionException;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    40
import java.net.http.HttpClient;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    41
import java.net.http.HttpClient.Version;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    42
import java.net.http.HttpConnectTimeoutException;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    43
import java.net.http.HttpRequest;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    44
import java.net.http.HttpRequest.BodyPublishers;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    45
import java.net.http.HttpResponse;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    46
import java.net.http.HttpResponse.BodyHandlers;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    47
import org.testng.annotations.AfterTest;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    48
import org.testng.annotations.BeforeTest;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    49
import org.testng.annotations.DataProvider;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    50
import static java.lang.String.format;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    51
import static java.lang.System.out;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    52
import static java.net.http.HttpClient.Builder.NO_PROXY;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    53
import static java.net.http.HttpClient.Version.HTTP_1_1;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    54
import static java.net.http.HttpClient.Version.HTTP_2;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    55
import static java.time.Duration.*;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    56
import static java.util.concurrent.TimeUnit.NANOSECONDS;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    57
import static org.testng.Assert.fail;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    58
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    59
public abstract class AbstractConnectTimeoutHandshake {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    60
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    61
    // The number of iterations each testXXXClient performs.
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    62
    static final int TIMES = 2;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    63
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    64
    Server server;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    65
    URI httpsURI;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    66
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    67
    static final Duration NO_DURATION = null;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    68
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    69
    static List<List<Duration>> TIMEOUTS = List.of(
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    70
                    // connectTimeout   HttpRequest timeout
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    71
            Arrays.asList( NO_DURATION,   ofSeconds(1)  ),
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    72
            Arrays.asList( NO_DURATION,   ofSeconds(2)  ),
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    73
            Arrays.asList( NO_DURATION,   ofMillis(500) ),
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    74
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    75
            Arrays.asList( ofSeconds(1),  NO_DURATION   ),
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    76
            Arrays.asList( ofSeconds(2),  NO_DURATION   ),
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    77
            Arrays.asList( ofMillis(500), NO_DURATION   ),
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    78
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    79
            Arrays.asList( ofSeconds(1),  ofMinutes(1)  ),
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    80
            Arrays.asList( ofSeconds(2),  ofMinutes(1)  ),
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    81
            Arrays.asList( ofMillis(500), ofMinutes(1)  )
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    82
    );
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    83
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    84
    static final List<String> METHODS = List.of("GET" , "POST");
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    85
    static final List<Version> VERSIONS = List.of(HTTP_2, HTTP_1_1);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    86
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    87
    @DataProvider(name = "variants")
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    88
    public Object[][] variants() {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    89
        List<Object[]> l = new ArrayList<>();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    90
        for (List<Duration> timeouts : TIMEOUTS) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    91
           Duration connectTimeout = timeouts.get(0);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    92
           Duration requestTimeout = timeouts.get(1);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    93
           for (String method: METHODS) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    94
            for (Version requestVersion : VERSIONS) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    95
             l.add(new Object[] {requestVersion, method, connectTimeout, requestTimeout});
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    96
        }}}
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    97
        return l.stream().toArray(Object[][]::new);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    98
    }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
    99
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   100
    //@Test(dataProvider = "variants")
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   101
    protected void timeoutSync(Version requestVersion,
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   102
                              String method,
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   103
                              Duration connectTimeout,
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   104
                              Duration requestTimeout)
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   105
        throws Exception
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   106
    {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   107
        out.printf("%n--- timeoutSync requestVersion=%s, method=%s, "
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   108
                   + "connectTimeout=%s, requestTimeout=%s ---%n",
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   109
                   requestVersion, method, connectTimeout, requestTimeout);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   110
        HttpClient client = newClient(connectTimeout);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   111
        HttpRequest request = newRequest(requestVersion, method, requestTimeout);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   112
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   113
        for (int i = 0; i < TIMES; i++) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   114
            out.printf("iteration %d%n", i);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   115
            long startTime = System.nanoTime();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   116
            try {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   117
                HttpResponse<String> resp = client.send(request, BodyHandlers.ofString());
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   118
                printResponse(resp);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   119
                fail("Unexpected response: " + resp);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   120
            } catch (HttpConnectTimeoutException expected) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   121
                long elapsedTime = NANOSECONDS.toMillis(System.nanoTime() - startTime);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   122
                out.printf("Client: received in %d millis%n", elapsedTime);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   123
                out.printf("Client: caught expected HttpConnectTimeoutException: %s%n", expected);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   124
                checkExceptionOrCause(ConnectException.class, expected);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   125
            }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   126
        }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   127
    }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   128
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   129
    //@Test(dataProvider = "variants")
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   130
    protected void timeoutAsync(Version requestVersion,
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   131
                                String method,
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   132
                                Duration connectTimeout,
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   133
                                Duration requestTimeout) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   134
        out.printf("%n--- timeoutAsync requestVersion=%s, method=%s, "
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   135
                        + "connectTimeout=%s, requestTimeout=%s ---%n",
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   136
                   requestVersion, method, connectTimeout, requestTimeout);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   137
        HttpClient client = newClient(connectTimeout);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   138
        HttpRequest request = newRequest(requestVersion, method, requestTimeout);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   139
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   140
        for (int i = 0; i < TIMES; i++) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   141
            out.printf("iteration %d%n", i);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   142
            long startTime = System.nanoTime();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   143
            CompletableFuture<HttpResponse<String>> cf =
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   144
                    client.sendAsync(request, BodyHandlers.ofString());
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   145
            try {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   146
                HttpResponse<String> resp = cf.join();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   147
                printResponse(resp);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   148
                fail("Unexpected response: " + resp);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   149
            } catch (CompletionException ce) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   150
                long elapsedTime = NANOSECONDS.toMillis(System.nanoTime() - startTime);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   151
                out.printf("Client: received in %d millis%n", elapsedTime);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   152
                Throwable expected = ce.getCause();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   153
                if (expected instanceof HttpConnectTimeoutException) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   154
                    out.printf("Client: caught expected HttpConnectTimeoutException: %s%n", expected);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   155
                    checkExceptionOrCause(ConnectException.class, expected);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   156
                } else {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   157
                    out.printf("Client: caught UNEXPECTED exception: %s%n", expected);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   158
                    throw ce;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   159
                }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   160
            }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   161
        }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   162
    }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   163
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   164
    static HttpClient newClient(Duration connectTimeout) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   165
        HttpClient.Builder builder = HttpClient.newBuilder().proxy(NO_PROXY);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   166
        if (connectTimeout != NO_DURATION)
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   167
            builder.connectTimeout(connectTimeout);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   168
        return builder.build();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   169
    }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   170
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   171
    HttpRequest newRequest(Version reqVersion,
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   172
                           String method,
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   173
                           Duration requestTimeout) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   174
        HttpRequest.Builder reqBuilder = HttpRequest.newBuilder(httpsURI);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   175
        reqBuilder = reqBuilder.version(reqVersion);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   176
        switch (method) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   177
            case "GET"   : reqBuilder.GET();                         break;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   178
            case "POST"  : reqBuilder.POST(BodyPublishers.noBody()); break;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   179
            default: throw new AssertionError("Unknown method:" + method);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   180
        }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   181
        if (requestTimeout != NO_DURATION)
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   182
            reqBuilder.timeout(requestTimeout);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   183
        return reqBuilder.build();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   184
    }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   185
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   186
    static void checkExceptionOrCause(Class<? extends Throwable> clazz, Throwable t) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   187
        final Throwable original = t;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   188
        do {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   189
            if (clazz.isInstance(t)) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   190
                System.out.println("Found expected exception/cause: " + t);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   191
                return; // found
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   192
            }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   193
        } while ((t = t.getCause()) != null);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   194
        original.printStackTrace(System.out);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   195
        throw new RuntimeException("Expected " + clazz + "in " + original);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   196
    }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   197
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   198
    static void printResponse(HttpResponse<?> response) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   199
        out.println("Unexpected response: " + response);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   200
        out.println("Headers: " + response.headers());
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   201
        out.println("Body: " + response.body());
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   202
    }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   203
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   204
    // -- Infrastructure
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   205
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   206
    static String serverAuthority(Server server) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   207
        return InetAddress.getLoopbackAddress().getHostName() + ":"
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   208
                + server.getPort();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   209
    }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   210
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   211
    @BeforeTest
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   212
    public void setup() throws Exception {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   213
        server = new Server();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   214
        httpsURI = URI.create("https://" + serverAuthority(server) + "/foo");
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   215
        out.println("HTTPS URI: " + httpsURI);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   216
    }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   217
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   218
    @AfterTest
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   219
    public void teardown() throws Exception {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   220
        server.close();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   221
        out.printf("%n--- teardown ---%n");
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   222
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   223
        int numClientConnections = variants().length * TIMES;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   224
        int serverCount = server.count;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   225
        out.printf("Client made %d connections.%n", numClientConnections);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   226
        out.printf("Server received %d connections.%n", serverCount);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   227
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   228
        // This is usually the case, but not always, do not assert. Remains
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   229
        // as an informative message only.
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   230
        //if (numClientConnections != serverCount)
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   231
        //    fail(format("[numTests: %d] != [serverCount: %d]",
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   232
        //                numClientConnections, serverCount));
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   233
    }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   234
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   235
    /**
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   236
     * Emulates a server-side, using plain cleartext Sockets, that just reads
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   237
     * initial client hello and does nothing more.
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   238
     */
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   239
    static class Server extends Thread implements AutoCloseable {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   240
        private final ServerSocket ss;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   241
        private volatile boolean closed;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   242
        volatile int count;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   243
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   244
        Server() throws IOException {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   245
            super("Server");
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   246
            ss = new ServerSocket();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   247
            ss.setReuseAddress(false);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   248
            ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   249
            this.start();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   250
        }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   251
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   252
        int getPort() {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   253
            return ss.getLocalPort();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   254
        }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   255
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   256
        @Override
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   257
        public void close() {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   258
            if (closed)
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   259
                return;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   260
            closed = true;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   261
            try {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   262
                ss.close();
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   263
            } catch (IOException e) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   264
                throw new UncheckedIOException("Unexpected", e);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   265
            }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   266
        }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   267
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   268
        @Override
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   269
        public void run() {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   270
            while (!closed) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   271
                try (Socket s = ss.accept()) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   272
                    count++;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   273
                    out.println("Server: accepted new connection");
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   274
                    InputStream is = new BufferedInputStream(s.getInputStream());
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   275
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   276
                    out.println("Server: starting to read");
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   277
                    while (is.read() != -1) ;
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   278
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   279
                    out.println("Server: closing connection");
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   280
                    s.close(); // close without giving any reply
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   281
                } catch (IOException e) {
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   282
                    if (!closed)
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   283
                        out.println("UNEXPECTED " + e);
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   284
                }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   285
            }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   286
        }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   287
    }
31d9e82b2e64 8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
diff changeset
   288
}