jdk/test/java/net/httpclient/SmallTimeout.java
author dfuchs
Tue, 11 Apr 2017 16:32:38 +0100
changeset 44639 5c2838d882a5
permissions -rw-r--r--
8178147: Race conditions in timeout handling code in http/2 incubator client Summary: Timeout handling code (in particular cancel(IOException)) is modified to take into account cancellation arising before exchange implementation is established or response completable future is created. TimeoutEvent::compareTo method is modified to be consistent with TimeoutEvent::equals. Reviewed-by: prappo, chegar, michaelm
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;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    27
import jdk.incubator.http.HttpClient;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    28
import jdk.incubator.http.HttpRequest;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    29
import jdk.incubator.http.HttpResponse;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    30
import jdk.incubator.http.HttpTimeoutException;
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;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    37
import static jdk.incubator.http.HttpResponse.BodyHandler.discard;
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
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    43
 * @run main/othervm SmallTimeout
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.
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    55
    static LinkedBlockingQueue<HttpRequest> queue = new LinkedBlockingQueue<>();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    56
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    57
    static volatile boolean error;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    58
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    59
    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
    60
        HttpClient client = HttpClient.newHttpClient();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    61
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    62
        try (ServerSocket ss = new ServerSocket(0, 20)) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    63
            int port = ss.getLocalPort();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    64
            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
    65
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    66
            HttpRequest[] requests = new HttpRequest[TIMEOUTS.length];
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    67
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    68
            out.println("--- TESTING Async");
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    69
            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
    70
                requests[i] = HttpRequest.newBuilder(uri)
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    71
                                         .timeout(Duration.ofMillis(TIMEOUTS[i]))
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    72
                                         .GET()
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    73
                                         .build();
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
                final HttpRequest req = requests[i];
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    76
                CompletableFuture<HttpResponse<Object>> response = client
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    77
                    .sendAsync(req, discard(null))
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    78
                    .whenComplete((HttpResponse<Object> r, Throwable t) -> {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    79
                        if (r != null) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    80
                            out.println("Unexpected response: " + r);
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    81
                            error = true;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    82
                        }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    83
                        if (t != null) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    84
                            if (!(t.getCause() instanceof HttpTimeoutException)) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    85
                                out.println("Wrong exception type:" + t.toString());
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    86
                                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
    87
                                c.printStackTrace();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    88
                                error = true;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    89
                            } else {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    90
                                out.println("Caught expected timeout: " + t.getCause());
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    91
                            }
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
                        if (t == null && r == null) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    94
                            out.println("Both response and throwable are null!");
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    95
                            error = true;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    96
                        }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    97
                        queue.add(req);
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    98
                    });
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
    99
            }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   100
            System.out.println("All requests submitted. Waiting ...");
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   101
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   102
            checkReturn(requests);
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   103
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   104
            if (error)
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   105
                throw new RuntimeException("Failed. Check output");
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   106
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   107
            // 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
   108
            out.println("--- TESTING Sync");
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   109
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   110
            // For running blocking response tasks
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   111
            ExecutorService executor = Executors.newCachedThreadPool();
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
            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
   114
                requests[i] = HttpRequest.newBuilder(uri)
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   115
                                         .timeout(Duration.ofMillis(TIMEOUTS[i]))
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   116
                                         .GET()
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   117
                                         .build();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   118
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   119
                final HttpRequest req = requests[i];
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   120
                executor.execute(() -> {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   121
                    try {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   122
                        client.send(req, discard(null));
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   123
                    } catch (HttpTimeoutException e) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   124
                        out.println("Caught expected timeout: " + e);
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   125
                        queue.offer(req);
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   126
                    } catch (IOException | InterruptedException ee) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   127
                        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
   128
                        c.printStackTrace();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   129
                        error = true;
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   130
                    }
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
            }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   133
            System.out.println("All requests submitted. Waiting ...");
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
            checkReturn(requests);
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   136
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   137
            executor.shutdownNow();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   138
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   139
            if (error)
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   140
                throw new RuntimeException("Failed. Check output");
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   141
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   142
        } finally {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   143
            ((ExecutorService) client.executor()).shutdownNow();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   144
        }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   145
    }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   146
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   147
    static void checkReturn(HttpRequest[] requests) throws InterruptedException {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   148
        // wait for exceptions and check order
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   149
        for (int j = 0; j < TIMEOUTS.length; j++) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   150
            HttpRequest req = queue.take();
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   151
            out.println("Got request from queue " + req + ", order: " + getRequest(req, requests));
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   152
        }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   153
        out.println("Return ok");
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   154
    }
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
    /** 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
   157
    static String getRequest(HttpRequest req, HttpRequest[] requests) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   158
        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
   159
            if (req == requests[i]) {
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   160
                return "r" + i;
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
        }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   163
        throw new AssertionError("Unknown request: " + req);
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   164
    }
5c2838d882a5 8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
diff changeset
   165
}