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