test/jdk/java/net/httpclient/SmallTimeout.java
author chegar
Wed, 07 Feb 2018 14:17:24 +0000
branchhttp-client-branch
changeset 56089 42208b2f224e
parent 56082 1da51fab3032
child 56092 fd85b2bf2b0d
permissions -rw-r--r--
http-client-branch: move to standard package and module name
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
     1
/*
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
     4
 *
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
     8
 *
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    14
 *
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    18
 *
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    21
 * questions.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    22
 */
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    23
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    24
import java.io.IOException;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    25
import java.net.ServerSocket;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    26
import java.net.URI;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    27
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    28
import java.net.http.HttpRequest;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    29
import java.net.http.HttpResponse;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    30
import java.net.http.HttpTimeoutException;
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    31
import java.time.Duration;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    32
import java.util.concurrent.CompletableFuture;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    33
import java.util.concurrent.ExecutorService;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    34
import java.util.concurrent.Executors;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    35
import java.util.concurrent.LinkedBlockingQueue;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    36
import static java.lang.System.out;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    37
import static java.net.http.HttpResponse.BodyHandler.replace;
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    38
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    39
/**
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    40
 * @test
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    41
 * @bug 8178147
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    42
 * @summary Ensures that small timeouts do not cause hangs due to race conditions
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    43
 * @run main/othervm -Djava.net.http.internal.common.DEBUG=true SmallTimeout
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    44
 */
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    45
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    46
// To enable logging use. Not enabled by default as it changes the dynamics
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    47
// of the test.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    48
// @run main/othervm -Djdk.httpclient.HttpClient.log=all,frames:all SmallTimeout
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    49
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    50
public class SmallTimeout {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    51
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    52
    static int[] TIMEOUTS = {2, 1, 3, 2, 100, 1};
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    53
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    54
    // A queue for placing timed out requests so that their order can be checked.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    55
    static LinkedBlockingQueue<HttpResult> queue = new LinkedBlockingQueue<>();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    56
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    57
    static final class HttpResult {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    58
         final HttpRequest request;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    59
         final Throwable   failed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    60
         HttpResult(HttpRequest request, Throwable   failed) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    61
             this.request = request;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    62
             this.failed = failed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    63
         }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    64
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    65
         static HttpResult of(HttpRequest request) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    66
             return new HttpResult(request, null);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    67
         }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    68
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    69
         static HttpResult of(HttpRequest request, Throwable t) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    70
             return new HttpResult(request, t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    71
         }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    72
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    73
    }
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    74
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    75
    static volatile boolean error;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    76
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    77
    public static void main(String[] args) throws Exception {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    78
        HttpClient client = HttpClient.newHttpClient();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    79
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    80
        try (ServerSocket ss = new ServerSocket(0, 20)) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    81
            int port = ss.getLocalPort();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    82
            URI uri = new URI("http://127.0.0.1:" + port + "/");
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    83
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    84
            HttpRequest[] requests = new HttpRequest[TIMEOUTS.length];
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    85
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    86
            out.println("--- TESTING Async");
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    87
            for (int i = 0; i < TIMEOUTS.length; i++) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    88
                requests[i] = HttpRequest.newBuilder(uri)
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    89
                                         .timeout(Duration.ofMillis(TIMEOUTS[i]))
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    90
                                         .GET()
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    91
                                         .build();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    92
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    93
                final HttpRequest req = requests[i];
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    94
                CompletableFuture<HttpResponse<Object>> response = client
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents: 48083
diff changeset
    95
                    .sendAsync(req, replace(null))
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    96
                    .whenComplete((HttpResponse<Object> r, Throwable t) -> {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    97
                        Throwable cause = null;
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    98
                        if (r != null) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    99
                            out.println("Unexpected response: " + r);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   100
                            cause = new RuntimeException("Unexpected response");
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   101
                            error = true;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   102
                        }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   103
                        if (t != null) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   104
                            if (!(t.getCause() instanceof HttpTimeoutException)) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   105
                                out.println("Wrong exception type:" + t.toString());
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   106
                                Throwable c = t.getCause() == null ? t : t.getCause();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   107
                                c.printStackTrace();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   108
                                cause = c;
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   109
                                error = true;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   110
                            } else {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   111
                                out.println("Caught expected timeout: " + t.getCause());
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   112
                            }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   113
                        }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   114
                        if (t == null && r == null) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   115
                            out.println("Both response and throwable are null!");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   116
                            cause = new RuntimeException("Both response and throwable are null!");
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   117
                            error = true;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   118
                        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   119
                        queue.add(HttpResult.of(req,cause));
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   120
                    });
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   121
            }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   122
            System.out.println("All requests submitted. Waiting ...");
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   123
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   124
            checkReturn(requests);
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   125
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   126
            if (error)
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   127
                throw new RuntimeException("Failed. Check output");
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   128
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   129
            // Repeat blocking in separate threads. Use queue to wait.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   130
            out.println("--- TESTING Sync");
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   131
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   132
            // For running blocking response tasks
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   133
            ExecutorService executor = Executors.newCachedThreadPool();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   134
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   135
            for (int i = 0; i < TIMEOUTS.length; i++) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   136
                requests[i] = HttpRequest.newBuilder(uri)
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   137
                                         .timeout(Duration.ofMillis(TIMEOUTS[i]))
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   138
                                         .GET()
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   139
                                         .build();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   140
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   141
                final HttpRequest req = requests[i];
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   142
                executor.execute(() -> {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   143
                    Throwable cause = null;
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   144
                    try {
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents: 48083
diff changeset
   145
                        client.send(req, replace(null));
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   146
                    } catch (HttpTimeoutException e) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   147
                        out.println("Caught expected timeout: " + e);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   148
                    } catch (Throwable ee) {
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   149
                        Throwable c = ee.getCause() == null ? ee : ee.getCause();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   150
                        c.printStackTrace();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   151
                        cause = c;
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   152
                        error = true;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   153
                    } finally {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   154
                        queue.offer(HttpResult.of(req, cause));
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   155
                    }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   156
                });
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   157
            }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   158
            System.out.println("All requests submitted. Waiting ...");
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   159
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   160
            checkReturn(requests);
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   161
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   162
            executor.shutdownNow();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   163
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   164
            if (error)
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   165
                throw new RuntimeException("Failed. Check output");
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   166
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   167
        }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   168
    }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   169
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   170
    static void checkReturn(HttpRequest[] requests) throws InterruptedException {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   171
        // wait for exceptions and check order
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   172
        boolean ok = true;
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   173
        for (int j = 0; j < TIMEOUTS.length; j++) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   174
            HttpResult res = queue.take();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   175
            HttpRequest req = res.request;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   176
            out.println("Got request from queue " + req + ", order: " + getRequest(req, requests)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   177
                         + (res.failed == null ? "" : " failed: " + res.failed));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   178
            ok = ok && res.failed == null;
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   179
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   180
        out.println("Return " + (ok ? "ok" : "nok"));
44639
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   181
    }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   182
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   183
    /** Returns the index of the request in the array. */
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   184
    static String getRequest(HttpRequest req, HttpRequest[] requests) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   185
        for (int i=0; i<requests.length; i++) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   186
            if (req == requests[i]) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   187
                return "r" + i;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   188
            }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   189
        }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   190
        throw new AssertionError("Unknown request: " + req);
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   191
    }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   192
}