test/jdk/java/net/httpclient/websocket/SendTest.java
author prappo
Mon, 19 Mar 2018 14:20:18 +0000
branchhttp-client-branch
changeset 56320 f82729ca8660
parent 56318 2a96e88888b2
child 56324 3edf200fff01
permissions -rw-r--r--
http-client-branch: (WebSocket) removed MessagePart
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
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56317
diff changeset
    32
import org.testng.annotations.Test;
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56317
diff changeset
    33
56317
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    34
import java.io.IOException;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    35
import java.net.http.WebSocket;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    36
import java.nio.ByteBuffer;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    37
import java.util.concurrent.CompletableFuture;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    38
import java.util.concurrent.TimeUnit;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    39
import java.util.concurrent.TimeoutException;
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56317
diff changeset
    40
56317
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    41
import static java.net.http.HttpClient.newHttpClient;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    42
import static java.net.http.WebSocket.NORMAL_CLOSURE;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    43
import static org.testng.Assert.assertEquals;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    44
import static org.testng.Assert.assertFalse;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    45
import static org.testng.Assert.assertThrows;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    46
import static org.testng.Assert.assertTrue;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    47
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    48
public class SendTest {
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
    private static final Class<NullPointerException> NPE = NullPointerException.class;
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
    @Test
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    53
    public void sendMethodsThrowNPE() throws IOException {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    54
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    55
            server.open();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    56
            WebSocket ws = newHttpClient()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    57
                    .newWebSocketBuilder()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    58
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    59
                    .join();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    60
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    61
            assertThrows(NPE, () -> ws.sendText(null, false));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    62
            assertThrows(NPE, () -> ws.sendText(null, true));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    63
            assertThrows(NPE, () -> ws.sendBinary(null, false));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    64
            assertThrows(NPE, () -> ws.sendBinary(null, true));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    65
            assertThrows(NPE, () -> ws.sendPing(null));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    66
            assertThrows(NPE, () -> ws.sendPong(null));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    67
            assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null));
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
            ws.abort();
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
            assertThrows(NPE, () -> ws.sendText(null, false));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    72
            assertThrows(NPE, () -> ws.sendText(null, true));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    73
            assertThrows(NPE, () -> ws.sendBinary(null, false));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    74
            assertThrows(NPE, () -> ws.sendBinary(null, true));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    75
            assertThrows(NPE, () -> ws.sendPing(null));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    76
            assertThrows(NPE, () -> ws.sendPong(null));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    77
            assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    78
        }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    79
    }
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
    // TODO: request in onClose/onError
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    82
    // TODO: throw exception in onClose/onError
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    83
    // TODO: exception is thrown from request()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    84
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    85
    @Test
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    86
    public void sendCloseCompleted() throws IOException {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    87
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    88
            server.open();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    89
            WebSocket ws = newHttpClient()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    90
                    .newWebSocketBuilder()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    91
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    92
                    .join();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    93
            ws.sendClose(NORMAL_CLOSURE, "").join();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    94
            assertTrue(ws.isOutputClosed());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    95
            assertEquals(ws.getSubprotocol(), "");
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    96
            ws.request(1); // No exceptions must be thrown
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
    97
        }
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
    @Test
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   101
    public void sendClosePending() throws Exception {
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56317
diff changeset
   102
        try (DummyWebSocketServer server = Support.notReadingServer()) {
56317
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   103
            server.open();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   104
            WebSocket ws = newHttpClient()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   105
                    .newWebSocketBuilder()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   106
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   107
                    .join();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   108
            try {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   109
                ByteBuffer data = ByteBuffer.allocate(65536);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   110
                for (int i = 0; ; i++) { // fill up the send buffer
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   111
                    System.out.printf("begin cycle #%s at %s%n",
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   112
                            i, System.currentTimeMillis());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   113
                    try {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   114
                        ws.sendBinary(data, true).get(10, TimeUnit.SECONDS);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   115
                        data.clear();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   116
                    } catch (TimeoutException e) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   117
                        break;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   118
                    } finally {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   119
                        System.out.printf("end cycle #%s at %s%n",
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   120
                                i, System.currentTimeMillis());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   121
                    }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   122
                }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   123
                CompletableFuture<WebSocket> cf = ws.sendClose(NORMAL_CLOSURE, "");
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   124
                // 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
   125
                assertFalse(cf.isDone());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   126
                assertTrue(ws.isOutputClosed());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   127
                assertEquals(ws.getSubprotocol(), "");
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   128
            } finally {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   129
                ws.abort();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   130
            }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   131
        }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   132
    }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   133
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   134
    @Test
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   135
    public void abortPendingSendBinary() throws Exception {
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56317
diff changeset
   136
        try (DummyWebSocketServer server = Support.notReadingServer()) {
56317
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   137
            server.open();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   138
            WebSocket ws = newHttpClient()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   139
                    .newWebSocketBuilder()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   140
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   141
                    .join();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   142
            ByteBuffer data = ByteBuffer.allocate(65536);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   143
            CompletableFuture<WebSocket> cf = null;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   144
            for (int i = 0; ; i++) {  // fill up the send buffer
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   145
                System.out.printf("begin cycle #%s at %s%n",
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   146
                        i, System.currentTimeMillis());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   147
                try {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   148
                    cf = ws.sendBinary(data, true);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   149
                    cf.get(10, TimeUnit.SECONDS);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   150
                    data.clear();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   151
                } catch (TimeoutException e) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   152
                    break;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   153
                } finally {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   154
                    System.out.printf("end cycle #%s at %s%n",
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   155
                            i, System.currentTimeMillis());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   156
                }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   157
            }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   158
            ws.abort();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   159
            assertTrue(ws.isOutputClosed());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   160
            assertTrue(ws.isInputClosed());
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56317
diff changeset
   161
            Support.assertFails(IOException.class, cf);
56317
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   162
        }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   163
    }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   164
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   165
    @Test
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   166
    public void abortPendingSendText() throws Exception {
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56317
diff changeset
   167
        try (DummyWebSocketServer server = Support.notReadingServer()) {
56317
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   168
            server.open();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   169
            WebSocket ws = newHttpClient()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   170
                    .newWebSocketBuilder()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   171
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   172
                    .join();
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56317
diff changeset
   173
            String data = Support.stringWith2NBytes(32768);
56317
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   174
            CompletableFuture<WebSocket> cf = null;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   175
            for (int i = 0; ; i++) {  // fill up the send buffer
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   176
                System.out.printf("begin cycle #%s at %s%n",
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   177
                        i, System.currentTimeMillis());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   178
                try {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   179
                    cf = ws.sendText(data, true);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   180
                    cf.get(10, TimeUnit.SECONDS);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   181
                } catch (TimeoutException e) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   182
                    break;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   183
                } finally {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   184
                    System.out.printf("end cycle #%s at %s%n",
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   185
                            i, System.currentTimeMillis());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   186
                }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   187
            }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   188
            ws.abort();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   189
            assertTrue(ws.isOutputClosed());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   190
            assertTrue(ws.isInputClosed());
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56317
diff changeset
   191
            Support.assertFails(IOException.class, cf);
56317
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   192
        }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   193
    }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   194
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   195
    @Test // FIXME: TO BE REMOVED as we agreed upon no timeout in sendClose
56317
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   196
    public void sendCloseTimeout() throws Exception {
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56317
diff changeset
   197
        try (DummyWebSocketServer server = Support.notReadingServer()) {
56317
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   198
            server.open();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   199
            WebSocket ws = newHttpClient()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   200
                    .newWebSocketBuilder()
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   201
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   202
                    .join();
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56317
diff changeset
   203
            String data = Support.stringWith2NBytes(32768);
56317
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   204
            CompletableFuture<WebSocket> cf = null;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   205
            for (int i = 0; ; i++) {  // fill up the send buffer
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   206
                System.out.printf("begin cycle #%s at %s%n",
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   207
                        i, System.currentTimeMillis());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   208
                try {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   209
                    cf = ws.sendText(data, true);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   210
                    cf.get(10, TimeUnit.SECONDS);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   211
                } catch (TimeoutException e) {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   212
                    break;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   213
                } finally {
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   214
                    System.out.printf("end 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
                }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   217
            }
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   218
            long before = System.currentTimeMillis();
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56317
diff changeset
   219
            Support.assertFails(IOException.class,
56317
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   220
                    ws.sendClose(WebSocket.NORMAL_CLOSURE, "ok"));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   221
            long after = System.currentTimeMillis();
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   222
            // default timeout should be 30 seconds
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   223
            long elapsed = after - before;
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   224
            System.out.printf("Elapsed %s ms%n", elapsed);
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   225
            assertTrue(elapsed >= 29_000, String.valueOf(elapsed));
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   226
            assertTrue(ws.isOutputClosed());
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   227
            assertTrue(ws.isInputClosed());
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56317
diff changeset
   228
            Support.assertFails(IOException.class, cf);
56317
52a9f6b74e43 http-client-branch: split large WebSocket test into three
chegar
parents:
diff changeset
   229
        }
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
}