test/jdk/java/net/httpclient/websocket/SendTest.java
author chegar
Sat, 17 Mar 2018 18:01:01 +0000
branchhttp-client-branch
changeset 56317 52a9f6b74e43
child 56318 2a96e88888b2
permissions -rw-r--r--
http-client-branch: split large WebSocket test into three
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56317
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
     1
/*
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
     4
 *
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
     8
 *
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    13
 * accompanied this code).
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    14
 *
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    18
 *
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    21
 * questions.
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    22
 */
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    23
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    24
/*
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    25
 * @test
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    26
 * @build DummyWebSocketServer
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    27
 * @run testng/othervm
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    28
 *      -Djdk.internal.httpclient.websocket.debug=true
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    29
 *       SendTest
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    30
 */
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    31
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    32
import java.io.IOException;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    33
import java.net.http.WebSocket;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    34
import java.nio.ByteBuffer;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    35
import java.nio.channels.SocketChannel;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    36
import java.nio.charset.StandardCharsets;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    37
import java.util.Arrays;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    38
import java.util.concurrent.CompletableFuture;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    39
import java.util.concurrent.CompletionStage;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    40
import java.util.concurrent.TimeUnit;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    41
import java.util.concurrent.TimeoutException;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    42
import org.testng.annotations.Test;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    43
import static java.net.http.HttpClient.newHttpClient;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    44
import static java.net.http.WebSocket.NORMAL_CLOSURE;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    45
import static org.testng.Assert.assertEquals;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    46
import static org.testng.Assert.assertFalse;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    47
import static org.testng.Assert.assertThrows;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    48
import static org.testng.Assert.assertTrue;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    49
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    50
public class SendTest {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    51
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    52
    private static final Class<NullPointerException> NPE = NullPointerException.class;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    53
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    54
    /* shortcut */
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    55
    private static void assertFails(Class<? extends Throwable> clazz,
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    56
                                    CompletionStage<?> stage) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    57
        Support.assertCompletesExceptionally(clazz, stage);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    58
    }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    59
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    60
    private static DummyWebSocketServer serverWithCannedData(byte... data) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    61
        byte[] copy = Arrays.copyOf(data, data.length);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    62
        return new DummyWebSocketServer() {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    63
            @Override
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    64
            protected void serve(SocketChannel channel) throws IOException {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    65
                ByteBuffer closeMessage = ByteBuffer.wrap(copy);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    66
                channel.write(closeMessage);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    67
                super.serve(channel);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    68
            }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    69
        };
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    70
    }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    71
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    72
    @Test
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    73
    public void sendMethodsThrowNPE() throws IOException {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    74
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    75
            server.open();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    76
            WebSocket ws = newHttpClient()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    77
                    .newWebSocketBuilder()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    78
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    79
                    .join();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    80
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    81
            assertThrows(NPE, () -> ws.sendText(null, false));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    82
            assertThrows(NPE, () -> ws.sendText(null, true));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    83
            assertThrows(NPE, () -> ws.sendBinary(null, false));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    84
            assertThrows(NPE, () -> ws.sendBinary(null, true));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    85
            assertThrows(NPE, () -> ws.sendPing(null));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    86
            assertThrows(NPE, () -> ws.sendPong(null));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    87
            assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    88
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    89
            ws.abort();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    90
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    91
            assertThrows(NPE, () -> ws.sendText(null, false));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    92
            assertThrows(NPE, () -> ws.sendText(null, true));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    93
            assertThrows(NPE, () -> ws.sendBinary(null, false));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    94
            assertThrows(NPE, () -> ws.sendBinary(null, true));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    95
            assertThrows(NPE, () -> ws.sendPing(null));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    96
            assertThrows(NPE, () -> ws.sendPong(null));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    97
            assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    98
        }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    99
    }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   100
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   101
    // TODO: request in onClose/onError
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   102
    // TODO: throw exception in onClose/onError
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   103
    // TODO: exception is thrown from request()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   104
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   105
    @Test
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   106
    public void sendCloseCompleted() throws IOException {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   107
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   108
            server.open();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   109
            WebSocket ws = newHttpClient()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   110
                    .newWebSocketBuilder()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   111
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   112
                    .join();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   113
            ws.sendClose(NORMAL_CLOSURE, "").join();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   114
            assertTrue(ws.isOutputClosed());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   115
            assertEquals(ws.getSubprotocol(), "");
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   116
            ws.request(1); // No exceptions must be thrown
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   117
        }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   118
    }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   119
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   120
    @Test
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   121
    public void sendClosePending() throws Exception {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   122
        try (DummyWebSocketServer server = notReadingServer()) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   123
            server.open();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   124
            WebSocket ws = newHttpClient()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   125
                    .newWebSocketBuilder()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   126
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   127
                    .join();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   128
            try {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   129
                ByteBuffer data = ByteBuffer.allocate(65536);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   130
                for (int i = 0; ; i++) { // fill up the send buffer
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   131
                    System.out.printf("begin cycle #%s at %s%n",
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   132
                            i, System.currentTimeMillis());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   133
                    try {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   134
                        ws.sendBinary(data, true).get(10, TimeUnit.SECONDS);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   135
                        data.clear();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   136
                    } catch (TimeoutException e) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   137
                        break;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   138
                    } finally {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   139
                        System.out.printf("end cycle #%s at %s%n",
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   140
                                i, System.currentTimeMillis());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   141
                    }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   142
                }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   143
                CompletableFuture<WebSocket> cf = ws.sendClose(NORMAL_CLOSURE, "");
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   144
                // The output closes even if the Close message has not been sent
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   145
                assertFalse(cf.isDone());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   146
                assertTrue(ws.isOutputClosed());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   147
                assertEquals(ws.getSubprotocol(), "");
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   148
            } finally {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   149
                ws.abort();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   150
            }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   151
        }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   152
    }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   153
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   154
    /*
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   155
     * This server does not read from the wire, allowing its client to fill up
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   156
     * their send buffer. Used to test scenarios with outstanding send
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   157
     * operations.
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   158
     */
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   159
    private static DummyWebSocketServer notReadingServer() {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   160
        return new DummyWebSocketServer() {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   161
            @Override
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   162
            protected void serve(SocketChannel channel) throws IOException {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   163
                try {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   164
                    Thread.sleep(Long.MAX_VALUE);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   165
                } catch (InterruptedException e) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   166
                    throw new IOException(e);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   167
                }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   168
            }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   169
        };
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   170
    }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   171
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   172
    @Test
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   173
    public void abortPendingSendBinary() throws Exception {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   174
        try (DummyWebSocketServer server = notReadingServer()) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   175
            server.open();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   176
            WebSocket ws = newHttpClient()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   177
                    .newWebSocketBuilder()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   178
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   179
                    .join();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   180
            ByteBuffer data = ByteBuffer.allocate(65536);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   181
            CompletableFuture<WebSocket> cf = null;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   182
            for (int i = 0; ; i++) {  // fill up the send buffer
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   183
                System.out.printf("begin cycle #%s at %s%n",
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   184
                        i, System.currentTimeMillis());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   185
                try {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   186
                    cf = ws.sendBinary(data, true);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   187
                    cf.get(10, TimeUnit.SECONDS);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   188
                    data.clear();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   189
                } catch (TimeoutException e) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   190
                    break;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   191
                } finally {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   192
                    System.out.printf("end cycle #%s at %s%n",
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   193
                            i, System.currentTimeMillis());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   194
                }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   195
            }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   196
            ws.abort();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   197
            assertTrue(ws.isOutputClosed());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   198
            assertTrue(ws.isInputClosed());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   199
            assertFails(IOException.class, cf);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   200
        }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   201
    }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   202
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   203
    @Test
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   204
    public void abortPendingSendText() throws Exception {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   205
        try (DummyWebSocketServer server = notReadingServer()) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   206
            server.open();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   207
            WebSocket ws = newHttpClient()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   208
                    .newWebSocketBuilder()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   209
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   210
                    .join();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   211
            String data = stringWith2NBytes(32768);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   212
            CompletableFuture<WebSocket> cf = null;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   213
            for (int i = 0; ; i++) {  // fill up the send buffer
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   214
                System.out.printf("begin cycle #%s at %s%n",
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   215
                        i, System.currentTimeMillis());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   216
                try {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   217
                    cf = ws.sendText(data, true);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   218
                    cf.get(10, TimeUnit.SECONDS);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   219
                } catch (TimeoutException e) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   220
                    break;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   221
                } finally {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   222
                    System.out.printf("end cycle #%s at %s%n",
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   223
                            i, System.currentTimeMillis());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   224
                }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   225
            }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   226
            ws.abort();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   227
            assertTrue(ws.isOutputClosed());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   228
            assertTrue(ws.isInputClosed());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   229
            assertFails(IOException.class, cf);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   230
        }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   231
    }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   232
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   233
    private static String stringWith2NBytes(int n) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   234
        // -- Russian Alphabet (33 characters, 2 bytes per char) --
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   235
        char[] abc = {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   236
                0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0401, 0x0416,
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   237
                0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E,
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   238
                0x041F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426,
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   239
                0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E,
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   240
                0x042F,
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   241
        };
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   242
        // repeat cyclically
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   243
        StringBuilder sb = new StringBuilder(n);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   244
        for (int i = 0, j = 0; i < n; i++, j = (j + 1) % abc.length) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   245
            sb.append(abc[j]);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   246
        }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   247
        String s = sb.toString();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   248
        assert s.length() == n && s.getBytes(StandardCharsets.UTF_8).length == 2 * n;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   249
        return s;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   250
    }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   251
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   252
    @Test
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   253
    public void sendCloseTimeout() throws Exception {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   254
        try (DummyWebSocketServer server = notReadingServer()) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   255
            server.open();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   256
            WebSocket ws = newHttpClient()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   257
                    .newWebSocketBuilder()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   258
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   259
                    .join();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   260
            String data = stringWith2NBytes(32768);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   261
            CompletableFuture<WebSocket> cf = null;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   262
            for (int i = 0; ; i++) {  // fill up the send buffer
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   263
                System.out.printf("begin cycle #%s at %s%n",
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   264
                        i, System.currentTimeMillis());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   265
                try {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   266
                    cf = ws.sendText(data, true);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   267
                    cf.get(10, TimeUnit.SECONDS);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   268
                } catch (TimeoutException e) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   269
                    break;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   270
                } finally {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   271
                    System.out.printf("end cycle #%s at %s%n",
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   272
                            i, System.currentTimeMillis());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   273
                }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   274
            }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   275
            long before = System.currentTimeMillis();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   276
            assertFails(IOException.class,
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   277
                    ws.sendClose(WebSocket.NORMAL_CLOSURE, "ok"));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   278
            long after = System.currentTimeMillis();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   279
            // default timeout should be 30 seconds
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   280
            long elapsed = after - before;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   281
            System.out.printf("Elapsed %s ms%n", elapsed);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   282
            assertTrue(elapsed >= 29_000, String.valueOf(elapsed));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   283
            assertTrue(ws.isOutputClosed());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   284
            assertTrue(ws.isInputClosed());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   285
            assertFails(IOException.class, cf);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   286
        }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   287
    }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   288
}