test/jdk/java/net/httpclient/websocket/Abort.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 58289 3a79d4cccbcb
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     1
/*
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
49765
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
 * @build DummyWebSocketServer
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
 * @run testng/othervm
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
 *      -Djdk.internal.httpclient.websocket.debug=true
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    29
 *       Abort
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 org.testng.annotations.Test;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
import java.net.ProtocolException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
import java.net.http.WebSocket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
import java.nio.ByteBuffer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
import java.util.Arrays;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
import java.util.List;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
import java.util.concurrent.CompletableFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
import java.util.concurrent.CompletionStage;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import java.util.concurrent.TimeUnit;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import java.util.concurrent.TimeoutException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
import static java.net.http.HttpClient.newHttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
import static java.net.http.WebSocket.NORMAL_CLOSURE;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
import static org.testng.Assert.assertEquals;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
import static org.testng.Assert.assertThrows;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
import static org.testng.Assert.assertTrue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
import static org.testng.Assert.fail;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
public class Abort {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
    private static final Class<NullPointerException> NPE = NullPointerException.class;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
    private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
    private static final Class<IOException> IOE = IOException.class;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
    public void onOpenThenAbort() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
        int[] bytes = new int[]{
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
                0x88, 0x00, // opcode=close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
        };
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    64
        try (var server = Support.serverWithCannedData(bytes)) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    65
            server.open();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    66
            // messages are available
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    67
            MockListener listener = new MockListener() {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    68
                @Override
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    69
                protected void onOpen0(WebSocket webSocket) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    70
                    // unbounded request
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    71
                    webSocket.request(Long.MAX_VALUE);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    72
                    webSocket.abort();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    73
                }
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    74
            };
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    75
            var webSocket = newHttpClient().newWebSocketBuilder()
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    76
                    .buildAsync(server.getURI(), listener)
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    77
                    .join();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    78
            try {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    79
                TimeUnit.SECONDS.sleep(5);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    80
                List<MockListener.Invocation> inv = listener.invocationsSoFar();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    81
                // no more invocations after onOpen as WebSocket was aborted
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    82
                assertEquals(inv, List.of(MockListener.Invocation.onOpen(webSocket)));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    83
            } finally {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
                webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
            }
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    86
        }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
    public void onOpenThenOnTextThenAbort() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
        int[] bytes = new int[]{
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
                0x81, 0x00, // opcode=text, fin=true
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
                0x88, 0x00, // opcode=close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
        };
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    95
        try (var server = Support.serverWithCannedData(bytes)) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    96
            server.open();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    97
            MockListener listener = new MockListener() {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    98
                @Override
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
    99
                protected void onOpen0(WebSocket webSocket) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   100
                    // unbounded request
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   101
                    webSocket.request(Long.MAX_VALUE);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   102
                }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   104
                @Override
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   105
                protected CompletionStage<?> onText0(WebSocket webSocket,
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   106
                                                     CharSequence message,
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   107
                                                     boolean last) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   108
                    webSocket.abort();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   109
                    return super.onText0(webSocket, message, last);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   110
                }
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   111
            };
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   112
            var webSocket = newHttpClient().newWebSocketBuilder()
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   113
                    .buildAsync(server.getURI(), listener)
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   114
                    .join();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   115
            try {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   116
                TimeUnit.SECONDS.sleep(5);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   117
                List<MockListener.Invocation> inv = listener.invocationsSoFar();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   118
                // no more invocations after onOpen, onBinary as WebSocket was aborted
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   119
                List<MockListener.Invocation> expected = List.of(
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   120
                        MockListener.Invocation.onOpen(webSocket),
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   121
                        MockListener.Invocation.onText(webSocket, "", true));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   122
                assertEquals(inv, expected);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   123
            } finally {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
                webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
            }
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   126
        }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
    public void onOpenThenOnBinaryThenAbort() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
        int[] bytes = new int[]{
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
                0x82, 0x00, // opcode=binary, fin=true
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
                0x88, 0x00, // opcode=close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
        };
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   135
        try (var server = Support.serverWithCannedData(bytes)) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   136
            server.open();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   137
            MockListener listener = new MockListener() {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   138
                @Override
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   139
                protected void onOpen0(WebSocket webSocket) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   140
                    // unbounded request
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   141
                    webSocket.request(Long.MAX_VALUE);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   142
                }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   144
                @Override
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   145
                protected CompletionStage<?> onBinary0(WebSocket webSocket,
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   146
                                                       ByteBuffer message,
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   147
                                                       boolean last) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   148
                    webSocket.abort();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   149
                    return super.onBinary0(webSocket, message, last);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   150
                }
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   151
            };
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   152
            var webSocket = newHttpClient().newWebSocketBuilder()
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   153
                    .buildAsync(server.getURI(), listener)
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   154
                    .join();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   155
            try {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   156
                TimeUnit.SECONDS.sleep(5);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   157
                List<MockListener.Invocation> inv = listener.invocationsSoFar();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   158
                // no more invocations after onOpen, onBinary as WebSocket was aborted
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   159
                List<MockListener.Invocation> expected = List.of(
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   160
                        MockListener.Invocation.onOpen(webSocket),
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   161
                        MockListener.Invocation.onBinary(webSocket, ByteBuffer.allocate(0), true));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   162
                assertEquals(inv, expected);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   163
            } finally {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
                webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
            }
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   166
        }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
    public void onOpenThenOnPingThenAbort() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
        int[] bytes = {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
                0x89, 0x00, // opcode=ping
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
                0x88, 0x00, // opcode=close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
        };
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   175
        try (var server = Support.serverWithCannedData(bytes)) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   176
            server.open();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   177
            MockListener listener = new MockListener() {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   178
                @Override
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   179
                protected void onOpen0(WebSocket webSocket) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   180
                    // unbounded request
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   181
                    webSocket.request(Long.MAX_VALUE);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   182
                }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   184
                @Override
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   185
                protected CompletionStage<?> onPing0(WebSocket webSocket,
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   186
                                                     ByteBuffer message) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   187
                    webSocket.abort();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   188
                    return super.onPing0(webSocket, message);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   189
                }
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   190
            };
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   191
            var webSocket = newHttpClient().newWebSocketBuilder()
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   192
                    .buildAsync(server.getURI(), listener)
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   193
                    .join();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   194
            try {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   195
                TimeUnit.SECONDS.sleep(5);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   196
                List<MockListener.Invocation> inv = listener.invocationsSoFar();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   197
                // no more invocations after onOpen, onPing as WebSocket was aborted
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   198
                List<MockListener.Invocation> expected = List.of(
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   199
                        MockListener.Invocation.onOpen(webSocket),
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   200
                        MockListener.Invocation.onPing(webSocket, ByteBuffer.allocate(0)));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   201
                assertEquals(inv, expected);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   202
            } finally {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
                webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
            }
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   205
        }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   209
    public void onOpenThenOnPongThenAbort() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
        int[] bytes = {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
                0x8a, 0x00, // opcode=pong
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
                0x88, 0x00, // opcode=close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   213
        };
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   214
        try (var server = Support.serverWithCannedData(bytes)) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   215
            server.open();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   216
            MockListener listener = new MockListener() {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   217
                @Override
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   218
                protected void onOpen0(WebSocket webSocket) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   219
                    // unbounded request
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   220
                    webSocket.request(Long.MAX_VALUE);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   221
                }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   222
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   223
                @Override
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   224
                protected CompletionStage<?> onPong0(WebSocket webSocket,
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   225
                                                     ByteBuffer message) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   226
                    webSocket.abort();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   227
                    return super.onPong0(webSocket, message);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   228
                }
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   229
            };
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   230
            var webSocket = newHttpClient().newWebSocketBuilder()
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   231
                    .buildAsync(server.getURI(), listener)
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   232
                    .join();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   233
            try {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   234
                TimeUnit.SECONDS.sleep(5);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   235
                List<MockListener.Invocation> inv = listener.invocationsSoFar();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   236
                // no more invocations after onOpen, onPong as WebSocket was aborted
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   237
                List<MockListener.Invocation> expected = List.of(
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   238
                        MockListener.Invocation.onOpen(webSocket),
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   239
                        MockListener.Invocation.onPong(webSocket, ByteBuffer.allocate(0)));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   240
                assertEquals(inv, expected);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   241
            } finally {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   242
                webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   243
            }
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   244
        }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   245
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   246
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   247
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   248
    public void onOpenThenOnCloseThenAbort() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   249
        int[] bytes = {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   250
                0x88, 0x00, // opcode=close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   251
                0x8a, 0x00, // opcode=pong
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   252
        };
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   253
        try (var server = Support.serverWithCannedData(bytes)) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   254
            server.open();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   255
            MockListener listener = new MockListener() {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   256
                @Override
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   257
                protected void onOpen0(WebSocket webSocket) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   258
                    // unbounded request
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   259
                    webSocket.request(Long.MAX_VALUE);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   260
                }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   261
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   262
                @Override
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   263
                protected CompletionStage<?> onClose0(WebSocket webSocket,
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   264
                                                      int statusCode,
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   265
                                                      String reason) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   266
                    webSocket.abort();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   267
                    return super.onClose0(webSocket, statusCode, reason);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   268
                }
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   269
            };
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   270
            var webSocket = newHttpClient().newWebSocketBuilder()
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   271
                    .buildAsync(server.getURI(), listener)
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   272
                    .join();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   273
            try {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   274
                TimeUnit.SECONDS.sleep(5);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   275
                List<MockListener.Invocation> inv = listener.invocationsSoFar();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   276
                // no more invocations after onOpen, onClose
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   277
                List<MockListener.Invocation> expected = List.of(
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   278
                        MockListener.Invocation.onOpen(webSocket),
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   279
                        MockListener.Invocation.onClose(webSocket, 1005, ""));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   280
                assertEquals(inv, expected);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   281
            } finally {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   282
                webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   283
            }
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   284
        }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   285
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   286
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   287
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   288
    public void onOpenThenOnErrorThenAbort() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   289
        // A header of 128 bytes long Ping (which is a protocol error)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   290
        int[] badPingHeader = new int[]{0x89, 0x7e, 0x00, 0x80};
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   291
        int[] closeMessage = new int[]{0x88, 0x00};
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   292
        int[] bytes = new int[badPingHeader.length + 128 + closeMessage.length];
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   293
        System.arraycopy(badPingHeader, 0, bytes, 0, badPingHeader.length);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   294
        System.arraycopy(closeMessage, 0, bytes, badPingHeader.length + 128, closeMessage.length);
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   295
        try (var server = Support.serverWithCannedData(bytes)) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   296
            server.open();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   297
            MockListener listener = new MockListener() {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   298
                @Override
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   299
                protected void onOpen0(WebSocket webSocket) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   300
                    // unbounded request
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   301
                    webSocket.request(Long.MAX_VALUE);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   302
                }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   303
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   304
                @Override
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   305
                protected void onError0(WebSocket webSocket, Throwable error) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   306
                    webSocket.abort();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   307
                    super.onError0(webSocket, error);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   308
                }
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   309
            };
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   310
            var webSocket = newHttpClient().newWebSocketBuilder()
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   311
                    .buildAsync(server.getURI(), listener)
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   312
                    .join();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   313
            try {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   314
                TimeUnit.SECONDS.sleep(5);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   315
                List<MockListener.Invocation> inv = listener.invocationsSoFar();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   316
                // no more invocations after onOpen, onError
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   317
                List<MockListener.Invocation> expected = List.of(
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   318
                        MockListener.Invocation.onOpen(webSocket),
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   319
                        MockListener.Invocation.onError(webSocket, ProtocolException.class));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   320
                System.out.println("actual invocations:" + Arrays.toString(inv.toArray()));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   321
                assertEquals(inv, expected);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   322
            } finally {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   323
                webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   324
            }
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   325
        }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   326
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   327
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   328
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   329
    public void immediateAbort() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   330
        CompletableFuture<Void> messageReceived = new CompletableFuture<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   331
        WebSocket.Listener listener = new WebSocket.Listener() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   332
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   333
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   334
            public void onOpen(WebSocket webSocket) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   335
                /* no initial request */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   336
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   337
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   338
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   339
            public CompletionStage<?> onText(WebSocket webSocket,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   340
                                             CharSequence message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   341
                                             boolean last) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   342
                messageReceived.complete(null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   343
                return null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   344
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   345
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   346
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   347
            public CompletionStage<?> onBinary(WebSocket webSocket,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   348
                                               ByteBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   349
                                               boolean last) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   350
                messageReceived.complete(null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   351
                return null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   352
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   353
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   354
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   355
            public CompletionStage<?> onPing(WebSocket webSocket,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   356
                                             ByteBuffer message) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   357
                messageReceived.complete(null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   358
                return null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   359
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   360
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   361
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   362
            public CompletionStage<?> onPong(WebSocket webSocket,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   363
                                             ByteBuffer message) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   364
                messageReceived.complete(null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   365
                return null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   366
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   367
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   368
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   369
            public CompletionStage<?> onClose(WebSocket webSocket,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   370
                                              int statusCode,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   371
                                              String reason) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   372
                messageReceived.complete(null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   373
                return null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   374
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   375
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   376
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   377
        int[] bytes = new int[]{
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   378
                0x82, 0x00, // opcode=binary, fin=true
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   379
                0x88, 0x00, // opcode=close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   380
        };
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   381
        try (var server = Support.serverWithCannedData(bytes)) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   382
            server.open();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   383
58289
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   384
            WebSocket ws = newHttpClient()
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   385
                    .newWebSocketBuilder()
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   386
                    .buildAsync(server.getURI(), listener)
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   387
                    .join();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   388
            try {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   389
                for (int i = 0; i < 3; i++) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   390
                    System.out.printf("iteration #%s%n", i);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   391
                    // after the first abort() each consecutive one must be a no-op,
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   392
                    // moreover, query methods should continue to return consistent
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   393
                    // values
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   394
                    for (int j = 0; j < 3; j++) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   395
                        System.out.printf("abort #%s%n", j);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   396
                        ws.abort();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   397
                        assertTrue(ws.isInputClosed());
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   398
                        assertTrue(ws.isOutputClosed());
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   399
                        assertEquals(ws.getSubprotocol(), "");
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   400
                    }
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   401
                    // at this point valid requests MUST be a no-op:
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   402
                    for (int j = 0; j < 3; j++) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   403
                        System.out.printf("request #%s%n", j);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   404
                        ws.request(1);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   405
                        ws.request(2);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   406
                        ws.request(8);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   407
                        ws.request(Integer.MAX_VALUE);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   408
                        ws.request(Long.MAX_VALUE);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   409
                        // invalid requests MUST throw IAE:
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   410
                        assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   411
                        assertThrows(IAE, () -> ws.request(Long.MIN_VALUE));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   412
                        assertThrows(IAE, () -> ws.request(-1));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   413
                        assertThrows(IAE, () -> ws.request(0));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   414
                    }
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   415
                }
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   416
                // even though there is a bunch of messages readily available on the
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   417
                // wire we shouldn't have received any of them as we aborted before
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   418
                // the first request
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   419
                try {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   420
                    messageReceived.get(5, TimeUnit.SECONDS);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   421
                    fail();
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   422
                } catch (TimeoutException expected) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   423
                    System.out.println("Finished waiting");
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   424
                }
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   425
                for (int i = 0; i < 3; i++) {
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   426
                    System.out.printf("send #%s%n", i);
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   427
                    Support.assertFails(IOE, ws.sendText("text!", false));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   428
                    Support.assertFails(IOE, ws.sendText("text!", true));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   429
                    Support.assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), false));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   430
                    Support.assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), true));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   431
                    Support.assertFails(IOE, ws.sendPing(ByteBuffer.allocate(16)));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   432
                    Support.assertFails(IOE, ws.sendPong(ByteBuffer.allocate(16)));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   433
                    Support.assertFails(IOE, ws.sendClose(NORMAL_CLOSURE, "a reason"));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   434
                    assertThrows(NPE, () -> ws.sendText(null, false));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   435
                    assertThrows(NPE, () -> ws.sendText(null, true));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   436
                    assertThrows(NPE, () -> ws.sendBinary(null, false));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   437
                    assertThrows(NPE, () -> ws.sendBinary(null, true));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   438
                    assertThrows(NPE, () -> ws.sendPing(null));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   439
                    assertThrows(NPE, () -> ws.sendPong(null));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   440
                    assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null));
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   441
                }
3a79d4cccbcb 8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents: 49765
diff changeset
   442
            } finally {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   443
                ws.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   444
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   445
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   446
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   447
}