test/jdk/java/net/httpclient/websocket/Exceptionally.java
author prappo
Wed, 31 Jan 2018 14:29:56 +0000
branchhttp-client-branch
changeset 56051 c4867f1e7e5a
parent 56048 f9b6016727b5
permissions -rw-r--r--
http-client-branch: (WebSocket) tests
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56024
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
     1
/*
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
     4
 *
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
     8
 *
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    13
 * accompanied this code).
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    14
 *
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    18
 *
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    21
 * questions.
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    22
 */
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    23
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    24
/*
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    25
 * @test
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    26
 * @build DummyWebSocketServer
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    27
 * @run testng/othervm -Djdk.httpclient.HttpClient.log=trace Exceptionally
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    28
 */
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    29
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    30
import jdk.incubator.http.WebSocket;
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    31
import org.testng.annotations.Test;
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    32
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    33
import java.io.IOException;
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    34
import java.nio.ByteBuffer;
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
    35
import java.nio.CharBuffer;
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
    36
import java.nio.channels.SocketChannel;
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
    37
import java.nio.charset.StandardCharsets;
56024
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    38
import java.util.concurrent.CompletableFuture;
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    39
import java.util.concurrent.CompletionException;
56048
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
    40
import java.util.concurrent.CompletionStage;
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
    41
import java.util.concurrent.TimeUnit;
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
    42
import java.util.concurrent.TimeoutException;
56024
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    43
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    44
import static jdk.incubator.http.HttpClient.newHttpClient;
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    45
import static jdk.incubator.http.WebSocket.NORMAL_CLOSURE;
56051
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    46
import static org.testng.Assert.assertEquals;
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    47
import static org.testng.Assert.assertFalse;
56024
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    48
import static org.testng.Assert.assertThrows;
56051
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    49
import static org.testng.Assert.assertTrue;
56024
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    50
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    51
public class Exceptionally {
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    52
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
    53
    private static final Class<NullPointerException> NPE
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
    54
            = NullPointerException.class;
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
    55
    private static final Class<IllegalArgumentException> IAE
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
    56
            = IllegalArgumentException.class;
56047
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
    57
    private static final Class<IllegalStateException> ISE
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
    58
            = IllegalStateException.class;
56024
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    59
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    60
    @Test
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    61
    public void testNull() throws IOException {
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    62
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    63
            server.open();
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    64
            WebSocket ws = newHttpClient()
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    65
                    .newWebSocketBuilder()
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    66
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    67
                    .join();
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    68
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
    69
            assertThrows(NPE, () -> ws.sendText(null, false));
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
    70
            assertThrows(NPE, () -> ws.sendText(null, true));
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
    71
            assertThrows(NPE, () -> ws.sendBinary(null, false));
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
    72
            assertThrows(NPE, () -> ws.sendBinary(null, true));
56024
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    73
            assertThrows(NPE, () -> ws.sendPing(null));
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    74
            assertThrows(NPE, () -> ws.sendPong(null));
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    75
            assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null));
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    76
        }
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    77
    }
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    78
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
    79
    @Test
56051
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    80
    public void testSendClose1() throws IOException {
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    81
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    82
            server.open();
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    83
            WebSocket ws = newHttpClient()
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    84
                    .newWebSocketBuilder()
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    85
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    86
                    .join();
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    87
            ws.sendClose(NORMAL_CLOSURE, "").join();
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    88
            assertTrue(ws.isOutputClosed());
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    89
            assertFalse(ws.isInputClosed());
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    90
            assertEquals(ws.getSubprotocol(), "");
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    91
            ws.request(1); // No exceptions must be thrown
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    92
        }
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    93
    }
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    94
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    95
    @Test
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    96
    public void testSendClose2() throws Exception {
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    97
        try (DummyWebSocketServer server = notReadingServer()) {
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    98
            server.open();
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
    99
            WebSocket ws = newHttpClient()
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   100
                    .newWebSocketBuilder()
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   101
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   102
                    .join();
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   103
            ByteBuffer data = ByteBuffer.allocate(65536);
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   104
            for (int i = 0; ; i++) {
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   105
                System.out.println("cycle #" + i);
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   106
                try {
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   107
                    ws.sendBinary(data, true).get(10, TimeUnit.SECONDS);
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   108
                    data.clear();
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   109
                } catch (TimeoutException e) {
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   110
                    break;
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   111
                }
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   112
            }
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   113
            CompletableFuture<WebSocket> cf = ws.sendClose(NORMAL_CLOSURE, "");
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   114
            assertTrue(ws.isOutputClosed());
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   115
            assertFalse(ws.isInputClosed());
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   116
            assertEquals(ws.getSubprotocol(), "");
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   117
            // The output closes regardless of whether or not the Close message
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   118
            // has been sent
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   119
            assertFalse(cf.isDone());
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   120
        }
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   121
    }
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   122
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   123
    /*
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   124
     * This server does not read from the wire, allowing its client to fill up
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   125
     * their send buffer. Used to test scenarios with outstanding send
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   126
     * operations.
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   127
     */
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   128
    private static DummyWebSocketServer notReadingServer() {
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   129
        return new DummyWebSocketServer() {
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   130
            @Override
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   131
            protected void serve(SocketChannel channel) throws IOException {
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   132
                try {
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   133
                    Thread.sleep(Long.MAX_VALUE);
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   134
                } catch (InterruptedException e) {
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   135
                    throw new IOException(e);
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   136
                }
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   137
            }
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   138
        };
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   139
    }
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   140
c4867f1e7e5a http-client-branch: (WebSocket) tests
prappo
parents: 56048
diff changeset
   141
    @Test
56024
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   142
    public void testIllegalArgument() throws IOException {
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   143
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   144
            server.open();
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   145
            WebSocket ws = newHttpClient()
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   146
                    .newWebSocketBuilder()
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   147
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   148
                    .join();
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   149
56047
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   150
            assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(126)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   151
            assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(127)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   152
            assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(128)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   153
            assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(129)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   154
            assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(256)));
56024
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   155
56047
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   156
            assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(126)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   157
            assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(127)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   158
            assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(128)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   159
            assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(129)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   160
            assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(256)));
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   161
56047
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   162
            assertCompletesExceptionally(IAE, ws.sendText(incompleteString(), true));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   163
            assertCompletesExceptionally(IAE, ws.sendText(incompleteString(), false));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   164
            assertCompletesExceptionally(IAE, ws.sendText(malformedString(), true));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   165
            assertCompletesExceptionally(IAE, ws.sendText(malformedString(), false));
56024
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   166
56047
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   167
            assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(124)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   168
            assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(125)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   169
            assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(128)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   170
            assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(256)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   171
            assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(257)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   172
            assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWith2NBytes((123 / 2) + 1)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   173
            assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, malformedString()));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   174
            assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, incompleteString()));
56024
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   175
56047
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   176
            assertCompletesExceptionally(IAE, ws.sendClose(-2, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   177
            assertCompletesExceptionally(IAE, ws.sendClose(-1, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   178
            assertCompletesExceptionally(IAE, ws.sendClose(0, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   179
            assertCompletesExceptionally(IAE, ws.sendClose(1, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   180
            assertCompletesExceptionally(IAE, ws.sendClose(500, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   181
            assertCompletesExceptionally(IAE, ws.sendClose(998, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   182
            assertCompletesExceptionally(IAE, ws.sendClose(999, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   183
            assertCompletesExceptionally(IAE, ws.sendClose(1002, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   184
            assertCompletesExceptionally(IAE, ws.sendClose(1003, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   185
            assertCompletesExceptionally(IAE, ws.sendClose(1006, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   186
            assertCompletesExceptionally(IAE, ws.sendClose(1007, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   187
            assertCompletesExceptionally(IAE, ws.sendClose(1009, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   188
            assertCompletesExceptionally(IAE, ws.sendClose(1010, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   189
            assertCompletesExceptionally(IAE, ws.sendClose(1012, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   190
            assertCompletesExceptionally(IAE, ws.sendClose(1013, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   191
            assertCompletesExceptionally(IAE, ws.sendClose(1015, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   192
            assertCompletesExceptionally(IAE, ws.sendClose(5000, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   193
            assertCompletesExceptionally(IAE, ws.sendClose(32768, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   194
            assertCompletesExceptionally(IAE, ws.sendClose(65535, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   195
            assertCompletesExceptionally(IAE, ws.sendClose(65536, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   196
            assertCompletesExceptionally(IAE, ws.sendClose(Integer.MAX_VALUE, "a reason"));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   197
            assertCompletesExceptionally(IAE, ws.sendClose(Integer.MIN_VALUE, "a reason"));
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   198
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   199
            assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE));
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   200
            assertThrows(IAE, () -> ws.request(-1));
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   201
            assertThrows(IAE, () -> ws.request(0));
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   202
        }
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   203
    }
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   204
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   205
    private static String malformedString() {
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   206
        return new String(new char[]{0xDC00, 0xD800});
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   207
    }
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   208
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   209
    private static String incompleteString() {
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   210
        return new String(new char[]{0xD800});
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   211
    }
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   212
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   213
    private static String stringWithNBytes(int n) {
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   214
        StringBuilder sb = new StringBuilder(n);
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   215
        for (int i = 0; i < n; i++) {
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   216
            sb.append("A");
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   217
        }
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   218
        return sb.toString();
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   219
    }
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   220
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   221
    private static String stringWith2NBytes(int n) {
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   222
        // Russian alphabet repeated cyclically
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   223
        char FIRST = '\u0410';
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   224
        char LAST = '\u042F';
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   225
        StringBuilder sb = new StringBuilder(n);
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   226
        char c = FIRST;
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   227
        for (int i = 0; i < n; i++) {
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   228
            if (++c > LAST) {
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   229
                c = FIRST;
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   230
            }
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   231
            sb.append(c);
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   232
        }
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   233
        String s = sb.toString();
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   234
        assert s.length() == n && s.getBytes(StandardCharsets.UTF_8).length == 2 * n;
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   235
        return s;
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   236
    }
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   237
56047
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   238
    private static void assertCompletesExceptionally(Class<? extends Throwable> clazz,
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   239
                                                     CompletableFuture<?> stage) {
56046
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   240
        stage.handle((result, error) -> {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   241
            if (error instanceof CompletionException) {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   242
                Throwable cause = error.getCause();
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   243
                if (cause == null) {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   244
                    throw new AssertionError("Unexpected null cause: " + error);
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   245
                }
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   246
                assertException(clazz, cause);
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   247
            } else {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   248
                assertException(clazz, error);
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   249
            }
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   250
            return null;
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   251
        }).join();
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   252
    }
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   253
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   254
    private static void assertException(Class<? extends Throwable> clazz,
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   255
                                        Throwable t) {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   256
        if (t == null) {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   257
            throw new AssertionError("Expected " + clazz + ", caught nothing");
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   258
        }
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   259
        if (!clazz.isInstance(t)) {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   260
            throw new AssertionError("Expected " + clazz + ", caught " + t);
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   261
        }
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   262
    }
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   263
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   264
    @Test
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   265
    public void testIllegalStateOutstanding1() throws Exception {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   266
        try (DummyWebSocketServer server = notReadingServer()) {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   267
            server.open();
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   268
            WebSocket ws = newHttpClient()
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   269
                    .newWebSocketBuilder()
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   270
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   271
                    .join();
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   272
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   273
            ByteBuffer data = ByteBuffer.allocate(65536);
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   274
            for (int i = 0; ; i++) {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   275
                System.out.println("cycle #" + i);
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   276
                try {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   277
                    ws.sendBinary(data, true).get(10, TimeUnit.SECONDS);
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   278
                    data.clear();
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   279
                } catch (TimeoutException e) {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   280
                    break;
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   281
                }
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   282
            }
56047
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   283
            assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(0), true));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   284
            assertCompletesExceptionally(ISE, ws.sendText("", true));
56046
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   285
        }
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   286
    }
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   287
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   288
    @Test
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   289
    public void testIllegalStateOutstanding2() throws Exception {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   290
        try (DummyWebSocketServer server = notReadingServer()) {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   291
            server.open();
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   292
            WebSocket ws = newHttpClient()
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   293
                    .newWebSocketBuilder()
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   294
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   295
                    .join();
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   296
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   297
            CharBuffer data = CharBuffer.allocate(65536);
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   298
            for (int i = 0; ; i++) {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   299
                System.out.println("cycle #" + i);
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   300
                try {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   301
                    ws.sendText(data, true).get(10, TimeUnit.SECONDS);
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   302
                    data.clear();
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   303
                } catch (TimeoutException e) {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   304
                    break;
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   305
                }
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   306
            }
56047
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   307
            assertCompletesExceptionally(ISE, ws.sendText("", true));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   308
            assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(0), true));
56046
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   309
        }
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   310
    }
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   311
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   312
    @Test
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   313
    public void testIllegalStateIntermixed1() throws IOException {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   314
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   315
            server.open();
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   316
            WebSocket ws = newHttpClient()
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   317
                    .newWebSocketBuilder()
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   318
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   319
                    .join();
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   320
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   321
            ws.sendBinary(ByteBuffer.allocate(16), false).join();
56047
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   322
            assertCompletesExceptionally(ISE, ws.sendText("text", false));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   323
            assertCompletesExceptionally(ISE, ws.sendText("text", true));
56046
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   324
        }
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   325
    }
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   326
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   327
    @Test
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   328
    public void testIllegalStateIntermixed2() throws IOException {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   329
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   330
            server.open();
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   331
            WebSocket ws = newHttpClient()
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   332
                    .newWebSocketBuilder()
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   333
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   334
                    .join();
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   335
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   336
            ws.sendText("text", false).join();
56047
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   337
            assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(16), false));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   338
            assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(16), true));
56046
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   339
        }
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   340
    }
75d9bc7febf5 http-client-branch: (WebSocket) rearranged methods in tests
prappo
parents: 56045
diff changeset
   341
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   342
    @Test
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   343
    public void testIllegalStateSendClose() throws IOException {
56024
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   344
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   345
            server.open();
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   346
            WebSocket ws = newHttpClient()
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   347
                    .newWebSocketBuilder()
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   348
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   349
                    .join();
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   350
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   351
            ws.sendClose(NORMAL_CLOSURE, "normal close").join();
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   352
56047
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   353
            assertCompletesExceptionally(ISE, ws.sendText("", true));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   354
            assertCompletesExceptionally(ISE, ws.sendText("", false));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   355
            assertCompletesExceptionally(ISE, ws.sendText("abc", true));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   356
            assertCompletesExceptionally(ISE, ws.sendText("abc", false));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   357
            assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(0), true));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   358
            assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(0), false));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   359
            assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(1), true));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   360
            assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(1), false));
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   361
56047
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   362
            assertCompletesExceptionally(ISE, ws.sendPing(ByteBuffer.allocate(125)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   363
            assertCompletesExceptionally(ISE, ws.sendPing(ByteBuffer.allocate(124)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   364
            assertCompletesExceptionally(ISE, ws.sendPing(ByteBuffer.allocate(1)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   365
            assertCompletesExceptionally(ISE, ws.sendPing(ByteBuffer.allocate(0)));
56024
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   366
56047
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   367
            assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(125)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   368
            assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(124)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   369
            assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(1)));
2e86bda80f79 http-client-branch: (WebSocket) consistent method names in test
prappo
parents: 56046
diff changeset
   370
            assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(0)));
56024
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   371
        }
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   372
    }
56048
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   373
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   374
    @Test
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   375
    public void testIllegalStateOnClose() throws Exception {
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   376
        DummyWebSocketServer server = new DummyWebSocketServer() {
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   377
            @Override
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   378
            protected void serve(SocketChannel channel) throws IOException {
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   379
                ByteBuffer closeMessage = ByteBuffer.wrap(new byte[]{(byte) 0x88, 0x00});
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   380
                int wrote = channel.write(closeMessage);
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   381
                System.out.println("Wrote bytes: " + wrote);
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   382
                super.serve(channel);
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   383
            }
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   384
        };
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   385
        try (server) {
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   386
            server.open();
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   387
            CompletableFuture<Void> onCloseCalled = new CompletableFuture<>();
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   388
            CompletableFuture<Void> canClose = new CompletableFuture<>();
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   389
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   390
            WebSocket ws = newHttpClient()
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   391
                    .newWebSocketBuilder()
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   392
                    .buildAsync(server.getURI(), new WebSocket.Listener() {
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   393
                        @Override
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   394
                        public CompletionStage<?> onClose(WebSocket webSocket,
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   395
                                                          int statusCode,
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   396
                                                          String reason) {
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   397
                            System.out.println("onClose(" + statusCode + ")");
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   398
                            onCloseCalled.complete(null);
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   399
                            return canClose;
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   400
                        }
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   401
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   402
                        @Override
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   403
                        public void onError(WebSocket webSocket, Throwable error) {
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   404
                            System.out.println("onError(" + error + ")");
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   405
                            error.printStackTrace();
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   406
                        }
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   407
                    })
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   408
                    .join();
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   409
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   410
            onCloseCalled.join();      // Wait for onClose to be called
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   411
            TimeUnit.SECONDS.sleep(5); // Give canClose some time to reach the WebSocket
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   412
            canClose.complete(null);   // Signal to the WebSocket it can close the output
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   413
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   414
            assertCompletesExceptionally(ISE, ws.sendText("", true));
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   415
            assertCompletesExceptionally(ISE, ws.sendText("", false));
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   416
            assertCompletesExceptionally(ISE, ws.sendText("abc", true));
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   417
            assertCompletesExceptionally(ISE, ws.sendText("abc", false));
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   418
            assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(0), true));
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   419
            assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(0), false));
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   420
            assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(1), true));
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   421
            assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(1), false));
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   422
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   423
            assertCompletesExceptionally(ISE, ws.sendPing(ByteBuffer.allocate(125)));
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   424
            assertCompletesExceptionally(ISE, ws.sendPing(ByteBuffer.allocate(124)));
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   425
            assertCompletesExceptionally(ISE, ws.sendPing(ByteBuffer.allocate(1)));
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   426
            assertCompletesExceptionally(ISE, ws.sendPing(ByteBuffer.allocate(0)));
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   427
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   428
            assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(125)));
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   429
            assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(124)));
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   430
            assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(1)));
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   431
            assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(0)));
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   432
        }
f9b6016727b5 http-client-branch: (WebSocket) A test for onClose & IllegalStateException
prappo
parents: 56047
diff changeset
   433
    }
56024
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
diff changeset
   434
}