test/jdk/java/net/httpclient/websocket/ConnectionHandoverTest.java
author chegar
Wed, 02 May 2018 15:47:57 +0100
branchhttp-client-branch
changeset 56507 2294c51eae30
parent 56451 9585061fdb04
permissions -rw-r--r--
http-client-branch: Merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     1
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     4
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     8
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    13
 * accompanied this code).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    14
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    18
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    21
 * questions.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    22
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    23
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    24
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    25
 * @test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    26
 * @bug 8164625
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
 * @summary Verifies HttpClient yields the connection to the WebSocket
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
 * @build DummyWebSocketServer
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    29
 * @run main/othervm -Djdk.httpclient.HttpClient.log=trace ConnectionHandoverTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
import java.net.URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
import java.net.http.WebSocket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
public class ConnectionHandoverTest {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
    /*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
     * An I/O channel associated with the connection is closed by WebSocket.abort().
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
     * If this connection is returned to the connection pool, then the second
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
     * attempt to use it would fail with a ClosedChannelException.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
     * The assumption is that since the WebSocket client is connecting to the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
     * same URI, the pooled connection is to be used.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
    public static void main(String[] args) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
            server.open();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
            URI uri = server.getURI();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
            WebSocket.Builder webSocketBuilder =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
                    HttpClient.newHttpClient().newWebSocketBuilder();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
            WebSocket ws1 = webSocketBuilder
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
                    .buildAsync(uri, new WebSocket.Listener() { }).join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
            ws1.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
            WebSocket ws2 = webSocketBuilder
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
                    .buildAsync(uri, new WebSocket.Listener() { }).join(); // Exception here if the connection was pooled
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
            ws2.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
}