test/jdk/java/net/httpclient/ThrowingPublishers.java
author chegar
Thu, 08 Mar 2018 17:42:16 +0000
branchhttp-client-branch
changeset 56265 ec34ae013fbe
parent 56218 fd7bd32963ef
child 56423 ba64c30666cc
permissions -rw-r--r--
http-client-branch: tests should bind to the loopback only
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
     1
/*
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
     4
 *
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
     8
 *
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    14
 *
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    18
 *
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    21
 * questions.
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    22
 */
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    23
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    24
/*
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    25
 * @test
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56218
diff changeset
    26
 * @summary Tests what happens when request publishers
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    27
 *          throw unexpected exceptions.
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    28
 * @library /lib/testlibrary http2/server
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    29
 * @build jdk.testlibrary.SimpleSSLContext HttpServerAdapters ThrowingPublishers
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    30
 * @modules java.base/sun.net.www.http
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.common
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    32
 *          java.net.http/jdk.internal.net.http.frame
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    33
 *          java.net.http/jdk.internal.net.http.hpack
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    34
 * @run testng/othervm -Djdk.internal.httpclient.debug=true ThrowingPublishers
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    35
 */
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    36
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    37
import com.sun.net.httpserver.HttpServer;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    38
import com.sun.net.httpserver.HttpsConfigurator;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    39
import com.sun.net.httpserver.HttpsServer;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    40
import jdk.testlibrary.SimpleSSLContext;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    41
import org.testng.annotations.AfterClass;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    42
import org.testng.annotations.AfterTest;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    43
import org.testng.annotations.BeforeTest;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    44
import org.testng.annotations.DataProvider;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    45
import org.testng.annotations.Test;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    46
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    47
import javax.net.ssl.SSLContext;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    48
import java.io.BufferedReader;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    49
import java.io.IOException;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    50
import java.io.InputStream;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    51
import java.io.InputStreamReader;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    52
import java.io.OutputStream;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    53
import java.io.UncheckedIOException;
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56218
diff changeset
    54
import java.net.InetAddress;
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    55
import java.net.InetSocketAddress;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    56
import java.net.URI;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    57
import java.net.http.HttpClient;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    58
import java.net.http.HttpRequest;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    59
import java.net.http.HttpRequest.BodyPublisher;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    60
import java.net.http.HttpRequest.BodyPublishers;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    61
import java.net.http.HttpResponse;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    62
import java.net.http.HttpResponse.BodyHandler;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    63
import java.net.http.HttpResponse.BodyHandlers;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    64
import java.nio.ByteBuffer;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    65
import java.nio.charset.StandardCharsets;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    66
import java.util.List;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    67
import java.util.concurrent.CompletableFuture;
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
    68
import java.util.concurrent.CompletionException;
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    69
import java.util.concurrent.ConcurrentHashMap;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    70
import java.util.concurrent.ConcurrentMap;
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
    71
import java.util.concurrent.ExecutionException;
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    72
import java.util.concurrent.Executor;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    73
import java.util.concurrent.Executors;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    74
import java.util.concurrent.Flow;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    75
import java.util.concurrent.SubmissionPublisher;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    76
import java.util.concurrent.atomic.AtomicLong;
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
    77
import java.util.function.BiPredicate;
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    78
import java.util.function.Consumer;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    79
import java.util.function.Supplier;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    80
import java.util.stream.Collectors;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    81
import java.util.stream.Stream;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    82
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    83
import static java.lang.String.format;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    84
import static java.lang.System.out;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    85
import static java.nio.charset.StandardCharsets.UTF_8;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    86
import static org.testng.Assert.assertEquals;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    87
import static org.testng.Assert.assertTrue;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    88
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    89
public class ThrowingPublishers implements HttpServerAdapters {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    90
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    91
    SSLContext sslContext;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    92
    HttpTestServer httpTestServer;    // HTTP/1.1    [ 4 servers ]
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    93
    HttpTestServer httpsTestServer;   // HTTPS/1.1
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    94
    HttpTestServer http2TestServer;   // HTTP/2 ( h2c )
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    95
    HttpTestServer https2TestServer;  // HTTP/2 ( h2  )
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    96
    String httpURI_fixed;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    97
    String httpURI_chunk;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    98
    String httpsURI_fixed;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    99
    String httpsURI_chunk;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   100
    String http2URI_fixed;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   101
    String http2URI_chunk;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   102
    String https2URI_fixed;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   103
    String https2URI_chunk;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   104
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   105
    static final int ITERATION_COUNT = 1;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   106
    // a shared executor helps reduce the amount of threads created by the test
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   107
    static final Executor executor = new TestExecutor(Executors.newCachedThreadPool());
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   108
    static final ConcurrentMap<String, Throwable> FAILURES = new ConcurrentHashMap<>();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   109
    static volatile boolean tasksFailed;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   110
    static final AtomicLong serverCount = new AtomicLong();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   111
    static final AtomicLong clientCount = new AtomicLong();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   112
    static final long start = System.nanoTime();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   113
    public static String now() {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   114
        long now = System.nanoTime() - start;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   115
        long secs = now / 1000_000_000;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   116
        long mill = (now % 1000_000_000) / 1000_000;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   117
        long nan = now % 1000_000;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   118
        return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   119
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   120
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   121
    private volatile HttpClient sharedClient;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   122
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   123
    static class TestExecutor implements Executor {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   124
        final AtomicLong tasks = new AtomicLong();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   125
        Executor executor;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   126
        TestExecutor(Executor executor) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   127
            this.executor = executor;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   128
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   129
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   130
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   131
        public void execute(Runnable command) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   132
            long id = tasks.incrementAndGet();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   133
            executor.execute(() -> {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   134
                try {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   135
                    command.run();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   136
                } catch (Throwable t) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   137
                    tasksFailed = true;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   138
                    System.out.printf(now() + "Task %s failed: %s%n", id, t);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   139
                    System.err.printf(now() + "Task %s failed: %s%n", id, t);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   140
                    FAILURES.putIfAbsent("Task " + id, t);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   141
                    throw t;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   142
                }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   143
            });
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   144
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   145
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   146
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   147
    @AfterClass
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   148
    static final void printFailedTests() {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   149
        out.println("\n=========================");
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   150
        try {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   151
            out.printf("%n%sCreated %d servers and %d clients%n",
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   152
                    now(), serverCount.get(), clientCount.get());
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   153
            if (FAILURES.isEmpty()) return;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   154
            out.println("Failed tests: ");
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   155
            FAILURES.entrySet().forEach((e) -> {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   156
                out.printf("\t%s: %s%n", e.getKey(), e.getValue());
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   157
                e.getValue().printStackTrace(out);
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   158
            });
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   159
            if (tasksFailed) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   160
                System.out.println("WARNING: Some tasks failed");
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   161
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   162
        } finally {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   163
            out.println("\n=========================\n");
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   164
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   165
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   166
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   167
    private String[] uris() {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   168
        return new String[] {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   169
                httpURI_fixed,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   170
                httpURI_chunk,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   171
                httpsURI_fixed,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   172
                httpsURI_chunk,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   173
                http2URI_fixed,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   174
                http2URI_chunk,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   175
                https2URI_fixed,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   176
                https2URI_chunk,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   177
        };
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   178
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   179
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   180
    @DataProvider(name = "noThrows")
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   181
    public Object[][] noThrows() {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   182
        String[] uris = uris();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   183
        Object[][] result = new Object[uris.length * 2][];
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   184
        //Object[][] result = new Object[uris.length][];
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   185
        int i = 0;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   186
        for (boolean sameClient : List.of(false, true)) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   187
            //if (!sameClient) continue;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   188
            for (String uri: uris()) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   189
                result[i++] = new Object[] {uri, sameClient};
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   190
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   191
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   192
        assert i == uris.length * 2;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   193
        // assert i == uris.length ;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   194
        return result;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   195
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   196
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   197
    @DataProvider(name = "variants")
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   198
    public Object[][] variants() {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   199
        String[] uris = uris();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   200
        Object[][] result = new Object[uris.length * 2 * 2][];
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   201
        //Object[][] result = new Object[(uris.length/2) * 2 * 2][];
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   202
        int i = 0;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   203
        for (Thrower thrower : List.of(
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   204
                new UncheckedIOExceptionThrower(),
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   205
                new UncheckedCustomExceptionThrower())) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   206
            for (boolean sameClient : List.of(false, true)) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   207
                for (String uri : uris()) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   208
                    // if (uri.contains("http2") || uri.contains("https2")) continue;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   209
                    // if (!sameClient) continue;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   210
                    result[i++] = new Object[]{uri, sameClient, thrower};
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   211
                }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   212
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   213
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   214
        assert i == uris.length * 2 * 2;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   215
        //assert Stream.of(result).filter(o -> o != null).count() == result.length;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   216
        return result;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   217
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   218
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   219
    private HttpClient makeNewClient() {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   220
        clientCount.incrementAndGet();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   221
        return HttpClient.newBuilder()
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   222
                .executor(executor)
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   223
                .sslContext(sslContext)
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   224
                .build();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   225
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   226
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   227
    HttpClient newHttpClient(boolean share) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   228
        if (!share) return makeNewClient();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   229
        HttpClient shared = sharedClient;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   230
        if (shared != null) return shared;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   231
        synchronized (this) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   232
            shared = sharedClient;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   233
            if (shared == null) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   234
                shared = sharedClient = makeNewClient();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   235
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   236
            return shared;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   237
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   238
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   239
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   240
    final String BODY = "Some string | that ? can | be split ? several | ways.";
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   241
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   242
    @Test(dataProvider = "noThrows")
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   243
    public void testNoThrows(String uri, boolean sameClient)
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   244
            throws Exception {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   245
        HttpClient client = null;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   246
        out.printf("%n%s testNoThrows(%s, %b)%n", now(), uri, sameClient);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   247
        for (int i=0; i< ITERATION_COUNT; i++) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   248
            if (!sameClient || client == null)
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   249
                client = newHttpClient(sameClient);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   250
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   251
            SubmissionPublisher<ByteBuffer> publisher
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   252
                    = new SubmissionPublisher<>(executor,10);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   253
            ThrowingBodyPublisher bodyPublisher = new ThrowingBodyPublisher((w) -> {},
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   254
                    BodyPublishers.fromPublisher(publisher));
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   255
            CompletableFuture<Void> subscribedCF = bodyPublisher.subscribedCF();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   256
            subscribedCF.whenComplete((r,t) -> System.out.println(now() + " subscribe completed " + t))
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   257
                    .thenAcceptAsync((v) -> {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   258
                                Stream.of(BODY.split("\\|"))
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   259
                                        .forEachOrdered(s -> {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   260
                                                System.out.println("submitting \"" + s +"\"");
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   261
                                                publisher.submit(ByteBuffer.wrap(s.getBytes(StandardCharsets.UTF_8)));
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   262
                                        });
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   263
                                System.out.println("publishing done");
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   264
                                publisher.close();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   265
                            },
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   266
                    executor);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   267
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   268
            HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   269
                    .POST(bodyPublisher)
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   270
                    .build();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   271
            BodyHandler<String> handler = BodyHandlers.ofString();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   272
            CompletableFuture<HttpResponse<String>> response = client.sendAsync(req, handler);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   273
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   274
            String body = response.join().body();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   275
            assertEquals(body, Stream.of(BODY.split("\\|")).collect(Collectors.joining()));
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   276
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   277
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   278
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   279
    @Test(dataProvider = "variants")
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   280
    public void testThrowingAsString(String uri,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   281
                                     boolean sameClient,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   282
                                     Thrower thrower)
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   283
            throws Exception
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   284
    {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   285
        String test = format("testThrowingAsString(%s, %b, %s)",
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   286
                             uri, sameClient, thrower);
56218
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   287
        List<byte[]> bytes = Stream.of(BODY.split("|"))
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   288
                .map(s -> s.getBytes(UTF_8))
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   289
                .collect(Collectors.toList());
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   290
        testThrowing(test, uri, sameClient, () -> BodyPublishers.ofByteArrays(bytes),
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   291
                this::shouldNotThrowInCancel, thrower,false);
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   292
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   293
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   294
    private <T,U> void testThrowing(String name, String uri, boolean sameClient,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   295
                                    Supplier<BodyPublisher> publishers,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   296
                                    Finisher finisher, Thrower thrower, boolean async)
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   297
            throws Exception
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   298
    {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   299
        out.printf("%n%s%s%n", now(), name);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   300
        try {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   301
            testThrowing(uri, sameClient, publishers, finisher, thrower, async);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   302
        } catch (Error | Exception x) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   303
            FAILURES.putIfAbsent(name, x);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   304
            throw x;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   305
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   306
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   307
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   308
    private void testThrowing(String uri, boolean sameClient,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   309
                                    Supplier<BodyPublisher> publishers,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   310
                                    Finisher finisher, Thrower thrower,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   311
                                    boolean async)
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   312
            throws Exception
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   313
    {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   314
        HttpClient client = null;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   315
        for (Where where : Where.values()) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   316
            //if (where == Where.ON_SUBSCRIBE) continue;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   317
            //if (where == Where.ON_ERROR) continue;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   318
            if (!sameClient || client == null)
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   319
                client = newHttpClient(sameClient);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   320
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   321
            ThrowingBodyPublisher bodyPublisher =
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   322
                    new ThrowingBodyPublisher(where.select(thrower), publishers.get());
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   323
            HttpRequest req = HttpRequest.
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   324
                    newBuilder(URI.create(uri))
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   325
                    .header("X-expect-exception", "true")
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   326
                    .POST(bodyPublisher)
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   327
                    .build();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   328
            BodyHandler<String> handler = BodyHandlers.ofString();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   329
            System.out.println("try throwing in " + where);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   330
            HttpResponse<String> response = null;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   331
            if (async) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   332
                try {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   333
                    response = client.sendAsync(req, handler).join();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   334
                } catch (Error | Exception x) {
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   335
                    Throwable cause = findCause(where, x, thrower);
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   336
                    if (cause == null) throw causeNotFound(where, x);
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   337
                    System.out.println(now() + "Got expected exception: " + cause);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   338
                }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   339
            } else {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   340
                try {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   341
                    response = client.send(req, handler);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   342
                } catch (Error | Exception t) {
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   343
                    if (thrower.test(where, t)) {
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   344
                        System.out.println(now() + "Got expected exception: " + t);
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   345
                    } else throw causeNotFound(where, t);
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   346
                }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   347
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   348
            if (response != null) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   349
                finisher.finish(where, response, thrower);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   350
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   351
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   352
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   353
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   354
    enum Where {
56218
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   355
        BEFORE_SUBSCRIBE, BEFORE_REQUEST, BEFORE_NEXT_REQUEST, BEFORE_CANCEL,
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   356
        AFTER_SUBSCRIBE, AFTER_REQUEST, AFTER_NEXT_REQUEST, AFTER_CANCEL;
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   357
        public Consumer<Where> select(Consumer<Where> consumer) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   358
            return new Consumer<Where>() {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   359
                @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   360
                public void accept(Where where) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   361
                    if (Where.this == where) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   362
                        consumer.accept(where);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   363
                    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   364
                }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   365
            };
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   366
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   367
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   368
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   369
    interface Thrower extends Consumer<Where>, BiPredicate<Where,Throwable> {
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   370
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   371
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   372
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   373
    interface Finisher<T,U> {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   374
        U finish(Where w, HttpResponse<T> resp, Thrower thrower) throws IOException;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   375
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   376
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   377
    final <T,U> U shouldNotThrowInCancel(Where w, HttpResponse<T> resp, Thrower thrower) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   378
        switch (w) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   379
            case BEFORE_CANCEL: return null;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   380
            case AFTER_CANCEL: return null;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   381
            default: break;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   382
        }
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   383
        return shouldHaveThrown(w, resp, thrower);
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   384
    }
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   385
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   386
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   387
    final <T,U> U shouldHaveThrown(Where w, HttpResponse<T> resp, Thrower thrower) {
56218
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   388
        String msg = "Expected exception not thrown in " + w
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   389
                + "\n\tReceived: " + resp
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   390
                + "\n\tWith body: " + resp.body();
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   391
        System.out.println(msg);
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   392
        throw new RuntimeException(msg);
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   393
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   394
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   395
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   396
    private static Throwable findCause(Where w,
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   397
                                       Throwable x,
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   398
                                       BiPredicate<Where, Throwable> filter) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   399
        while (x != null && !filter.test(w,x)) x = x.getCause();
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   400
        return x;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   401
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   402
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   403
    static AssertionError causeNotFound(Where w, Throwable t) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   404
        return new AssertionError("Expected exception not found in " + w, t);
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   405
    }
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   406
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   407
    static boolean isConnectionClosedLocally(Throwable t) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   408
        if (t instanceof CompletionException) t = t.getCause();
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   409
        if (t instanceof ExecutionException) t = t.getCause();
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   410
        if (t instanceof IOException) {
56218
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   411
            String msg = t.getMessage();
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   412
            return msg == null ? false
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   413
                    : msg.contains("connection closed locally");
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   414
        }
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   415
        return false;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   416
    }
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   417
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   418
    static final class UncheckedCustomExceptionThrower implements Thrower {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   419
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   420
        public void accept(Where where) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   421
            out.println(now() + "Throwing in " + where);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   422
            throw new UncheckedCustomException(where.name());
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   423
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   424
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   425
        @Override
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   426
        public boolean test(Where w, Throwable throwable) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   427
            switch (w) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   428
                case AFTER_REQUEST:
56218
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   429
                case BEFORE_NEXT_REQUEST:
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   430
                case AFTER_NEXT_REQUEST:
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   431
                    if (isConnectionClosedLocally(throwable)) return true;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   432
                    break;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   433
                default:
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   434
                    break;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   435
            }
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   436
            return UncheckedCustomException.class.isInstance(throwable);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   437
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   438
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   439
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   440
        public String toString() {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   441
            return "UncheckedCustomExceptionThrower";
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   442
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   443
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   444
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   445
    static final class UncheckedIOExceptionThrower implements Thrower {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   446
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   447
        public void accept(Where where) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   448
            out.println(now() + "Throwing in " + where);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   449
            throw new UncheckedIOException(new CustomIOException(where.name()));
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   450
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   451
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   452
        @Override
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   453
        public boolean test(Where w, Throwable throwable) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   454
            switch (w) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   455
                case AFTER_REQUEST:
56218
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   456
                case BEFORE_NEXT_REQUEST:
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   457
                case AFTER_NEXT_REQUEST:
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   458
                    if (isConnectionClosedLocally(throwable)) return true;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   459
                    break;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   460
                default:
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   461
                    break;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   462
            }
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   463
            return UncheckedIOException.class.isInstance(throwable)
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   464
                    && CustomIOException.class.isInstance(throwable.getCause());
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   465
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   466
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   467
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   468
        public String toString() {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   469
            return "UncheckedIOExceptionThrower";
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   470
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   471
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   472
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   473
    static final class UncheckedCustomException extends RuntimeException {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   474
        UncheckedCustomException(String message) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   475
            super(message);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   476
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   477
        UncheckedCustomException(String message, Throwable cause) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   478
            super(message, cause);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   479
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   480
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   481
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   482
    static final class CustomIOException extends IOException {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   483
        CustomIOException(String message) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   484
            super(message);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   485
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   486
        CustomIOException(String message, Throwable cause) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   487
            super(message, cause);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   488
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   489
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   490
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   491
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   492
    static final class ThrowingBodyPublisher implements BodyPublisher {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   493
        private final BodyPublisher publisher;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   494
        private final CompletableFuture<Void> subscribedCF = new CompletableFuture<>();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   495
        final Consumer<Where> throwing;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   496
        ThrowingBodyPublisher(Consumer<Where> throwing, BodyPublisher publisher) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   497
            this.throwing = throwing;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   498
            this.publisher = publisher;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   499
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   500
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   501
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   502
        public long contentLength() {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   503
            return publisher.contentLength();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   504
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   505
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   506
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   507
        public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   508
            try {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   509
                throwing.accept(Where.BEFORE_SUBSCRIBE);
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   510
                publisher.subscribe(new SubscriberWrapper(subscriber));
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   511
                subscribedCF.complete(null);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   512
                throwing.accept(Where.AFTER_SUBSCRIBE);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   513
            } catch (Throwable t) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   514
                subscribedCF.completeExceptionally(t);
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   515
                throw t;
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   516
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   517
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   518
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   519
        CompletableFuture<Void> subscribedCF() {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   520
            return subscribedCF;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   521
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   522
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   523
        class SubscriptionWrapper implements Flow.Subscription {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   524
            final Flow.Subscription subscription;
56218
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   525
            final AtomicLong requestCount = new AtomicLong();
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   526
            SubscriptionWrapper(Flow.Subscription subscription) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   527
                this.subscription = subscription;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   528
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   529
            @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   530
            public void request(long n) {
56218
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   531
                long count = requestCount.incrementAndGet();
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   532
                System.out.printf("%s request-%d(%d)%n", now(), count, n);
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   533
                if (count > 1) throwing.accept(Where.BEFORE_NEXT_REQUEST);
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   534
                throwing.accept(Where.BEFORE_REQUEST);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   535
                subscription.request(n);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   536
                throwing.accept(Where.AFTER_REQUEST);
56218
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   537
                if (count > 1) throwing.accept(Where.AFTER_NEXT_REQUEST);
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   538
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   539
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   540
            @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   541
            public void cancel() {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   542
                throwing.accept(Where.BEFORE_CANCEL);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   543
                subscription.cancel();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   544
                throwing.accept(Where.AFTER_CANCEL);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   545
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   546
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   547
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   548
        class SubscriberWrapper implements Flow.Subscriber<ByteBuffer> {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   549
            final Flow.Subscriber<? super ByteBuffer> subscriber;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   550
            SubscriberWrapper(Flow.Subscriber<? super ByteBuffer> subscriber) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   551
                this.subscriber = subscriber;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   552
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   553
            @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   554
            public void onSubscribe(Flow.Subscription subscription) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   555
                subscriber.onSubscribe(new SubscriptionWrapper(subscription));
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   556
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   557
            @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   558
            public void onNext(ByteBuffer item) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   559
                subscriber.onNext(item);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   560
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   561
            @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   562
            public void onComplete() {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   563
                subscriber.onComplete();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   564
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   565
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   566
            @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   567
            public void onError(Throwable throwable) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   568
                subscriber.onError(throwable);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   569
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   570
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   571
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   572
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   573
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   574
    @BeforeTest
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   575
    public void setup() throws Exception {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   576
        sslContext = new SimpleSSLContext().get();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   577
        if (sslContext == null)
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   578
            throw new AssertionError("Unexpected null sslContext");
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   579
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   580
        // HTTP/1.1
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   581
        HttpTestHandler h1_fixedLengthHandler = new HTTP_FixedLengthHandler();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   582
        HttpTestHandler h1_chunkHandler = new HTTP_ChunkedHandler();
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56218
diff changeset
   583
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   584
        httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   585
        httpTestServer.addHandler(h1_fixedLengthHandler, "/http1/fixed");
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   586
        httpTestServer.addHandler(h1_chunkHandler, "/http1/chunk");
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56218
diff changeset
   587
        httpURI_fixed = "http://" + httpTestServer.serverAuthority() + "/http1/fixed/x";
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56218
diff changeset
   588
        httpURI_chunk = "http://" + httpTestServer.serverAuthority() + "/http1/chunk/x";
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   589
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   590
        HttpsServer httpsServer = HttpsServer.create(sa, 0);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   591
        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   592
        httpsTestServer = HttpTestServer.of(httpsServer);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   593
        httpsTestServer.addHandler(h1_fixedLengthHandler, "/https1/fixed");
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   594
        httpsTestServer.addHandler(h1_chunkHandler, "/https1/chunk");
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56218
diff changeset
   595
        httpsURI_fixed = "https://" + httpsTestServer.serverAuthority() + "/https1/fixed/x";
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56218
diff changeset
   596
        httpsURI_chunk = "https://" + httpsTestServer.serverAuthority() + "/https1/chunk/x";
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   597
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   598
        // HTTP/2
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   599
        HttpTestHandler h2_fixedLengthHandler = new HTTP_FixedLengthHandler();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   600
        HttpTestHandler h2_chunkedHandler = new HTTP_ChunkedHandler();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   601
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56218
diff changeset
   602
        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   603
        http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   604
        http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56218
diff changeset
   605
        http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed/x";
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56218
diff changeset
   606
        http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk/x";
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   607
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56218
diff changeset
   608
        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, 0));
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   609
        https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   610
        https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56218
diff changeset
   611
        https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed/x";
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56218
diff changeset
   612
        https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/chunk/x";
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   613
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   614
        serverCount.addAndGet(4);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   615
        httpTestServer.start();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   616
        httpsTestServer.start();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   617
        http2TestServer.start();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   618
        https2TestServer.start();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   619
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   620
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   621
    @AfterTest
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   622
    public void teardown() throws Exception {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   623
        sharedClient = null;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   624
        httpTestServer.stop();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   625
        httpsTestServer.stop();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   626
        http2TestServer.stop();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   627
        https2TestServer.stop();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   628
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   629
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   630
    static class HTTP_FixedLengthHandler implements HttpTestHandler {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   631
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   632
        public void handle(HttpTestExchange t) throws IOException {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   633
            out.println("HTTP_FixedLengthHandler received request to " + t.getRequestURI());
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   634
            byte[] resp;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   635
            try (InputStream is = t.getRequestBody()) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   636
                resp = is.readAllBytes();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   637
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   638
            t.sendResponseHeaders(200, resp.length);  //fixed content length
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   639
            try (OutputStream os = t.getResponseBody()) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   640
                os.write(resp);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   641
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   642
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   643
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   644
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   645
    static class HTTP_ChunkedHandler implements HttpTestHandler {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   646
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   647
        public void handle(HttpTestExchange t) throws IOException {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   648
            out.println("HTTP_ChunkedHandler received request to " + t.getRequestURI());
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   649
            byte[] resp;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   650
            try (InputStream is = t.getRequestBody()) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   651
                resp = is.readAllBytes();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   652
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   653
            t.sendResponseHeaders(200, -1); // chunked/variable
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   654
            try (OutputStream os = t.getResponseBody()) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   655
                os.write(resp);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   656
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   657
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   658
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   659
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   660
}