test/jdk/java/net/httpclient/MaxStreams.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 53525 c55508666f24
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:
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     1
/*
53525
c55508666f24 8217882: java/net/httpclient/MaxStreams.java failed once
dfuchs
parents: 52121
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     4
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     8
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    13
 * accompanied this code).
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    14
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    18
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    21
 * questions.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    22
 */
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    23
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    24
/*
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    25
 * @test
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    26
 * @bug 8196389
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    27
 * @summary Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    28
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    29
 * @modules java.base/sun.net.www.http
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    30
 *          java.net.http/jdk.internal.net.http.common
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.frame
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    32
 *          java.net.http/jdk.internal.net.http.hpack
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    33
 *          java.logging
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    34
 *          jdk.httpserver
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    35
 * @library /test/lib http2/server
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    36
 * @build Http2TestServer
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    37
 * @build jdk.test.lib.net.SimpleSSLContext
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    38
 * @run testng/othervm -ea -esa MaxStreams
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    39
 */
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    40
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    41
import java.io.IOException;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    42
import java.io.InputStream;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    43
import java.io.OutputStream;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    44
import java.net.InetAddress;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    45
import java.net.InetSocketAddress;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    46
import java.net.URI;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    47
import java.util.List;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    48
import java.util.LinkedList;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    49
import java.util.Properties;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    50
import java.util.concurrent.atomic.AtomicInteger;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    51
import java.util.concurrent.CompletableFuture;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    52
import java.util.concurrent.CompletionException;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    53
import java.util.concurrent.CountDownLatch;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    54
import java.util.concurrent.Executors;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    55
import java.util.concurrent.ExecutorService;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    56
import java.util.concurrent.Semaphore;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    57
import javax.net.ssl.SSLContext;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    58
import java.net.http.HttpClient;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    59
import java.net.http.HttpRequest;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    60
import java.net.http.HttpResponse;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    61
import java.net.http.HttpResponse.BodyHandler;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    62
import java.net.http.HttpResponse.BodyHandlers;
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    63
import jdk.test.lib.net.SimpleSSLContext;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    64
import org.testng.annotations.AfterTest;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    65
import org.testng.annotations.BeforeTest;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    66
import org.testng.annotations.DataProvider;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    67
import org.testng.annotations.Test;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    68
import static java.nio.charset.StandardCharsets.UTF_8;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    69
import static java.net.http.HttpResponse.BodyHandlers.discarding;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    70
import static org.testng.Assert.assertEquals;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    71
import static org.testng.Assert.assertFalse;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    72
import static org.testng.Assert.fail;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    73
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    74
public class MaxStreams {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    75
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    76
    Http2TestServer http2TestServer;   // HTTP/2 ( h2c )
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    77
    Http2TestServer https2TestServer;   // HTTP/2 ( h2 )
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    78
    final Http2FixedHandler handler = new Http2FixedHandler();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    79
    SSLContext ctx;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    80
    String http2FixedURI;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    81
    String https2FixedURI;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    82
    volatile CountDownLatch latch;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    83
    ExecutorService exec;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    84
    final Semaphore canStartTestRun = new Semaphore(1);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    85
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    86
    // we send an initial warm up request, then MAX_STREAMS+1 requests
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    87
    // in parallel. The last of them should hit the limit.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    88
    // Then we wait for all the responses and send a further request
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    89
    // which should succeed. The server should see (and respond to)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    90
    // MAX_STREAMS+2 requests per test run.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    91
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    92
    static final int MAX_STREAMS = 10;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    93
    static final String RESPONSE = "Hello world";
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    94
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    95
    @DataProvider(name = "uris")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    96
    public Object[][] variants() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    97
        return new Object[][]{
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    98
                {http2FixedURI},
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    99
                {https2FixedURI},
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   100
                {http2FixedURI},
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   101
                {https2FixedURI}
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   102
        };
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   103
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   104
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   105
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   106
    @Test(dataProvider = "uris", timeOut=20000)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   107
    void testAsString(String uri) throws Exception {
53525
c55508666f24 8217882: java/net/httpclient/MaxStreams.java failed once
dfuchs
parents: 52121
diff changeset
   108
        System.err.println("Semaphore acquire");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   109
        canStartTestRun.acquire();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   110
        latch = new CountDownLatch(1);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   111
        handler.setLatch(latch);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   112
        HttpClient client = HttpClient.newBuilder().sslContext(ctx).build();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   113
        List<CompletableFuture<HttpResponse<String>>> responses = new LinkedList<>();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   114
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   115
        HttpRequest request = HttpRequest.newBuilder(URI.create(uri))
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   116
                                         .version(HttpClient.Version.HTTP_2)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   117
                                         .GET()
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   118
                                         .build();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   119
        // send warmup to ensure we only have one Http2Connection
53525
c55508666f24 8217882: java/net/httpclient/MaxStreams.java failed once
dfuchs
parents: 52121
diff changeset
   120
        System.err.println("Sending warmup request");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   121
        HttpResponse<String> warmup = client.send(request, BodyHandlers.ofString());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   122
        if (warmup.statusCode() != 200 || !warmup.body().equals(RESPONSE))
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   123
            throw new RuntimeException();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   124
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   125
        for (int i=0;i<MAX_STREAMS+1; i++) {
53525
c55508666f24 8217882: java/net/httpclient/MaxStreams.java failed once
dfuchs
parents: 52121
diff changeset
   126
            System.err.println("Sending request " + i);
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   127
            responses.add(client.sendAsync(request, BodyHandlers.ofString()));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   128
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   129
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   130
        // wait until we get local exception before allow server to proceed
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   131
        try {
53525
c55508666f24 8217882: java/net/httpclient/MaxStreams.java failed once
dfuchs
parents: 52121
diff changeset
   132
            System.err.println("Waiting for first exception");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   133
            CompletableFuture.anyOf(responses.toArray(new CompletableFuture<?>[0])).join();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   134
        } catch (Exception ee) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   135
            System.err.println("Expected exception 1 " + ee);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   136
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   137
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   138
        latch.countDown();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   139
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   140
        // check the first MAX_STREAMS requests succeeded
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   141
        try {
53525
c55508666f24 8217882: java/net/httpclient/MaxStreams.java failed once
dfuchs
parents: 52121
diff changeset
   142
            System.err.println("Waiting for second exception");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   143
            CompletableFuture.allOf(responses.toArray(new CompletableFuture<?>[0])).join();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   144
            System.err.println("Did not get Expected exception 2 ");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   145
        } catch (Exception ee) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   146
            System.err.println("Expected exception 2 " + ee);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   147
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   148
        int count = 0;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   149
        int failures = 0;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   150
        for (CompletableFuture<HttpResponse<String>> cf : responses) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   151
            HttpResponse<String> r = null;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   152
            try {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   153
                count++;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   154
                r = cf.join();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   155
                if (r.statusCode() != 200 || !r.body().equals(RESPONSE))
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   156
                    throw new RuntimeException();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   157
            } catch (Throwable t) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   158
                failures++;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   159
                System.err.printf("Failure %d at count %d\n", failures, count);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   160
                System.err.println(t);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   161
                t.printStackTrace();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   162
            }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   163
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   164
        if (failures != 1) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   165
            String msg = "Expected 1 failure. Got " + failures;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   166
            throw new RuntimeException(msg);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   167
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   168
53525
c55508666f24 8217882: java/net/httpclient/MaxStreams.java failed once
dfuchs
parents: 52121
diff changeset
   169
        System.err.println("Sending last request");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   170
        // make sure it succeeds now as number of streams == 0 now
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   171
        HttpResponse<String> warmdown = client.send(request, BodyHandlers.ofString());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   172
        if (warmdown.statusCode() != 200 || !warmdown.body().equals(RESPONSE))
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   173
            throw new RuntimeException();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   174
        System.err.println("Test OK");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   175
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   176
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   177
    @BeforeTest
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   178
    public void setup() throws Exception {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   179
        ctx = (new SimpleSSLContext()).get();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   180
        exec = Executors.newCachedThreadPool();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   181
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   182
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   183
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   184
        Properties props = new Properties();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   185
        props.setProperty("http2server.settings.max_concurrent_streams", Integer.toString(MAX_STREAMS));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   186
        http2TestServer = new Http2TestServer("localhost", false, 0, exec, 10, props, null);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   187
        http2TestServer.addHandler(handler, "/http2/fixed");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   188
        http2FixedURI = "http://" + http2TestServer.serverAuthority()+ "/http2/fixed";
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   189
        http2TestServer.start();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   190
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   191
        https2TestServer = new Http2TestServer("localhost", true, 0, exec, 10, props, ctx);
53525
c55508666f24 8217882: java/net/httpclient/MaxStreams.java failed once
dfuchs
parents: 52121
diff changeset
   192
        https2TestServer.addHandler(handler, "/https2/fixed");
c55508666f24 8217882: java/net/httpclient/MaxStreams.java failed once
dfuchs
parents: 52121
diff changeset
   193
        https2FixedURI = "https://" + https2TestServer.serverAuthority()+ "/https2/fixed";
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   194
        https2TestServer.start();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   195
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   196
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   197
    @AfterTest
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   198
    public void teardown() throws Exception {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   199
        System.err.println("Stopping test server now");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   200
        http2TestServer.stop();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   201
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   202
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   203
    class Http2FixedHandler implements Http2Handler {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   204
        final AtomicInteger counter = new AtomicInteger(0);
53525
c55508666f24 8217882: java/net/httpclient/MaxStreams.java failed once
dfuchs
parents: 52121
diff changeset
   205
        volatile CountDownLatch latch;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   206
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   207
        synchronized void setLatch(CountDownLatch latch) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   208
            this.latch = latch;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   209
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   210
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   211
        synchronized CountDownLatch getLatch() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   212
            return latch;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   213
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   214
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   215
        @Override
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   216
        public void handle(Http2TestExchange t) throws IOException {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   217
            int c = -1;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   218
            try (InputStream is = t.getRequestBody();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   219
                 OutputStream os = t.getResponseBody()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   220
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   221
                is.readAllBytes();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   222
                c = counter.getAndIncrement();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   223
                if (c > 0 && c <= MAX_STREAMS) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   224
                    // Wait for latch.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   225
                    try {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   226
                        // don't send any replies until all requests are sent
53525
c55508666f24 8217882: java/net/httpclient/MaxStreams.java failed once
dfuchs
parents: 52121
diff changeset
   227
                        System.err.println("Latch await");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   228
                        getLatch().await();
53525
c55508666f24 8217882: java/net/httpclient/MaxStreams.java failed once
dfuchs
parents: 52121
diff changeset
   229
                        System.err.println("Latch resume");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   230
                    } catch (InterruptedException ee) {}
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   231
                }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   232
                t.sendResponseHeaders(200, RESPONSE.length());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   233
                os.write(RESPONSE.getBytes());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   234
            } finally {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   235
                // client issues MAX_STREAMS + 3 requests in total
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   236
                // but server should only see MAX_STREAMS + 2 in total. One is rejected by client
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   237
                // counter c captured before increment so final value is MAX_STREAMS + 1
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   238
                if (c == MAX_STREAMS + 1) {
53525
c55508666f24 8217882: java/net/httpclient/MaxStreams.java failed once
dfuchs
parents: 52121
diff changeset
   239
                    System.err.println("Semaphore release");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   240
                    counter.set(0);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   241
                    canStartTestRun.release();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   242
                }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   243
            }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   244
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   245
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   246
}