test/jdk/java/net/httpclient/ByteArrayPublishers.java
author chegar
Fri, 27 Sep 2019 09:55:35 +0100
changeset 58365 73950479184b
parent 55520 33bb8c970770
permissions -rw-r--r--
8231504: Update networking tests to avoid implicit dependency on the system proxies Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55520
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
     1
/*
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
     4
 *
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
     8
 *
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    13
 * accompanied this code).
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    14
 *
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    18
 *
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    21
 * questions.
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    22
 */
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    23
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    24
/*
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    25
 * @test
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    26
 * @bug 8222968
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    27
 * @summary ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    28
 * @run main/othervm ByteArrayPublishers
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    29
 */
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    30
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    31
import java.net.InetAddress;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    32
import java.net.URI;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    33
import java.net.URISyntaxException;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    34
import java.net.http.HttpClient;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    35
import java.net.http.HttpRequest;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    36
import java.net.http.HttpResponse;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    37
import java.util.ArrayList;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    38
import java.util.concurrent.LinkedBlockingQueue;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    39
import java.util.concurrent.CompletableFuture;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    40
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    41
import com.sun.net.httpserver.HttpExchange;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    42
import com.sun.net.httpserver.HttpServer;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    43
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    44
import java.io.IOException;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    45
import java.net.InetSocketAddress;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    46
import java.nio.charset.StandardCharsets;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    47
import static java.net.http.HttpRequest.BodyPublisher;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    48
import static java.net.http.HttpRequest.BodyPublishers;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    49
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    50
public class ByteArrayPublishers {
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    51
    private static final BodyPublisher BODY_PUBLISHER =
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    52
        BodyPublishers.ofByteArray("abcdefghijklmnopqrstuvwxyz".getBytes());
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    53
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    54
    static int LOOPS = 100;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    55
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    56
    public static void main(String[] args) throws Exception {
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    57
        HttpServer server = null;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    58
        try {
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    59
            InetAddress loopBack = InetAddress.getLoopbackAddress();
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    60
            String lpBackStr = loopBack.getHostAddress();
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    61
            InetSocketAddress serverAddr = new InetSocketAddress(loopBack, 0);
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    62
            server = HttpServer.create(serverAddr, 500);
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    63
            server.createContext("/", (HttpExchange e) -> {
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    64
                    e.getRequestBody().readAllBytes();
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    65
                    String response = "Hello world";
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    66
                    e.sendResponseHeaders(200, response.length());
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    67
                    e.getResponseBody().write(response.getBytes(StandardCharsets.ISO_8859_1));
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    68
                    e.close();
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    69
            });
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    70
            server.start();
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    71
            var address = server.getAddress();
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    72
            URI dest = new URI("http://" + lpBackStr + ":"
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    73
                + Integer.toString(address.getPort()) + "/");
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    74
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    75
            HttpClient client = createClient();
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    76
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    77
            ArrayList<CompletableFuture<HttpResponse<Void>>> futures = new ArrayList<>(LOOPS);
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    78
            LinkedBlockingQueue<Object> results = new LinkedBlockingQueue<Object>();
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    79
            for (int i=0;i<LOOPS;i++) {
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    80
                futures.add(
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    81
                    client.sendAsync(createRequest(dest), HttpResponse.BodyHandlers.discarding())
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    82
                          .handle((v, t) -> {
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    83
                                if (t != null)
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    84
                                    results.add(t);
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    85
                                else
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    86
                                    results.add(v);
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    87
                                return null;
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    88
                          }));
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    89
            }
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    90
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    91
            for (int i=0; i<LOOPS; i++) {
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    92
                Object o = results.take();
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    93
                if (o instanceof Exception) {
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    94
                    throw new RuntimeException((Exception)o);
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    95
                }
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    96
            }
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    97
        } finally {
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    98
            server.stop(1);
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
    99
        }
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   100
    }
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   101
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   102
    private static HttpRequest createRequest(URI uri) throws URISyntaxException {
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   103
        HttpRequest.Builder builder = HttpRequest.newBuilder(uri)
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   104
                .method("POST", BODY_PUBLISHER)
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   105
                .version(HttpClient.Version.HTTP_1_1);
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   106
        builder.header("content-type", "text/plain");
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   107
        return builder.build();
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   108
    }
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   109
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   110
    private static HttpClient createClient() {
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   111
        return HttpClient.newBuilder()
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   112
                .version(HttpClient.Version.HTTP_1_1)
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   113
                .build();
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   114
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   115
    }
33bb8c970770 8222968: ByteArrayPublisher is not thread-safe resulting in broken re-use of HttpRequests
michaelm
parents:
diff changeset
   116
}