test/jdk/java/net/httpclient/http2/FixedThreadPoolTest.java
author michaelm
Tue, 16 Jan 2018 15:52:01 +0000
branchhttp-client-branch
changeset 56010 782b2f2d1e76
parent 55973 4d9b002587db
child 56089 42208b2f224e
permissions -rw-r--r--
http-client-branch: updated server push API
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
     1
/*
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 43984
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
     4
 *
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
     8
 *
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    13
 * accompanied this code).
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    14
 *
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    18
 *
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    21
 * questions.
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    22
 */
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    23
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    24
/*
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    25
 * @test
46836
2684bef98033 8177935: java/net/httpclient/http2/FixedThreadPoolTest.java fails frequently
dfuchs
parents: 45716
diff changeset
    26
 * @bug 8087112 8177935
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    27
 * @library /lib/testlibrary server
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    28
 * @build jdk.testlibrary.SimpleSSLContext
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    29
 * @modules java.base/sun.net.www.http
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    30
 *          jdk.incubator.httpclient/jdk.incubator.http.internal.common
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    31
 *          jdk.incubator.httpclient/jdk.incubator.http.internal.frame
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    32
 *          jdk.incubator.httpclient/jdk.incubator.http.internal.hpack
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    33
 * @run testng/othervm -Djdk.httpclient.HttpClient.log=ssl,requests,responses,errors FixedThreadPoolTest
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    34
 */
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    35
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    36
import java.net.*;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    37
import jdk.incubator.http.*;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    38
import static jdk.incubator.http.HttpClient.Version.HTTP_2;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    39
import javax.net.ssl.*;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    40
import java.nio.file.*;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    41
import java.util.concurrent.*;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    42
import jdk.testlibrary.SimpleSSLContext;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    43
import static jdk.incubator.http.HttpRequest.BodyPublisher.fromFile;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    44
import static jdk.incubator.http.HttpRequest.BodyPublisher.fromString;
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    45
import static jdk.incubator.http.HttpResponse.BodyHandler.asFile;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    46
import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    47
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    48
import org.testng.annotations.Test;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    49
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    50
@Test
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    51
public class FixedThreadPoolTest {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    52
    static int httpPort, httpsPort;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    53
    static Http2TestServer httpServer, httpsServer;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    54
    static HttpClient client = null;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    55
    static ExecutorService exec;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    56
    static SSLContext sslContext;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    57
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    58
    static String httpURIString, httpsURIString;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    59
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    60
    static void initialize() throws Exception {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    61
        try {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    62
            SimpleSSLContext sslct = new SimpleSSLContext();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    63
            sslContext = sslct.get();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    64
            client = getClient();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    65
            httpServer = new Http2TestServer(false, 0, exec, sslContext);
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 43984
diff changeset
    66
            httpServer.addHandler(new Http2EchoHandler(), "/");
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    67
            httpPort = httpServer.getAddress().getPort();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    68
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    69
            httpsServer = new Http2TestServer(true, 0, exec, sslContext);
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 43984
diff changeset
    70
            httpsServer.addHandler(new Http2EchoHandler(), "/");
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    71
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    72
            httpsPort = httpsServer.getAddress().getPort();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    73
            httpURIString = "http://127.0.0.1:" + httpPort + "/foo/";
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    74
            httpsURIString = "https://127.0.0.1:" + httpsPort + "/bar/";
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    75
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    76
            httpServer.start();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    77
            httpsServer.start();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    78
        } catch (Throwable e) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    79
            System.err.println("Throwing now");
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    80
            e.printStackTrace();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    81
            throw e;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    82
        }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    83
    }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    84
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    85
    @Test
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    86
    public static void test() throws Exception {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    87
        try {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    88
            initialize();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    89
            simpleTest(false);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    90
            simpleTest(true);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    91
            streamTest(false);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    92
            streamTest(true);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    93
            paramsTest();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    94
            Thread.sleep(1000 * 4);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    95
        } catch (Exception | Error tt) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    96
            tt.printStackTrace();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    97
            throw tt;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    98
        } finally {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
    99
            httpServer.stop();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   100
            httpsServer.stop();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   101
            exec.shutdownNow();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   102
        }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   103
    }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   104
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   105
    static HttpClient getClient() {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   106
        if (client == null) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   107
            exec = Executors.newCachedThreadPool();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   108
            // Executor e1 = Executors.newFixedThreadPool(1);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   109
            // Executor e = (Runnable r) -> e1.execute(() -> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   110
            //    System.out.println("[" + Thread.currentThread().getName()
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   111
            //                       + "] Executing: "
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   112
            //                       + r.getClass().getName());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   113
            //    r.run();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   114
            // });
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   115
            client = HttpClient.newBuilder()
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   116
                               .executor(Executors.newFixedThreadPool(2))
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   117
                               .sslContext(sslContext)
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   118
                               .version(HTTP_2)
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   119
                               .build();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   120
        }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   121
        return client;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   122
    }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   123
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   124
    static URI getURI(boolean secure) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   125
        if (secure)
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   126
            return URI.create(httpsURIString);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   127
        else
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   128
            return URI.create(httpURIString);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   129
    }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   130
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   131
    static void checkStatus(int expected, int found) throws Exception {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   132
        if (expected != found) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   133
            System.err.printf ("Test failed: wrong status code %d/%d\n",
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   134
                expected, found);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   135
            throw new RuntimeException("Test failed");
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   136
        }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   137
    }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   138
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   139
    static void checkStrings(String expected, String found) throws Exception {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   140
        if (!expected.equals(found)) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   141
            System.err.printf ("Test failed: wrong string %s/%s\n",
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   142
                expected, found);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   143
            throw new RuntimeException("Test failed");
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   144
        }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   145
    }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   146
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   147
    static Void compareFiles(Path path1, Path path2) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   148
        return TestUtil.compareFiles(path1, path2);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   149
    }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   150
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   151
    static Path tempFile() {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   152
        return TestUtil.tempFile();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   153
    }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   154
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   155
    static final String SIMPLE_STRING = "Hello world Goodbye world";
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   156
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   157
    static final int LOOPS = 32;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   158
    static final int FILESIZE = 64 * 1024 + 200;
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   159
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   160
    static void streamTest(boolean secure) throws Exception {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   161
        URI uri = getURI(secure);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   162
        System.err.printf("streamTest %b to %s\n" , secure, uri);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   163
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   164
        HttpClient client = getClient();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   165
        Path src = TestUtil.getAFile(FILESIZE * 4);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   166
        HttpRequest req = HttpRequest.newBuilder(uri)
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   167
                                     .POST(fromFile(src))
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   168
                                     .build();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   169
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   170
        Path dest = Paths.get("streamtest.txt");
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   171
        dest.toFile().delete();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   172
        CompletableFuture<Path> response = client.sendAsync(req, asFile(dest))
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   173
                .thenApply(resp -> {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   174
                    if (resp.statusCode() != 200)
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   175
                        throw new RuntimeException();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   176
                    return resp.body();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   177
                });
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   178
        response.join();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   179
        compareFiles(src, dest);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   180
        System.err.println("DONE");
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   181
    }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   182
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   183
    static void paramsTest() throws Exception {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   184
        System.err.println("paramsTest");
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   185
        Http2TestServer server = new Http2TestServer(true, 0, exec, sslContext);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   186
        server.addHandler((t -> {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   187
            SSLSession s = t.getSSLSession();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   188
            String prot = s.getProtocol();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   189
            if (prot.equals("TLSv1.2")) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   190
                t.sendResponseHeaders(200, -1);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   191
            } else {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   192
                System.err.printf("Protocols =%s\n", prot);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   193
                t.sendResponseHeaders(500, -1);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   194
            }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   195
        }), "/");
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   196
        server.start();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   197
        int port = server.getAddress().getPort();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   198
        URI u = new URI("https://127.0.0.1:"+port+"/foo");
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   199
        HttpClient client = getClient();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   200
        HttpRequest req = HttpRequest.newBuilder(u).build();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   201
        HttpResponse<String> resp = client.sendAsync(req, asString()).get();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   202
        int stat = resp.statusCode();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   203
        if (stat != 200) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   204
            throw new RuntimeException("paramsTest failed "
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   205
                + Integer.toString(stat));
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   206
        }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   207
    }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   208
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   209
    static void simpleTest(boolean secure) throws Exception {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   210
        URI uri = getURI(secure);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   211
        System.err.println("Request to " + uri);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   212
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   213
        // Do a simple warmup request
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   214
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   215
        HttpClient client = getClient();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   216
        HttpRequest req = HttpRequest.newBuilder(uri)
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   217
                                     .POST(fromString(SIMPLE_STRING))
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   218
                                     .build();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   219
        HttpResponse<String> response = client.sendAsync(req, asString()).get();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   220
        HttpHeaders h = response.headers();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   221
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   222
        checkStatus(200, response.statusCode());
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   223
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   224
        String responseBody = response.body();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   225
        checkStrings(SIMPLE_STRING, responseBody);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   226
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   227
        checkStrings(h.firstValue("x-hello").get(), "world");
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   228
        checkStrings(h.firstValue("x-bye").get(), "universe");
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   229
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   230
        // Do loops asynchronously
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   231
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   232
        CompletableFuture[] responses = new CompletableFuture[LOOPS];
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   233
        final Path source = TestUtil.getAFile(FILESIZE);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   234
        HttpRequest request = HttpRequest.newBuilder(uri)
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   235
                                         .POST(fromFile(source))
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   236
                                         .build();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   237
        for (int i = 0; i < LOOPS; i++) {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   238
            responses[i] = client.sendAsync(request, asFile(tempFile()))
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   239
                //.thenApply(resp -> compareFiles(resp.body(), source));
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   240
                .thenApply(resp -> {
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   241
                    System.out.printf("Resp status %d body size %d\n",
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   242
                                      resp.statusCode(), resp.body().toFile().length());
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   243
                    return compareFiles(resp.body(), source);
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   244
                });
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   245
        }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   246
        CompletableFuture.allOf(responses).join();
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   247
        System.err.println("DONE");
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   248
    }
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents:
diff changeset
   249
}