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