test/jdk/java/net/httpclient/ThrowingSubscribers.java
author dfuchs
Fri, 13 Apr 2018 15:06:50 +0100
branchhttp-client-branch
changeset 56426 f39b316f10c9
parent 56423 ba64c30666cc
child 56451 9585061fdb04
permissions -rw-r--r--
http-client-branch: review comment - several tests should be setting NO_PROXY when testing without proxy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
     1
/*
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
     4
 *
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
     8
 *
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    14
 *
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    18
 *
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    21
 * questions.
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    22
 */
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    23
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    24
/*
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    25
 * @test
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    26
 * @summary Tests what happens when response body handlers and subscribers
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    27
 *          throw unexpected exceptions.
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    28
 * @library /lib/testlibrary http2/server
56423
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
    29
 * @build jdk.testlibrary.SimpleSSLContext HttpServerAdapters
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
    30
  *       ReferenceTracker ThrowingSubscribers
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    31
 * @modules java.base/sun.net.www.http
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    32
 *          java.net.http/jdk.internal.net.http.common
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    33
 *          java.net.http/jdk.internal.net.http.frame
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    34
 *          java.net.http/jdk.internal.net.http.hpack
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
    35
 * @run testng/othervm -Djdk.internal.httpclient.debug=true ThrowingSubscribers
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    36
 */
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    37
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    38
import com.sun.net.httpserver.HttpServer;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    39
import com.sun.net.httpserver.HttpsConfigurator;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    40
import com.sun.net.httpserver.HttpsServer;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    41
import jdk.testlibrary.SimpleSSLContext;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    42
import org.testng.annotations.AfterTest;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    43
import org.testng.annotations.AfterClass;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    44
import org.testng.annotations.BeforeTest;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    45
import org.testng.annotations.DataProvider;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    46
import org.testng.annotations.Test;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    47
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    48
import javax.net.ssl.SSLContext;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    49
import java.io.BufferedReader;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    50
import java.io.IOException;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    51
import java.io.InputStream;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    52
import java.io.InputStreamReader;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    53
import java.io.OutputStream;
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
    54
import java.io.UncheckedIOException;
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56209
diff changeset
    55
import java.net.InetAddress;
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    56
import java.net.InetSocketAddress;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    57
import java.net.URI;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    58
import java.net.http.HttpClient;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    59
import java.net.http.HttpHeaders;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    60
import java.net.http.HttpRequest;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    61
import java.net.http.HttpResponse;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    62
import java.net.http.HttpResponse.BodyHandler;
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56165
diff changeset
    63
import java.net.http.HttpResponse.BodyHandlers;
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    64
import java.net.http.HttpResponse.BodySubscriber;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    65
import java.nio.ByteBuffer;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    66
import java.nio.charset.StandardCharsets;
56288
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
    67
import java.util.EnumSet;
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    68
import java.util.List;
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
    69
import java.util.concurrent.CompletableFuture;
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    70
import java.util.concurrent.CompletionStage;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    71
import java.util.concurrent.ConcurrentHashMap;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    72
import java.util.concurrent.ConcurrentMap;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    73
import java.util.concurrent.Executor;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    74
import java.util.concurrent.Executors;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    75
import java.util.concurrent.Flow;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    76
import java.util.concurrent.atomic.AtomicLong;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    77
import java.util.function.Consumer;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    78
import java.util.function.Predicate;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    79
import java.util.function.Supplier;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    80
import java.util.stream.Collectors;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    81
import java.util.stream.Stream;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    82
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    83
import static java.lang.System.out;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    84
import static java.lang.String.format;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    85
import static java.nio.charset.StandardCharsets.UTF_8;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    86
import static org.testng.Assert.assertEquals;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    87
import static org.testng.Assert.assertTrue;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    88
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    89
public class ThrowingSubscribers implements HttpServerAdapters {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    90
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    91
    SSLContext sslContext;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    92
    HttpTestServer httpTestServer;    // HTTP/1.1    [ 4 servers ]
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    93
    HttpTestServer httpsTestServer;   // HTTPS/1.1
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    94
    HttpTestServer http2TestServer;   // HTTP/2 ( h2c )
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    95
    HttpTestServer https2TestServer;  // HTTP/2 ( h2  )
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    96
    String httpURI_fixed;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    97
    String httpURI_chunk;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    98
    String httpsURI_fixed;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
    99
    String httpsURI_chunk;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   100
    String http2URI_fixed;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   101
    String http2URI_chunk;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   102
    String https2URI_fixed;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   103
    String https2URI_chunk;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   104
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   105
    static final int ITERATION_COUNT = 1;
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   106
    // a shared executor helps reduce the amount of threads created by the test
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   107
    static final Executor executor = new TestExecutor(Executors.newCachedThreadPool());
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   108
    static final ConcurrentMap<String, Throwable> FAILURES = new ConcurrentHashMap<>();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   109
    static volatile boolean tasksFailed;
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   110
    static final AtomicLong serverCount = new AtomicLong();
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   111
    static final AtomicLong clientCount = new AtomicLong();
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   112
    static final long start = System.nanoTime();
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   113
    public static String now() {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   114
        long now = System.nanoTime() - start;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   115
        long secs = now / 1000_000_000;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   116
        long mill = (now % 1000_000_000) / 1000_000;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   117
        long nan = now % 1000_000;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   118
        return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   119
    }
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   120
56423
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   121
    final ReferenceTracker TRACKER = ReferenceTracker.INSTANCE;
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   122
    private volatile HttpClient sharedClient;
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   123
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   124
    static class TestExecutor implements Executor {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   125
        final AtomicLong tasks = new AtomicLong();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   126
        Executor executor;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   127
        TestExecutor(Executor executor) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   128
            this.executor = executor;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   129
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   130
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   131
        @Override
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   132
        public void execute(Runnable command) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   133
            long id = tasks.incrementAndGet();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   134
            executor.execute(() -> {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   135
                try {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   136
                    command.run();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   137
                } catch (Throwable t) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   138
                    tasksFailed = true;
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   139
                    System.out.printf(now() + "Task %s failed: %s%n", id, t);
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   140
                    System.err.printf(now() + "Task %s failed: %s%n", id, t);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   141
                    FAILURES.putIfAbsent("Task " + id, t);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   142
                    throw t;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   143
                }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   144
            });
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   145
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   146
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   147
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   148
    @AfterClass
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   149
    static final void printFailedTests() {
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   150
        out.println("\n=========================");
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   151
        try {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   152
            out.printf("%n%sCreated %d servers and %d clients%n",
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   153
                    now(), serverCount.get(), clientCount.get());
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   154
            if (FAILURES.isEmpty()) return;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   155
            out.println("Failed tests: ");
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   156
            FAILURES.entrySet().forEach((e) -> {
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   157
                out.printf("\t%s: %s%n", e.getKey(), e.getValue());
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56167
diff changeset
   158
                e.getValue().printStackTrace(out);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   159
                e.getValue().printStackTrace();
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   160
            });
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   161
            if (tasksFailed) {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   162
                System.out.println("WARNING: Some tasks failed");
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   163
            }
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   164
        } finally {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   165
            out.println("\n=========================\n");
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   166
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   167
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   168
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   169
    private String[] uris() {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   170
        return new String[] {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   171
                httpURI_fixed,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   172
                httpURI_chunk,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   173
                httpsURI_fixed,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   174
                httpsURI_chunk,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   175
                http2URI_fixed,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   176
                http2URI_chunk,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   177
                https2URI_fixed,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   178
                https2URI_chunk,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   179
        };
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   180
    }
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   181
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   182
    @DataProvider(name = "noThrows")
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   183
    public Object[][] noThrows() {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   184
        String[] uris = uris();
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   185
        Object[][] result = new Object[uris.length * 2][];
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   186
        int i = 0;
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   187
        for (boolean sameClient : List.of(false, true)) {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   188
            for (String uri: uris()) {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   189
                result[i++] = new Object[] {uri, sameClient};
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   190
            }
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   191
        }
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   192
        assert i == uris.length * 2;
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   193
        return result;
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   194
    }
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   195
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   196
    @DataProvider(name = "variants")
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   197
    public Object[][] variants() {
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   198
        String[] uris = uris();
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   199
        Object[][] result = new Object[uris.length * 2 * 2][];
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   200
        int i = 0;
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   201
        for (Thrower thrower : List.of(
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   202
                new UncheckedIOExceptionThrower(),
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   203
                new UncheckedCustomExceptionThrower())) {
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   204
            for (boolean sameClient : List.of(false, true)) {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   205
                for (String uri : uris()) {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   206
                    result[i++] = new Object[]{uri, sameClient, thrower};
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   207
                }
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   208
            }
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   209
        }
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   210
        assert i == uris.length * 2 * 2;
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   211
        return result;
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   212
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   213
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   214
    private HttpClient makeNewClient() {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   215
        clientCount.incrementAndGet();
56423
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   216
        HttpClient client =  HttpClient.newBuilder()
56426
f39b316f10c9 http-client-branch: review comment - several tests should be setting NO_PROXY when testing without proxy
dfuchs
parents: 56423
diff changeset
   217
                .proxy(HttpClient.Builder.NO_PROXY)
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   218
                .executor(executor)
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   219
                .sslContext(sslContext)
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   220
                .build();
56423
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   221
        return TRACKER.track(client);
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   222
    }
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   223
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   224
    HttpClient newHttpClient(boolean share) {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   225
        if (!share) return makeNewClient();
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   226
        HttpClient shared = sharedClient;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   227
        if (shared != null) return shared;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   228
        synchronized (this) {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   229
            shared = sharedClient;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   230
            if (shared == null) {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   231
                shared = sharedClient = makeNewClient();
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   232
            }
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   233
            return shared;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   234
        }
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   235
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   236
56288
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   237
    enum SubscriberType {
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   238
        INLINE,  // In line subscribers complete their CF on ON_COMPLETE
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   239
                 // e.g. BodySubscribers::ofString
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   240
        OFFLINE; // Off line subscribers complete their CF immediately
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   241
                 // but require the client to pull the data after the
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   242
                 // CF completes (e.g. BodySubscribers::ofInputStream)
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   243
    }
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   244
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   245
    static EnumSet<Where> excludes(SubscriberType type) {
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   246
        EnumSet<Where> set = EnumSet.noneOf(Where.class);
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   247
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   248
        if (type == SubscriberType.OFFLINE) {
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   249
            // Throwing on onSubscribe needs some more work
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   250
            // for the case of InputStream, where the body has already
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   251
            // completed by the time the subscriber is subscribed.
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   252
            // The only way we have at that point to relay the exception
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   253
            // is to call onError on the subscriber, but should we if
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   254
            // Subscriber::onSubscribed has thrown an exception and
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   255
            // not completed normally?
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   256
            set.add(Where.ON_SUBSCRIBE);
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   257
        }
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   258
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   259
        // Don't know how to make the stack reliably cause onError
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   260
        // to be called without closing the connection.
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   261
        // And how do we get the exception if onError throws anyway?
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   262
        set.add(Where.ON_ERROR);
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   263
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   264
        return set;
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   265
    }
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   266
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   267
    @Test(dataProvider = "noThrows")
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   268
    public void testNoThrows(String uri, boolean sameClient)
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   269
            throws Exception {
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   270
        HttpClient client = null;
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   271
        out.printf("%ntestNoThrows(%s, %b)%n", uri, sameClient);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   272
        for (int i=0; i< ITERATION_COUNT; i++) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   273
            if (!sameClient || client == null)
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   274
                client = newHttpClient(sameClient);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   275
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   276
            HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   277
                    .build();
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   278
            BodyHandler<String> handler =
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   279
                    new ThrowingBodyHandler((w) -> {},
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56165
diff changeset
   280
                                            BodyHandlers.ofString());
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   281
            HttpResponse<String> response = client.send(req, handler);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   282
            String body = response.body();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   283
            assertEquals(URI.create(body).getPath(), URI.create(uri).getPath());
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   284
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   285
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   286
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   287
    @Test(dataProvider = "variants")
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   288
    public void testThrowingAsString(String uri,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   289
                                     boolean sameClient,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   290
                                     Thrower thrower)
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   291
            throws Exception
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   292
    {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   293
        String test = format("testThrowingAsString(%s, %b, %s)",
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   294
                             uri, sameClient, thrower);
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56165
diff changeset
   295
        testThrowing(test, uri, sameClient, BodyHandlers::ofString,
56288
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   296
                this::shouldHaveThrown, thrower,false,
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   297
                excludes(SubscriberType.INLINE));
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   298
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   299
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   300
    @Test(dataProvider = "variants")
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   301
    public void testThrowingAsLines(String uri,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   302
                                    boolean sameClient,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   303
                                    Thrower thrower)
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   304
            throws Exception
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   305
    {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   306
        String test =  format("testThrowingAsLines(%s, %b, %s)",
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   307
                uri, sameClient, thrower);
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56165
diff changeset
   308
        testThrowing(test, uri, sameClient, BodyHandlers::ofLines,
56288
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   309
                this::checkAsLines, thrower,false,
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   310
                excludes(SubscriberType.OFFLINE));
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   311
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   312
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   313
    @Test(dataProvider = "variants")
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   314
    public void testThrowingAsInputStream(String uri,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   315
                                          boolean sameClient,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   316
                                          Thrower thrower)
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   317
            throws Exception
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   318
    {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   319
        String test = format("testThrowingAsInputStream(%s, %b, %s)",
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   320
                uri, sameClient, thrower);
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56165
diff changeset
   321
        testThrowing(test, uri, sameClient, BodyHandlers::ofInputStream,
56288
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   322
                this::checkAsInputStream,  thrower,false,
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   323
                excludes(SubscriberType.OFFLINE));
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   324
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   325
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   326
    @Test(dataProvider = "variants")
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   327
    public void testThrowingAsStringAsync(String uri,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   328
                                          boolean sameClient,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   329
                                          Thrower thrower)
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   330
            throws Exception
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   331
    {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   332
        String test = format("testThrowingAsStringAsync(%s, %b, %s)",
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   333
                uri, sameClient, thrower);
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56165
diff changeset
   334
        testThrowing(test, uri, sameClient, BodyHandlers::ofString,
56288
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   335
                     this::shouldHaveThrown, thrower, true,
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   336
                excludes(SubscriberType.INLINE));
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   337
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   338
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   339
    @Test(dataProvider = "variants")
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   340
    public void testThrowingAsLinesAsync(String uri,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   341
                                         boolean sameClient,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   342
                                         Thrower thrower)
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   343
            throws Exception
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   344
    {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   345
        String test = format("testThrowingAsLinesAsync(%s, %b, %s)",
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   346
                uri, sameClient, thrower);
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56165
diff changeset
   347
        testThrowing(test, uri, sameClient, BodyHandlers::ofLines,
56288
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   348
                this::checkAsLines, thrower,true,
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   349
                excludes(SubscriberType.OFFLINE));
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   350
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   351
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   352
    @Test(dataProvider = "variants")
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   353
    public void testThrowingAsInputStreamAsync(String uri,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   354
                                               boolean sameClient,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   355
                                               Thrower thrower)
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   356
            throws Exception
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   357
    {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   358
        String test = format("testThrowingAsInputStreamAsync(%s, %b, %s)",
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   359
                uri, sameClient, thrower);
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56165
diff changeset
   360
        testThrowing(test, uri, sameClient, BodyHandlers::ofInputStream,
56288
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   361
                this::checkAsInputStream, thrower,true,
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   362
                excludes(SubscriberType.OFFLINE));
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   363
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   364
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   365
    private <T,U> void testThrowing(String name, String uri, boolean sameClient,
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   366
                                    Supplier<BodyHandler<T>> handlers,
56288
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   367
                                    Finisher finisher, Thrower thrower,
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   368
                                    boolean async, EnumSet<Where> excludes)
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   369
            throws Exception
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   370
    {
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   371
        out.printf("%n%s%s%n", now(), name);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   372
        try {
56288
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   373
            testThrowing(uri, sameClient, handlers, finisher, thrower, async, excludes);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   374
        } catch (Error | Exception x) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   375
            FAILURES.putIfAbsent(name, x);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   376
            throw x;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   377
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   378
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   379
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   380
    private <T,U> void testThrowing(String uri, boolean sameClient,
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   381
                                    Supplier<BodyHandler<T>> handlers,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   382
                                    Finisher finisher, Thrower thrower,
56288
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   383
                                    boolean async,
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   384
                                    EnumSet<Where> excludes)
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   385
            throws Exception
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   386
    {
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   387
        HttpClient client = null;
56288
2de1aa88cf06 http-client-branch: fix for subscribers throwing in onSubscribe
dfuchs
parents: 56286
diff changeset
   388
        for (Where where : EnumSet.complementOf(excludes)) {
56286
3d8333fa243b http-client-branch: added some comments to test/jdk/java/net/httpclient/ThrowingSubscribers.java
dfuchs
parents: 56273
diff changeset
   389
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   390
            if (!sameClient || client == null)
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   391
                client = newHttpClient(sameClient);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   392
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   393
            HttpRequest req = HttpRequest.
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   394
                    newBuilder(URI.create(uri))
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   395
                    .build();
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   396
            BodyHandler<T> handler =
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   397
                    new ThrowingBodyHandler(where.select(thrower), handlers.get());
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   398
            System.out.println("try throwing in " + where);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   399
            HttpResponse<T> response = null;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   400
            if (async) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   401
                try {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   402
                    response = client.sendAsync(req, handler).join();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   403
                } catch (Error | Exception x) {
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   404
                    Throwable cause = findCause(x, thrower);
56273
329e410c9a01 http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents: 56265
diff changeset
   405
                    if (cause == null) throw causeNotFound(where, x);
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   406
                    System.out.println(now() + "Got expected exception: " + cause);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   407
                }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   408
            } else {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   409
                try {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   410
                    response = client.send(req, handler);
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   411
                } catch (Error | Exception t) {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   412
                    if (thrower.test(t)) {
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   413
                        System.out.println(now() + "Got expected exception: " + t);
56273
329e410c9a01 http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents: 56265
diff changeset
   414
                    } else throw causeNotFound(where, t);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   415
                }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   416
            }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   417
            if (response != null) {
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   418
                finisher.finish(where, response, thrower);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   419
            }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   420
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   421
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   422
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   423
    enum Where {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   424
        BODY_HANDLER, ON_SUBSCRIBE, ON_NEXT, ON_COMPLETE, ON_ERROR, GET_BODY, BODY_CF;
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   425
        public Consumer<Where> select(Consumer<Where> consumer) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   426
            return new Consumer<Where>() {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   427
                @Override
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   428
                public void accept(Where where) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   429
                    if (Where.this == where) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   430
                        consumer.accept(where);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   431
                    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   432
                }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   433
            };
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   434
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   435
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   436
56273
329e410c9a01 http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents: 56265
diff changeset
   437
    static AssertionError causeNotFound(Where w, Throwable t) {
329e410c9a01 http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents: 56265
diff changeset
   438
        return new AssertionError("Expected exception not found in " + w, t);
329e410c9a01 http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents: 56265
diff changeset
   439
    }
329e410c9a01 http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents: 56265
diff changeset
   440
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   441
    interface Thrower extends Consumer<Where>, Predicate<Throwable> {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   442
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   443
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   444
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   445
    interface Finisher<T,U> {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   446
        U finish(Where w, HttpResponse<T> resp, Thrower thrower) throws IOException;
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   447
    }
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   448
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   449
    final <T,U> U shouldHaveThrown(Where w, HttpResponse<T> resp, Thrower thrower) {
56273
329e410c9a01 http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents: 56265
diff changeset
   450
        String msg = "Expected exception not thrown in " + w
329e410c9a01 http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents: 56265
diff changeset
   451
                + "\n\tReceived: " + resp
329e410c9a01 http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents: 56265
diff changeset
   452
                + "\n\tWith body: " + resp.body();
329e410c9a01 http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents: 56265
diff changeset
   453
        System.out.println(msg);
329e410c9a01 http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents: 56265
diff changeset
   454
        throw new RuntimeException(msg);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   455
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   456
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   457
    final List<String> checkAsLines(Where w, HttpResponse<Stream<String>> resp, Thrower thrower) {
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   458
        switch(w) {
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   459
            case BODY_HANDLER: return shouldHaveThrown(w, resp, thrower);
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   460
            case GET_BODY: return shouldHaveThrown(w, resp, thrower);
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   461
            case BODY_CF: return shouldHaveThrown(w, resp, thrower);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   462
            default: break;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   463
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   464
        List<String> result = null;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   465
        try {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   466
            result = resp.body().collect(Collectors.toList());
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   467
        } catch (Error | Exception x) {
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   468
            Throwable cause = findCause(x, thrower);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   469
            if (cause != null) {
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   470
                out.println(now() + "Got expected exception in " + w + ": " + cause);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   471
                return result;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   472
            }
56273
329e410c9a01 http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents: 56265
diff changeset
   473
            throw causeNotFound(w, x);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   474
        }
56399
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56288
diff changeset
   475
        return shouldHaveThrown(w, resp, thrower);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   476
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   477
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   478
    final List<String> checkAsInputStream(Where w, HttpResponse<InputStream> resp,
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   479
                                    Thrower thrower)
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   480
            throws IOException
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   481
    {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   482
        switch(w) {
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   483
            case BODY_HANDLER: return shouldHaveThrown(w, resp, thrower);
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   484
            case GET_BODY: return shouldHaveThrown(w, resp, thrower);
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   485
            case BODY_CF: return shouldHaveThrown(w, resp, thrower);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   486
            default: break;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   487
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   488
        List<String> result = null;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   489
        try (InputStreamReader r1 = new InputStreamReader(resp.body(), UTF_8);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   490
             BufferedReader r = new BufferedReader(r1)) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   491
            try {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   492
                result = r.lines().collect(Collectors.toList());
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   493
            } catch (Error | Exception x) {
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   494
                Throwable cause = findCause(x, thrower);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   495
                if (cause != null) {
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   496
                    out.println(now() + "Got expected exception in " + w + ": " + cause);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   497
                    return result;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   498
                }
56273
329e410c9a01 http-client-branch: improved failure report in ThrowingSubscribers
dfuchs
parents: 56265
diff changeset
   499
                throw causeNotFound(w, x);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   500
            }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   501
        }
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   502
        return shouldHaveThrown(w, resp, thrower);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   503
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   504
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   505
    private static Throwable findCause(Throwable x,
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   506
                                       Predicate<Throwable> filter) {
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   507
        while (x != null && !filter.test(x)) x = x.getCause();
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   508
        return x;
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   509
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   510
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   511
    static final class UncheckedCustomExceptionThrower implements Thrower {
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   512
        @Override
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   513
        public void accept(Where where) {
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   514
            out.println(now() + "Throwing in " + where);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   515
            throw new UncheckedCustomException(where.name());
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   516
        }
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   517
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   518
        @Override
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   519
        public boolean test(Throwable throwable) {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   520
            return UncheckedCustomException.class.isInstance(throwable);
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   521
        }
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   522
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   523
        @Override
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   524
        public String toString() {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   525
            return "UncheckedCustomExceptionThrower";
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   526
        }
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   527
    }
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   528
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   529
    static final class UncheckedIOExceptionThrower implements Thrower {
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   530
        @Override
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   531
        public void accept(Where where) {
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   532
            out.println(now() + "Throwing in " + where);
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   533
            throw new UncheckedIOException(new CustomIOException(where.name()));
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   534
        }
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   535
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   536
        @Override
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   537
        public boolean test(Throwable throwable) {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   538
            return UncheckedIOException.class.isInstance(throwable)
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   539
                    && CustomIOException.class.isInstance(throwable.getCause());
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   540
        }
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   541
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   542
        @Override
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   543
        public String toString() {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   544
            return "UncheckedIOExceptionThrower";
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   545
        }
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   546
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   547
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   548
    static final class UncheckedCustomException extends RuntimeException {
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   549
        UncheckedCustomException(String message) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   550
            super(message);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   551
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   552
        UncheckedCustomException(String message, Throwable cause) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   553
            super(message, cause);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   554
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   555
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   556
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   557
    static final class CustomIOException extends IOException {
56134
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   558
        CustomIOException(String message) {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   559
            super(message);
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   560
        }
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   561
        CustomIOException(String message, Throwable cause) {
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   562
            super(message, cause);
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   563
        }
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   564
    }
2fc9edf5628d http-client-branch: Extend ThrowingSubscribers.java to test several exception classes
dfuchs
parents: 56133
diff changeset
   565
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   566
    static final class ThrowingBodyHandler<T> implements BodyHandler<T> {
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   567
        final Consumer<Where> throwing;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   568
        final BodyHandler<T> bodyHandler;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   569
        ThrowingBodyHandler(Consumer<Where> throwing, BodyHandler<T> bodyHandler) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   570
            this.throwing = throwing;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   571
            this.bodyHandler = bodyHandler;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   572
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   573
        @Override
56410
1b37529eaf3a http-client-branch: 8201312: More evolution friendly BodyHandler API
michaelm
parents: 56399
diff changeset
   574
        public BodySubscriber<T> apply(HttpResponse.ResponseInfo rinfo) {
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   575
            throwing.accept(Where.BODY_HANDLER);
56410
1b37529eaf3a http-client-branch: 8201312: More evolution friendly BodyHandler API
michaelm
parents: 56399
diff changeset
   576
            BodySubscriber<T> subscriber = bodyHandler.apply(rinfo);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   577
            return new ThrowingBodySubscriber(throwing, subscriber);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   578
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   579
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   580
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   581
    static final class ThrowingBodySubscriber<T> implements BodySubscriber<T> {
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   582
        private final BodySubscriber<T> subscriber;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   583
        volatile boolean onSubscribeCalled;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   584
        final Consumer<Where> throwing;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   585
        ThrowingBodySubscriber(Consumer<Where> throwing, BodySubscriber<T> subscriber) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   586
            this.throwing = throwing;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   587
            this.subscriber = subscriber;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   588
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   589
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   590
        @Override
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   591
        public void onSubscribe(Flow.Subscription subscription) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   592
            //out.println("onSubscribe ");
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   593
            onSubscribeCalled = true;
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   594
            throwing.accept(Where.ON_SUBSCRIBE);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   595
            subscriber.onSubscribe(subscription);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   596
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   597
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   598
        @Override
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   599
        public void onNext(List<ByteBuffer> item) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   600
           // out.println("onNext " + item);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   601
            assertTrue(onSubscribeCalled);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   602
            throwing.accept(Where.ON_NEXT);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   603
            subscriber.onNext(item);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   604
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   605
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   606
        @Override
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   607
        public void onError(Throwable throwable) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   608
            //out.println("onError");
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   609
            assertTrue(onSubscribeCalled);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   610
            throwing.accept(Where.ON_ERROR);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   611
            subscriber.onError(throwable);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   612
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   613
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   614
        @Override
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   615
        public void onComplete() {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   616
            //out.println("onComplete");
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   617
            assertTrue(onSubscribeCalled, "onComplete called before onSubscribe");
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   618
            throwing.accept(Where.ON_COMPLETE);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   619
            subscriber.onComplete();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   620
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   621
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   622
        @Override
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   623
        public CompletionStage<T> getBody() {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   624
            throwing.accept(Where.GET_BODY);
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   625
            try {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   626
                throwing.accept(Where.BODY_CF);
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   627
            } catch (Throwable t) {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   628
                return CompletableFuture.failedFuture(t);
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   629
            }
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   630
            return subscriber.getBody();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   631
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   632
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   633
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   634
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   635
    @BeforeTest
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   636
    public void setup() throws Exception {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   637
        sslContext = new SimpleSSLContext().get();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   638
        if (sslContext == null)
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   639
            throw new AssertionError("Unexpected null sslContext");
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   640
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   641
        // HTTP/1.1
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   642
        HttpTestHandler h1_fixedLengthHandler = new HTTP_FixedLengthHandler();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   643
        HttpTestHandler h1_chunkHandler = new HTTP_ChunkedHandler();
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56209
diff changeset
   644
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   645
        httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   646
        httpTestServer.addHandler(h1_fixedLengthHandler, "/http1/fixed");
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   647
        httpTestServer.addHandler(h1_chunkHandler, "/http1/chunk");
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56209
diff changeset
   648
        httpURI_fixed = "http://" + httpTestServer.serverAuthority() + "/http1/fixed/x";
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56209
diff changeset
   649
        httpURI_chunk = "http://" + httpTestServer.serverAuthority() + "/http1/chunk/x";
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   650
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   651
        HttpsServer httpsServer = HttpsServer.create(sa, 0);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   652
        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   653
        httpsTestServer = HttpTestServer.of(httpsServer);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   654
        httpsTestServer.addHandler(h1_fixedLengthHandler, "/https1/fixed");
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   655
        httpsTestServer.addHandler(h1_chunkHandler, "/https1/chunk");
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56209
diff changeset
   656
        httpsURI_fixed = "https://" + httpsTestServer.serverAuthority() + "/https1/fixed/x";
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56209
diff changeset
   657
        httpsURI_chunk = "https://" + httpsTestServer.serverAuthority() + "/https1/chunk/x";
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   658
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   659
        // HTTP/2
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   660
        HttpTestHandler h2_fixedLengthHandler = new HTTP_FixedLengthHandler();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   661
        HttpTestHandler h2_chunkedHandler = new HTTP_ChunkedHandler();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   662
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56209
diff changeset
   663
        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   664
        http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   665
        http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56209
diff changeset
   666
        http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed/x";
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56209
diff changeset
   667
        http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk/x";
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   668
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56209
diff changeset
   669
        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, 0));
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   670
        https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   671
        https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56209
diff changeset
   672
        https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed/x";
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56209
diff changeset
   673
        https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/chunk/x";
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   674
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   675
        serverCount.addAndGet(4);
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   676
        httpTestServer.start();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   677
        httpsTestServer.start();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   678
        http2TestServer.start();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   679
        https2TestServer.start();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   680
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   681
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   682
    @AfterTest
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   683
    public void teardown() throws Exception {
56423
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   684
        String sharedClientName =
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   685
                sharedClient == null ? null : sharedClient.toString();
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56134
diff changeset
   686
        sharedClient = null;
56423
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   687
        Thread.sleep(100);
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   688
        AssertionError fail = TRACKER.check(500);
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   689
        try {
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   690
            httpTestServer.stop();
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   691
            httpsTestServer.stop();
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   692
            http2TestServer.stop();
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   693
            https2TestServer.stop();
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   694
        } finally {
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   695
            if (fail != null) {
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   696
                if (sharedClientName != null) {
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   697
                    System.err.println("Shared client name is: " + sharedClientName);
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   698
                }
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   699
                throw fail;
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   700
            }
ba64c30666cc http-client-branch: add test coverage and better diagnosis for HttpClientImpl operations ref count
dfuchs
parents: 56410
diff changeset
   701
        }
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   702
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   703
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   704
    static class HTTP_FixedLengthHandler implements HttpTestHandler {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   705
        @Override
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   706
        public void handle(HttpTestExchange t) throws IOException {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   707
            out.println("HTTP_FixedLengthHandler received request to " + t.getRequestURI());
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   708
            try (InputStream is = t.getRequestBody()) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   709
                is.readAllBytes();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   710
            }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   711
            byte[] resp = t.getRequestURI().toString().getBytes(StandardCharsets.UTF_8);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   712
            t.sendResponseHeaders(200, resp.length);  //fixed content length
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   713
            try (OutputStream os = t.getResponseBody()) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   714
                os.write(resp);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   715
            }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   716
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   717
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   718
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   719
    static class HTTP_ChunkedHandler implements HttpTestHandler {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   720
        @Override
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   721
        public void handle(HttpTestExchange t) throws IOException {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   722
            out.println("HTTP_ChunkedHandler received request to " + t.getRequestURI());
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   723
            byte[] resp = t.getRequestURI().toString().getBytes(StandardCharsets.UTF_8);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   724
            try (InputStream is = t.getRequestBody()) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   725
                is.readAllBytes();
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   726
            }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   727
            t.sendResponseHeaders(200, -1); // chunked/variable
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   728
            try (OutputStream os = t.getResponseBody()) {
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   729
                os.write(resp);
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   730
            }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   731
        }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   732
    }
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   733
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents:
diff changeset
   734
}