test/jdk/java/net/httpclient/ThrowingPublishers.java
author dfuchs
Wed, 28 Feb 2018 15:48:46 +0000
branchhttp-client-branch
changeset 56209 43d5ad612710
parent 56204 e5d0c20217a3
child 56218 fd7bd32963ef
permissions -rw-r--r--
http-client-branch: more ThrowingPublishers tests and fixes
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
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    26
 * @summary Tests what happens when request publishers 
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;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    54
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
    55
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
    56
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
    57
import java.net.http.HttpHeaders;
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.net.http.HttpResponse.BodySubscriber;
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.ByteBuffer;
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.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
    67
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
    68
import java.util.concurrent.CompletableFuture;
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
    69
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
    70
import java.util.concurrent.CompletionStage;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    71
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
    72
import java.util.concurrent.ConcurrentMap;
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
    73
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
    74
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
    75
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
    76
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
    77
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
    78
import java.util.concurrent.atomic.AtomicLong;
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
    79
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
    80
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
    81
import java.util.function.Predicate;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    82
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
    83
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
    84
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
    85
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 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
    87
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
    88
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
    89
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
    90
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
    91
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    92
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
    93
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    94
    SSLContext sslContext;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
    95
    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
    96
    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
    97
    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
    98
    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
    99
    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
   100
    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
   101
    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
   102
    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
   103
    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
   104
    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
   105
    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
   106
    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
   107
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 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
   109
    // 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
   110
    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
   111
    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
   112
    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
   113
    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
   114
    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
   115
    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
   116
    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
   117
        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
   118
        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
   119
        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
   120
        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
   121
        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
   122
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   123
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   124
    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
   125
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   126
    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
   127
        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
   128
        Executor executor;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   129
        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
   130
            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
   131
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   132
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   133
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   134
        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
   135
            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
   136
            executor.execute(() -> {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   137
                try {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   138
                    command.run();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   139
                } 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
   140
                    tasksFailed = true;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   141
                    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
   142
                    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
   143
                    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
   144
                    throw t;
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
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   148
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   149
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   150
    @AfterClass
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   151
    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
   152
        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
   153
        try {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   154
            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
   155
                    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
   156
            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
   157
            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
   158
            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
   159
                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
   160
                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
   161
            });
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   162
            if (tasksFailed) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   163
                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
   164
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   165
        } finally {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   166
            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
   167
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   168
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   169
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   170
    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
   171
        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
   172
                httpURI_fixed,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   173
                httpURI_chunk,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   174
                httpsURI_fixed,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   175
                httpsURI_chunk,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   176
                http2URI_fixed,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   177
                http2URI_chunk,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   178
                https2URI_fixed,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   179
                https2URI_chunk,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   180
        };
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   181
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   182
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   183
    @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
   184
    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
   185
        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
   186
        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
   187
        //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
   188
        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
   189
        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
   190
            //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
   191
            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
   192
                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
   193
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   194
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   195
        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
   196
        // 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
   197
        return result;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   198
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   199
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   200
    @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
   201
    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
   202
        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
   203
        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
   204
        //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
   205
        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
   206
        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
   207
                new UncheckedIOExceptionThrower(),
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   208
                new UncheckedCustomExceptionThrower())) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   209
            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
   210
                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
   211
                    // 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
   212
                    // 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
   213
                    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
   214
                }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   215
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   216
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   217
        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
   218
        //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
   219
        return result;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   220
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   221
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   222
    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
   223
        clientCount.incrementAndGet();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   224
        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
   225
                .executor(executor)
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   226
                .sslContext(sslContext)
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   227
                .build();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   228
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   229
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   230
    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
   231
        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
   232
        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
   233
        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
   234
        synchronized (this) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   235
            shared = sharedClient;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   236
            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
   237
                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
   238
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   239
            return shared;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   240
        }
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
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   243
    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
   244
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   245
    @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
   246
    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
   247
            throws Exception {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   248
        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
   249
        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
   250
        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
   251
            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
   252
                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
   253
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   254
            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
   255
                    = 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
   256
            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
   257
                    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
   258
            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
   259
            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
   260
                    .thenAcceptAsync((v) -> {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   261
                                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
   262
                                        .forEachOrdered(s -> {
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("submitting \"" + s +"\"");
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   264
                                                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
   265
                                        });
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   266
                                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
   267
                                publisher.close();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   268
                            },
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   269
                    executor);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   270
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   271
            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
   272
                    .POST(bodyPublisher)
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   273
                    .build();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   274
            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
   275
            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
   276
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   277
            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
   278
            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
   279
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   280
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   281
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   282
    @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
   283
    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
   284
                                     boolean sameClient,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   285
                                     Thrower thrower)
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   286
            throws Exception
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   287
    {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   288
        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
   289
                             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
   290
        testThrowing(test, uri, sameClient, () -> BodyPublishers.ofString(BODY),
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 {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   355
        BEFORE_SUBSCRIBE, BEFORE_REQUEST, BEFORE_CANCEL,
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   356
        AFTER_SUBSCRIBE, AFTER_REQUEST, AFTER_CANCEL;
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) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   388
        throw new RuntimeException("Expected exception not thrown in " + w);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   389
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   390
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   391
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   392
    private static Throwable findCause(Where w,
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   393
                                       Throwable x,
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   394
                                       BiPredicate<Where, Throwable> filter) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   395
        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
   396
        return x;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   397
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   398
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   399
    static AssertionError causeNotFound(Where w, Throwable t) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   400
        return new AssertionError("Expected exception not found in " + w, t);
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   401
    }
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   402
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   403
    static boolean isConnectionClosedLocally(Throwable t) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   404
        if (t instanceof CompletionException) t = t.getCause();
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   405
        if (t instanceof ExecutionException) t = t.getCause();
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   406
        if (t instanceof IOException) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   407
            return t.getMessage().contains("connection closed locally");
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   408
        }
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   409
        return false;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   410
    }
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   411
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   412
    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
   413
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   414
        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
   415
            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
   416
            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
   417
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   418
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   419
        @Override
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   420
        public boolean test(Where w, Throwable throwable) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   421
            switch (w) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   422
                case AFTER_REQUEST:
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   423
                    if (isConnectionClosedLocally(throwable)) return true;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   424
                    break;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   425
                default:
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   426
                    break;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   427
            }
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   428
            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
   429
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   430
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   431
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   432
        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
   433
            return "UncheckedCustomExceptionThrower";
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   434
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   435
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   436
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   437
    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
   438
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   439
        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
   440
            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
   441
            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
   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
        @Override
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   445
        public boolean test(Where w, Throwable throwable) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   446
            switch (w) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   447
                case AFTER_REQUEST:
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   448
                    if (isConnectionClosedLocally(throwable)) return true;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   449
                    break;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   450
                default:
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   451
                    break;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   452
            }
56204
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   453
            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
   454
                    && 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
   455
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   456
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   457
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   458
        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
   459
            return "UncheckedIOExceptionThrower";
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   460
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   461
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   462
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   463
    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
   464
        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
   465
            super(message);
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
        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
   468
            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
   469
        }
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
    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
   473
        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
   474
            super(message);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   475
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   476
        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
   477
            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
   478
        }
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 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
   483
        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
   484
        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
   485
        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
   486
        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
   487
            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
   488
            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
   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
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   492
        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
   493
            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
   494
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   495
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   496
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   497
        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
   498
            try {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   499
                throwing.accept(Where.BEFORE_SUBSCRIBE);
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   500
                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
   501
                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
   502
                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
   503
            } 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
   504
                subscribedCF.completeExceptionally(t);
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56204
diff changeset
   505
                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
   506
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   507
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   508
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   509
        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
   510
            return subscribedCF;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   511
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   512
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   513
        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
   514
            final 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
   515
            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
   516
                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
   517
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   518
            @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   519
            public void request(long n) {
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   520
                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
   521
                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
   522
                throwing.accept(Where.AFTER_REQUEST);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   523
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   524
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   525
            @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   526
            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
   527
                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
   528
                subscription.cancel();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   529
                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
   530
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   531
        }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   532
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   533
        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
   534
            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
   535
            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
   536
                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
   537
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   538
            @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   539
            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
   540
                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
   541
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   542
            @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   543
            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
   544
                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
   545
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   546
            @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   547
            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
   548
                subscriber.onComplete();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   549
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   550
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   551
            @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   552
            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
   553
                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
   554
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   555
        }
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
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   558
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   559
    @BeforeTest
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   560
    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
   561
        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
   562
        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
   563
            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
   564
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   565
        // 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
   566
        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
   567
        HttpTestHandler h1_chunkHandler = 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
   568
        InetSocketAddress sa = new InetSocketAddress(0);
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   569
        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
   570
        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
   571
        httpTestServer.addHandler(h1_chunkHandler, "/http1/chunk");
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   572
        httpURI_fixed = "http://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/http1/fixed/x";
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   573
        httpURI_chunk = "http://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/http1/chunk/x";
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   574
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   575
        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
   576
        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
   577
        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
   578
        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
   579
        httpsTestServer.addHandler(h1_chunkHandler, "/https1/chunk");
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   580
        httpsURI_fixed = "https://127.0.0.1:" + httpsTestServer.getAddress().getPort() + "/https1/fixed/x";
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   581
        httpsURI_chunk = "https://127.0.0.1:" + httpsTestServer.getAddress().getPort() + "/https1/chunk/x";
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   582
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   583
        // HTTP/2
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   584
        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
   585
        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
   586
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   587
        http2TestServer = HttpTestServer.of(new Http2TestServer("127.0.0.1", false, 0));
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   588
        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
   589
        http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   590
        int port = http2TestServer.getAddress().getPort();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   591
        http2URI_fixed = "http://127.0.0.1:" + port + "/http2/fixed/x";
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   592
        http2URI_chunk = "http://127.0.0.1:" + port + "/http2/chunk/x";
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   593
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   594
        https2TestServer = HttpTestServer.of(new Http2TestServer("127.0.0.1", true, 0));
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   595
        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
   596
        https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   597
        port = https2TestServer.getAddress().getPort();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   598
        https2URI_fixed = "https://127.0.0.1:" + port + "/https2/fixed/x";
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   599
        https2URI_chunk = "https://127.0.0.1:" + port + "/https2/chunk/x";
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   600
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   601
        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
   602
        httpTestServer.start();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   603
        httpsTestServer.start();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   604
        http2TestServer.start();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   605
        https2TestServer.start();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   606
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   607
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   608
    @AfterTest
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   609
    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
   610
        sharedClient = null;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   611
        httpTestServer.stop();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   612
        httpsTestServer.stop();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   613
        http2TestServer.stop();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   614
        https2TestServer.stop();
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   615
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   616
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   617
    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
   618
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   619
        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
   620
            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
   621
            byte[] resp;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   622
            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
   623
                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
   624
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   625
            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
   626
            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
   627
                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
   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
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   631
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   632
    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
   633
        @Override
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   634
        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
   635
            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
   636
            byte[] resp;
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   637
            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
   638
                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
   639
            }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   640
            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
   641
            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
   642
                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
   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
    }
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   646
e5d0c20217a3 http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
diff changeset
   647
}