test/jdk/java/net/httpclient/DependentActionsTest.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 52121 934969c63223
permissions -rw-r--r--
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException Summary: Change implementation to use iterator instead of plain LinkedList Reviewed-by: dfuchs, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     1
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     4
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     8
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    13
 * accompanied this code).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    14
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    18
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    21
 * questions.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    22
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    23
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    24
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    25
 * @test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    26
 * @summary Verify that dependent synchronous actions added before the CF
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
 *          completes are executed either asynchronously in an executor when the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
 *          CF later completes, or in the user thread that joins.
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    29
 * @library /test/lib http2/server
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    30
 * @build jdk.test.lib.net.SimpleSSLContext HttpServerAdapters DependentActionsTest
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
 * @modules java.base/sun.net.www.http
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
 *          java.net.http/jdk.internal.net.http.common
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
 *          java.net.http/jdk.internal.net.http.frame
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
 *          java.net.http/jdk.internal.net.http.hpack
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
 * @run testng/othervm -Djdk.internal.httpclient.debug=true DependentActionsTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
 * @run testng/othervm/java.security.policy=dependent.policy
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
  *        -Djdk.internal.httpclient.debug=true DependentActionsTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
import java.io.BufferedReader;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
import java.io.InputStreamReader;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import java.lang.StackWalker.StackFrame;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import com.sun.net.httpserver.HttpServer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
import com.sun.net.httpserver.HttpsConfigurator;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
import com.sun.net.httpserver.HttpsServer;
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    46
import jdk.test.lib.net.SimpleSSLContext;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
import org.testng.annotations.AfterTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
import org.testng.annotations.AfterClass;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
import org.testng.annotations.BeforeTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
import org.testng.annotations.DataProvider;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
import org.testng.annotations.Test;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
import javax.net.ssl.SSLContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
import java.io.InputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
import java.io.OutputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
import java.net.InetAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
import java.net.InetSocketAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
import java.net.URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
import java.net.http.HttpHeaders;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
import java.net.http.HttpRequest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
import java.net.http.HttpResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
import java.net.http.HttpResponse.BodyHandler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
import java.net.http.HttpResponse.BodyHandlers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
import java.net.http.HttpResponse.BodySubscriber;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
import java.nio.ByteBuffer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
import java.nio.charset.StandardCharsets;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
import java.util.EnumSet;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
import java.util.List;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
import java.util.Optional;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
import java.util.concurrent.CompletableFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
import java.util.concurrent.CompletionException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
import java.util.concurrent.CompletionStage;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
import java.util.concurrent.ConcurrentHashMap;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
import java.util.concurrent.ConcurrentMap;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
import java.util.concurrent.Executor;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
import java.util.concurrent.Executors;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
import java.util.concurrent.Flow;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
import java.util.concurrent.Semaphore;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
import java.util.concurrent.atomic.AtomicBoolean;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
import java.util.concurrent.atomic.AtomicLong;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
import java.util.concurrent.atomic.AtomicReference;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
import java.util.function.Consumer;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
    85
import java.util.function.Predicate;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
import java.util.function.Supplier;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
import java.util.stream.Collectors;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
import java.util.stream.Stream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
import static java.lang.System.out;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
import static java.lang.String.format;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
    92
import static java.util.stream.Collectors.toList;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
import static org.testng.Assert.assertEquals;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
import static org.testng.Assert.assertTrue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
public class DependentActionsTest implements HttpServerAdapters {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
    SSLContext sslContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
    HttpTestServer httpTestServer;    // HTTP/1.1    [ 4 servers ]
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
    HttpTestServer httpsTestServer;   // HTTPS/1.1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
    HttpTestServer http2TestServer;   // HTTP/2 ( h2c )
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
    HttpTestServer https2TestServer;  // HTTP/2 ( h2  )
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
    String httpURI_fixed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
    String httpURI_chunk;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
    String httpsURI_fixed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
    String httpsURI_chunk;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
    String http2URI_fixed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
    String http2URI_chunk;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
    String https2URI_fixed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
    String https2URI_chunk;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
    static final StackWalker WALKER =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
            StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
    static final int ITERATION_COUNT = 1;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
    // a shared executor helps reduce the amount of threads created by the test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
    static final Executor executor = new TestExecutor(Executors.newCachedThreadPool());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
    static final ConcurrentMap<String, Throwable> FAILURES = new ConcurrentHashMap<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
    static volatile boolean tasksFailed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
    static final AtomicLong serverCount = new AtomicLong();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
    static final AtomicLong clientCount = new AtomicLong();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
    static final long start = System.nanoTime();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
    public static String now() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
        long now = System.nanoTime() - start;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
        long secs = now / 1000_000_000;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
        long mill = (now % 1000_000_000) / 1000_000;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
        long nan = now % 1000_000;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
        return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
    private volatile HttpClient sharedClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
    static class TestExecutor implements Executor {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
        final AtomicLong tasks = new AtomicLong();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   135
        Executor executor;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   136
        TestExecutor(Executor executor) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   137
            this.executor = executor;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
        public void execute(Runnable command) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
            long id = tasks.incrementAndGet();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
            executor.execute(() -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
                try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
                    command.run();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
                } catch (Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
                    tasksFailed = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
                    System.out.printf(now() + "Task %s failed: %s%n", id, t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   149
                    System.err.printf(now() + "Task %s failed: %s%n", id, t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
                    FAILURES.putIfAbsent("Task " + id, t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
                    throw t;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
            });
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
    @AfterClass
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
    static final void printFailedTests() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
        out.println("\n=========================");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
            out.printf("%n%sCreated %d servers and %d clients%n",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
                    now(), serverCount.get(), clientCount.get());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
            if (FAILURES.isEmpty()) return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
            out.println("Failed tests: ");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
            FAILURES.entrySet().forEach((e) -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
                out.printf("\t%s: %s%n", e.getKey(), e.getValue());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
                e.getValue().printStackTrace(out);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
                e.getValue().printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
            });
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
            if (tasksFailed) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
                System.out.println("WARNING: Some tasks failed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
        } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
            out.println("\n=========================\n");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   178
    private String[] uris() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   179
        return new String[] {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
                httpURI_fixed,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
                httpURI_chunk,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
                httpsURI_fixed,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
                httpsURI_chunk,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
                http2URI_fixed,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
                http2URI_chunk,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   186
                https2URI_fixed,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
                https2URI_chunk,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
    static final class SemaphoreStallerSupplier
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
            implements Supplier<SemaphoreStaller> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
        public SemaphoreStaller get() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
            return new SemaphoreStaller();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
        public String toString() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
            return "SemaphoreStaller";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   202
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
    @DataProvider(name = "noStalls")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
    public Object[][] noThrows() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   205
        String[] uris = uris();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
        Object[][] result = new Object[uris.length * 2][];
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
        int i = 0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
        for (boolean sameClient : List.of(false, true)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   209
            for (String uri: uris()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
                result[i++] = new Object[] {uri, sameClient};
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   213
        assert i == uris.length * 2;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   214
        return result;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   215
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   216
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   217
    @DataProvider(name = "variants")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   218
    public Object[][] variants() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   219
        String[] uris = uris();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   220
        Object[][] result = new Object[uris.length * 2][];
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   221
        int i = 0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   222
        Supplier<? extends Staller> s = new SemaphoreStallerSupplier();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   223
        for (Supplier<? extends Staller> staller : List.of(s)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   224
            for (boolean sameClient : List.of(false, true)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   225
                for (String uri : uris()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   226
                    result[i++] = new Object[]{uri, sameClient, staller};
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   227
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   228
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   229
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   230
        assert i == uris.length * 2;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   231
        return result;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   232
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   233
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   234
    private HttpClient makeNewClient() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   235
        clientCount.incrementAndGet();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   236
        return HttpClient.newBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   237
                .executor(executor)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   238
                .sslContext(sslContext)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   239
                .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   240
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   241
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   242
    HttpClient newHttpClient(boolean share) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   243
        if (!share) return makeNewClient();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   244
        HttpClient shared = sharedClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   245
        if (shared != null) return shared;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   246
        synchronized (this) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   247
            shared = sharedClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   248
            if (shared == null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   249
                shared = sharedClient = makeNewClient();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   250
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   251
            return shared;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   252
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   253
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   254
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   255
    @Test(dataProvider = "noStalls")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   256
    public void testNoStalls(String uri, boolean sameClient)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   257
            throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   258
        HttpClient client = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   259
        out.printf("%ntestNoStalls(%s, %b)%n", uri, sameClient);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   260
        for (int i=0; i< ITERATION_COUNT; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   261
            if (!sameClient || client == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   262
                client = newHttpClient(sameClient);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   263
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   264
            HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   265
                    .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   266
            BodyHandler<String> handler =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   267
                    new StallingBodyHandler((w) -> {},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   268
                            BodyHandlers.ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   269
            HttpResponse<String> response = client.send(req, handler);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   270
            String body = response.body();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   271
            assertEquals(URI.create(body).getPath(), URI.create(uri).getPath());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   272
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   273
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   274
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   275
    @Test(dataProvider = "variants")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   276
    public void testAsStringAsync(String uri,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   277
                                  boolean sameClient,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   278
                                  Supplier<Staller> s)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   279
            throws Exception
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   280
    {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   281
        Staller staller = s.get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   282
        String test = format("testAsStringAsync(%s, %b, %s)",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   283
                uri, sameClient, staller);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   284
        testDependent(test, uri, sameClient, BodyHandlers::ofString,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   285
                this::finish, this::extractString, staller);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   286
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   287
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   288
    @Test(dataProvider = "variants")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   289
    public void testAsLinesAsync(String uri,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   290
                                 boolean sameClient,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   291
                                 Supplier<Staller> s)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   292
            throws Exception
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   293
    {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   294
        Staller staller = s.get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   295
        String test = format("testAsLinesAsync(%s, %b, %s)",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   296
                uri, sameClient, staller);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   297
        testDependent(test, uri, sameClient, BodyHandlers::ofLines,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   298
                this::finish, this::extractStream, staller);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   299
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   300
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   301
    @Test(dataProvider = "variants")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   302
    public void testAsInputStreamAsync(String uri,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   303
                                       boolean sameClient,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   304
                                       Supplier<Staller> s)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   305
            throws Exception
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   306
    {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   307
        Staller staller = s.get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   308
        String test = format("testAsInputStreamAsync(%s, %b, %s)",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   309
                uri, sameClient, staller);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   310
        testDependent(test, uri, sameClient, BodyHandlers::ofInputStream,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   311
                this::finish, this::extractInputStream, staller);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   312
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   313
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   314
    private <T,U> void testDependent(String name, String uri, boolean sameClient,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   315
                                     Supplier<BodyHandler<T>> handlers,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   316
                                     Finisher finisher,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   317
                                     Extractor extractor,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   318
                                     Staller staller)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   319
            throws Exception
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   320
    {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   321
        out.printf("%n%s%s%n", now(), name);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   322
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   323
            testDependent(uri, sameClient, handlers, finisher, extractor, staller);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   324
        } catch (Error | Exception x) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   325
            FAILURES.putIfAbsent(name, x);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   326
            throw x;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   327
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   328
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   329
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   330
    private <T,U> void testDependent(String uri, boolean sameClient,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   331
                                     Supplier<BodyHandler<T>> handlers,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   332
                                     Finisher finisher,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   333
                                     Extractor extractor,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   334
                                     Staller staller)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   335
            throws Exception
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   336
    {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   337
        HttpClient client = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   338
        for (Where where : EnumSet.of(Where.BODY_HANDLER)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   339
            if (!sameClient || client == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   340
                client = newHttpClient(sameClient);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   341
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   342
            HttpRequest req = HttpRequest.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   343
                    newBuilder(URI.create(uri))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   344
                    .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   345
            BodyHandler<T> handler =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   346
                    new StallingBodyHandler(where.select(staller), handlers.get());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   347
            System.out.println("try stalling in " + where);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   348
            staller.acquire();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   349
            assert staller.willStall();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   350
            CompletableFuture<HttpResponse<T>> responseCF = client.sendAsync(req, handler);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   351
            assert !responseCF.isDone();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   352
            finisher.finish(where, responseCF, staller, extractor);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   353
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   354
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   355
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   356
    enum Where {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   357
        BODY_HANDLER, ON_SUBSCRIBE, ON_NEXT, ON_COMPLETE, ON_ERROR, GET_BODY, BODY_CF;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   358
        public Consumer<Where> select(Consumer<Where> consumer) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   359
            return new Consumer<Where>() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   360
                @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   361
                public void accept(Where where) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   362
                    if (Where.this == where) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   363
                        consumer.accept(where);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   364
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   365
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   366
            };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   367
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   368
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   369
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   370
    interface Extractor<T> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   371
        public List<String> extract(HttpResponse<T> resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   372
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   373
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   374
    final List<String> extractString(HttpResponse<String> resp) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   375
        return List.of(resp.body());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   376
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   377
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   378
    final List<String> extractStream(HttpResponse<Stream<String>> resp) {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   379
        return resp.body().collect(toList());
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   380
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   381
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   382
    final List<String> extractInputStream(HttpResponse<InputStream> resp) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   383
        try (InputStream is = resp.body()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   384
            return new BufferedReader(new InputStreamReader(is))
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   385
                    .lines().collect(toList());
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   386
        } catch (IOException x) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   387
            throw new CompletionException(x);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   388
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   389
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   390
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   391
    interface Finisher<T> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   392
        public void finish(Where w,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   393
                           CompletableFuture<HttpResponse<T>> cf,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   394
                           Staller staller,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   395
                           Extractor extractor);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   396
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   397
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   398
    Optional<StackFrame> findFrame(Stream<StackFrame> s, String name) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   399
        return s.filter((f) -> f.getClassName().contains(name))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   400
                .filter((f) -> f.getDeclaringClass().getModule().equals(HttpClient.class.getModule()))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   401
                .findFirst();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   402
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   403
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   404
    static final Predicate<StackFrame> DAT = sfe ->
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   405
            sfe.getClassName().startsWith("DependentActionsTest");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   406
    static final Predicate<StackFrame> JUC = sfe ->
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   407
            sfe.getClassName().startsWith("java.util.concurrent");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   408
    static final Predicate<StackFrame> JLT = sfe ->
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   409
            sfe.getClassName().startsWith("java.lang.Thread");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   410
    static final Predicate<StackFrame> NotDATorJUCorJLT = Predicate.not(DAT.or(JUC).or(JLT));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   411
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   412
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   413
    <T> void checkThreadAndStack(Thread thread,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   414
                                 AtomicReference<RuntimeException> failed,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   415
                                 T result,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   416
                                 Throwable error) {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   417
        //failed.set(new RuntimeException("Dependant action was executed in " + thread));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   418
        List<StackFrame> otherFrames = WALKER.walk(s -> s.filter(NotDATorJUCorJLT).collect(toList()));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   419
        if (!otherFrames.isEmpty()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   420
            System.out.println("Found unexpected trace: ");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   421
            otherFrames.forEach(f -> System.out.printf("\t%s%n", f));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   422
            failed.set(new RuntimeException("Dependant action has unexpected frame in " +
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   423
                       Thread.currentThread() + ": " + otherFrames.get(0)));
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   424
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   425
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   426
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   427
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   428
    <T> void finish(Where w, CompletableFuture<HttpResponse<T>> cf,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   429
                    Staller staller,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   430
                    Extractor<T> extractor) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   431
        Thread thread = Thread.currentThread();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   432
        AtomicReference<RuntimeException> failed = new AtomicReference<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   433
        CompletableFuture<HttpResponse<T>> done = cf.whenComplete(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   434
                (r,t) -> checkThreadAndStack(thread, failed, r, t));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   435
        assert !cf.isDone();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   436
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   437
            Thread.sleep(100);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   438
        } catch (Throwable t) {/* don't care */}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   439
        assert !cf.isDone();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   440
        staller.release();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   441
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   442
            HttpResponse<T> response = done.join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   443
            List<String> result = extractor.extract(response);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   444
            RuntimeException error = failed.get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   445
            if (error != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   446
                throw new RuntimeException("Test failed in "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   447
                        + w + ": " + response, error);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   448
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   449
            assertEquals(result, List.of(response.request().uri().getPath()));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   450
        } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   451
            staller.reset();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   452
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   453
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   454
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   455
    interface Staller extends Consumer<Where> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   456
        void release();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   457
        void acquire();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   458
        void reset();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   459
        boolean willStall();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   460
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   461
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   462
    static final class SemaphoreStaller implements Staller {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   463
        final Semaphore sem = new Semaphore(1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   464
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   465
        public void accept(Where where) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   466
            System.out.println("Acquiring semaphore in "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   467
                    + where + " permits=" + sem.availablePermits());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   468
            sem.acquireUninterruptibly();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   469
            System.out.println("Semaphored acquired in " + where);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   470
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   471
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   472
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   473
        public void release() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   474
            System.out.println("Releasing semaphore: permits="
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   475
                    + sem.availablePermits());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   476
            sem.release();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   477
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   478
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   479
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   480
        public void acquire() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   481
            sem.acquireUninterruptibly();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   482
            System.out.println("Semaphored acquired");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   483
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   484
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   485
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   486
        public void reset() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   487
            System.out.println("Reseting semaphore: permits="
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   488
                    + sem.availablePermits());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   489
            sem.drainPermits();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   490
            sem.release();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   491
            System.out.println("Semaphore reset: permits="
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   492
                    + sem.availablePermits());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   493
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   494
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   495
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   496
        public boolean willStall() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   497
            return sem.availablePermits() <= 0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   498
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   499
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   500
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   501
        public String toString() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   502
            return "SemaphoreStaller";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   503
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   504
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   505
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   506
    static final class StallingBodyHandler<T> implements BodyHandler<T> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   507
        final Consumer<Where> stalling;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   508
        final BodyHandler<T> bodyHandler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   509
        StallingBodyHandler(Consumer<Where> stalling, BodyHandler<T> bodyHandler) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   510
            this.stalling = stalling;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   511
            this.bodyHandler = bodyHandler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   512
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   513
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   514
        public BodySubscriber<T> apply(HttpResponse.ResponseInfo rinfo) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   515
            stalling.accept(Where.BODY_HANDLER);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   516
            BodySubscriber<T> subscriber = bodyHandler.apply(rinfo);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   517
            return new StallingBodySubscriber(stalling, subscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   518
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   519
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   520
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   521
    static final class StallingBodySubscriber<T> implements BodySubscriber<T> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   522
        private final BodySubscriber<T> subscriber;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   523
        volatile boolean onSubscribeCalled;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   524
        final Consumer<Where> stalling;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   525
        StallingBodySubscriber(Consumer<Where> stalling, BodySubscriber<T> subscriber) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   526
            this.stalling = stalling;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   527
            this.subscriber = subscriber;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   528
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   529
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   530
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   531
        public void onSubscribe(Flow.Subscription subscription) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   532
            //out.println("onSubscribe ");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   533
            onSubscribeCalled = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   534
            stalling.accept(Where.ON_SUBSCRIBE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   535
            subscriber.onSubscribe(subscription);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   536
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   537
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   538
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   539
        public void onNext(List<ByteBuffer> item) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   540
            // out.println("onNext " + item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   541
            assertTrue(onSubscribeCalled);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   542
            stalling.accept(Where.ON_NEXT);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   543
            subscriber.onNext(item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   544
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   545
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   546
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   547
        public void onError(Throwable throwable) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   548
            //out.println("onError");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   549
            assertTrue(onSubscribeCalled);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   550
            stalling.accept(Where.ON_ERROR);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   551
            subscriber.onError(throwable);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   552
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   553
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   554
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   555
        public void onComplete() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   556
            //out.println("onComplete");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   557
            assertTrue(onSubscribeCalled, "onComplete called before onSubscribe");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   558
            stalling.accept(Where.ON_COMPLETE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   559
            subscriber.onComplete();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   560
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   561
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   562
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   563
        public CompletionStage<T> getBody() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   564
            stalling.accept(Where.GET_BODY);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   565
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   566
                stalling.accept(Where.BODY_CF);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   567
            } catch (Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   568
                return CompletableFuture.failedFuture(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   569
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   570
            return subscriber.getBody();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   571
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   572
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   573
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   574
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   575
    @BeforeTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   576
    public void setup() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   577
        sslContext = new SimpleSSLContext().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   578
        if (sslContext == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   579
            throw new AssertionError("Unexpected null sslContext");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   580
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   581
        // HTTP/1.1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   582
        HttpTestHandler h1_fixedLengthHandler = new HTTP_FixedLengthHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   583
        HttpTestHandler h1_chunkHandler = new HTTP_ChunkedHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   584
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   585
        httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   586
        httpTestServer.addHandler(h1_fixedLengthHandler, "/http1/fixed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   587
        httpTestServer.addHandler(h1_chunkHandler, "/http1/chunk");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   588
        httpURI_fixed = "http://" + httpTestServer.serverAuthority() + "/http1/fixed/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   589
        httpURI_chunk = "http://" + httpTestServer.serverAuthority() + "/http1/chunk/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   590
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   591
        HttpsServer httpsServer = HttpsServer.create(sa, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   592
        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   593
        httpsTestServer = HttpTestServer.of(httpsServer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   594
        httpsTestServer.addHandler(h1_fixedLengthHandler, "/https1/fixed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   595
        httpsTestServer.addHandler(h1_chunkHandler, "/https1/chunk");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   596
        httpsURI_fixed = "https://" + httpsTestServer.serverAuthority() + "/https1/fixed/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   597
        httpsURI_chunk = "https://" + httpsTestServer.serverAuthority() + "/https1/chunk/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   598
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   599
        // HTTP/2
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   600
        HttpTestHandler h2_fixedLengthHandler = new HTTP_FixedLengthHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   601
        HttpTestHandler h2_chunkedHandler = new HTTP_ChunkedHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   602
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   603
        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   604
        http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   605
        http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   606
        http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   607
        http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   608
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   609
        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   610
        https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   611
        https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   612
        https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   613
        https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/chunk/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   614
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   615
        serverCount.addAndGet(4);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   616
        httpTestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   617
        httpsTestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   618
        http2TestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   619
        https2TestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   620
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   621
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   622
    @AfterTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   623
    public void teardown() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   624
        sharedClient = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   625
        httpTestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   626
        httpsTestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   627
        http2TestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   628
        https2TestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   629
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   630
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   631
    static class HTTP_FixedLengthHandler implements HttpTestHandler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   632
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   633
        public void handle(HttpTestExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   634
            out.println("HTTP_FixedLengthHandler received request to " + t.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   635
            try (InputStream is = t.getRequestBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   636
                is.readAllBytes();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   637
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   638
            byte[] resp = t.getRequestURI().getPath().getBytes(StandardCharsets.UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   639
            t.sendResponseHeaders(200, resp.length);  //fixed content length
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   640
            try (OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   641
                os.write(resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   642
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   643
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   644
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   645
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   646
    static class HTTP_ChunkedHandler implements HttpTestHandler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   647
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   648
        public void handle(HttpTestExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   649
            out.println("HTTP_ChunkedHandler received request to " + t.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   650
            byte[] resp = t.getRequestURI().getPath().toString().getBytes(StandardCharsets.UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   651
            try (InputStream is = t.getRequestBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   652
                is.readAllBytes();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   653
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   654
            t.sendResponseHeaders(200, -1); // chunked/variable
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   655
            try (OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   656
                os.write(resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   657
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   658
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   659
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   660
}