test/jdk/java/net/httpclient/websocket/java.net.http/jdk/internal/net/http/websocket/WebSocketImplTest.java
author chegar
Wed, 07 Feb 2018 21:45:37 +0000
branchhttp-client-branch
changeset 56092 fd85b2bf2b0d
parent 56089 test/jdk/java/net/httpclient/websocket/java.net.http/java/net/http/internal/websocket/WebSocketImplTest.java@42208b2f224e
permissions -rw-r--r--
http-client-branch: move implementation to jdk.internal.net.http
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     1
/*
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     4
 *
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     8
 *
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    13
 * accompanied this code).
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    14
 *
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    18
 *
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    21
 * questions.
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    22
 */
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    23
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    24
package jdk.internal.net.http.websocket;
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    25
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55992
diff changeset
    26
import java.net.http.WebSocket;
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    27
import org.testng.annotations.Test;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    28
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    29
import java.net.URI;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    30
import java.nio.ByteBuffer;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    31
import java.util.Collection;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    32
import java.util.List;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    33
import java.util.Random;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    34
import java.util.concurrent.CompletableFuture;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    35
import java.util.concurrent.TimeUnit;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    36
import java.util.concurrent.atomic.AtomicInteger;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    37
import java.util.function.Consumer;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    38
import java.util.function.Supplier;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    39
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55992
diff changeset
    40
import static java.net.http.WebSocket.MessagePart.FIRST;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55992
diff changeset
    41
import static java.net.http.WebSocket.MessagePart.LAST;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55992
diff changeset
    42
import static java.net.http.WebSocket.MessagePart.PART;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55992
diff changeset
    43
import static java.net.http.WebSocket.MessagePart.WHOLE;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55992
diff changeset
    44
import static java.net.http.WebSocket.NORMAL_CLOSURE;
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    45
import static jdk.internal.net.http.websocket.MockListener.Invocation.onClose;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    46
import static jdk.internal.net.http.websocket.MockListener.Invocation.onError;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    47
import static jdk.internal.net.http.websocket.MockListener.Invocation.onOpen;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    48
import static jdk.internal.net.http.websocket.MockListener.Invocation.onPing;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    49
import static jdk.internal.net.http.websocket.MockListener.Invocation.onPong;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    50
import static jdk.internal.net.http.websocket.MockListener.Invocation.onText;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    51
import static jdk.internal.net.http.websocket.MockTransport.onClose;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    52
import static jdk.internal.net.http.websocket.MockTransport.onPing;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    53
import static jdk.internal.net.http.websocket.MockTransport.onPong;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    54
import static jdk.internal.net.http.websocket.MockTransport.onText;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    55
import static jdk.internal.net.http.websocket.TestSupport.assertCompletesExceptionally;
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    56
import static org.testng.Assert.assertEquals;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    57
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    58
/*
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    59
 * Formatting in this file may seem strange:
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    60
 *
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    61
 *  (
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    62
 *   ( ...)
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    63
 *  ...
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    64
 *  )
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    65
 *  ...
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    66
 *
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    67
 *  However there is a rationale behind it. Sometimes the level of argument
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    68
 *  nesting is high, which makes it hard to manage parentheses.
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    69
 */
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    70
public class WebSocketImplTest {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    71
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    72
    // TODO: request in onClose/onError
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    73
    // TODO: throw exception in onClose/onError
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    74
    // TODO: exception is thrown from request()
55989
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    75
    // TODO: repeated sendClose complete normally
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    76
    // TODO: default Close message is sent if IAE is thrown from sendClose
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    77
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    78
    @Test
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    79
    public void testNonPositiveRequest() throws Exception {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    80
        MockListener listener = new MockListener(Long.MAX_VALUE) {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    81
            @Override
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    82
            protected void onOpen0(WebSocket webSocket) {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    83
                webSocket.request(0);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    84
            }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    85
        };
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    86
        WebSocket ws = newInstance(listener, List.of(now(onText("1", WHOLE))));
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    87
        listener.onCloseOrOnErrorCalled().get(10, TimeUnit.SECONDS);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    88
        List<MockListener.Invocation> invocations = listener.invocations();
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    89
        assertEquals(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    90
                invocations,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    91
                List.of(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    92
                        onOpen(ws),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    93
                        onError(ws, IllegalArgumentException.class)
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    94
                )
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    95
        );
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    96
    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    97
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    98
    @Test
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    99
    public void testText1() throws Exception {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   100
        MockListener listener = new MockListener(Long.MAX_VALUE);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   101
        WebSocket ws = newInstance(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   102
                listener,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   103
                List.of(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   104
                        now(onText("1", FIRST)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   105
                        now(onText("2", PART)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   106
                        now(onText("3", LAST)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   107
                        now(onClose(NORMAL_CLOSURE, "no reason"))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   108
                )
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   109
        );
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   110
        listener.onCloseOrOnErrorCalled().get(10, TimeUnit.SECONDS);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   111
        List<MockListener.Invocation> invocations = listener.invocations();
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   112
        assertEquals(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   113
                invocations,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   114
                List.of(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   115
                        onOpen(ws),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   116
                        onText(ws, "1", FIRST),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   117
                        onText(ws, "2", PART),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   118
                        onText(ws, "3", LAST),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   119
                        onClose(ws, NORMAL_CLOSURE, "no reason")
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   120
                )
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   121
        );
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   122
    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   123
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   124
    @Test
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   125
    public void testText2() throws Exception {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   126
        MockListener listener = new MockListener(Long.MAX_VALUE);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   127
        WebSocket ws = newInstance(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   128
                listener,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   129
                List.of(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   130
                        now(onText("1", FIRST)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   131
                        seconds(1, onText("2", PART)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   132
                        now(onText("3", LAST)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   133
                        seconds(1, onClose(NORMAL_CLOSURE, "no reason"))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   134
                )
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   135
        );
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   136
        listener.onCloseOrOnErrorCalled().get(10, TimeUnit.SECONDS);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   137
        List<MockListener.Invocation> invocations = listener.invocations();
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   138
        assertEquals(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   139
                invocations,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   140
                List.of(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   141
                        onOpen(ws),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   142
                        onText(ws, "1", FIRST),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   143
                        onText(ws, "2", PART),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   144
                        onText(ws, "3", LAST),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   145
                        onClose(ws, NORMAL_CLOSURE, "no reason")
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   146
                )
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   147
        );
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   148
    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   149
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   150
    @Test
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   151
    public void testTextIntermixedWithPongs() throws Exception {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   152
        MockListener listener = new MockListener(Long.MAX_VALUE);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   153
        WebSocket ws = newInstance(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   154
                listener,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   155
                List.of(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   156
                        now(onText("1", FIRST)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   157
                        now(onText("2", PART)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   158
                        now(onPong(ByteBuffer.allocate(16))),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   159
                        seconds(1, onPong(ByteBuffer.allocate(32))),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   160
                        now(onText("3", LAST)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   161
                        now(onPong(ByteBuffer.allocate(64))),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   162
                        now(onClose(NORMAL_CLOSURE, "no reason"))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   163
                )
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   164
        );
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   165
        listener.onCloseOrOnErrorCalled().get(10, TimeUnit.SECONDS);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   166
        List<MockListener.Invocation> invocations = listener.invocations();
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   167
        assertEquals(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   168
                invocations,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   169
                List.of(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   170
                        onOpen(ws),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   171
                        onText(ws, "1", FIRST),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   172
                        onText(ws, "2", PART),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   173
                        onPong(ws, ByteBuffer.allocate(16)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   174
                        onPong(ws, ByteBuffer.allocate(32)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   175
                        onText(ws, "3", LAST),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   176
                        onPong(ws, ByteBuffer.allocate(64)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   177
                        onClose(ws, NORMAL_CLOSURE, "no reason")
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   178
                )
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   179
        );
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   180
    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   181
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   182
    @Test
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   183
    public void testTextIntermixedWithPings() throws Exception {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   184
        MockListener listener = new MockListener(Long.MAX_VALUE);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   185
        WebSocket ws = newInstance(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   186
                listener,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   187
                List.of(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   188
                        now(onText("1", FIRST)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   189
                        now(onText("2", PART)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   190
                        now(onPing(ByteBuffer.allocate(16))),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   191
                        seconds(1, onPing(ByteBuffer.allocate(32))),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   192
                        now(onText("3", LAST)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   193
                        now(onPing(ByteBuffer.allocate(64))),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   194
                        now(onClose(NORMAL_CLOSURE, "no reason"))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   195
                )
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   196
        );
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   197
        listener.onCloseOrOnErrorCalled().get(10, TimeUnit.SECONDS);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   198
        List<MockListener.Invocation> invocations = listener.invocations();
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   199
        assertEquals(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   200
                invocations,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   201
                List.of(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   202
                        onOpen(ws),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   203
                        onText(ws, "1", FIRST),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   204
                        onText(ws, "2", PART),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   205
                        onPing(ws, ByteBuffer.allocate(16)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   206
                        onPing(ws, ByteBuffer.allocate(32)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   207
                        onText(ws, "3", LAST),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   208
                        onPing(ws, ByteBuffer.allocate(64)),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   209
                        onClose(ws, NORMAL_CLOSURE, "no reason"))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   210
        );
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   211
    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   212
55989
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   213
    // Tease out "java.lang.IllegalStateException: Send pending" due to possible
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   214
    // race between sending a message and replenishing the permit
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   215
    @Test
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   216
    public void testManyTextMessages() {
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   217
        WebSocketImpl ws = newInstance(
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   218
                new MockListener(1),
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   219
                new TransportFactory() {
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   220
                    @Override
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   221
                    public <T> Transport<T> createTransport(Supplier<T> sendResultSupplier,
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   222
                                                            MessageStreamConsumer consumer) {
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   223
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   224
                        final Random r = new Random();
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   225
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   226
                        return new MockTransport<>(sendResultSupplier, consumer) {
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   227
                            @Override
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   228
                            protected CompletableFuture<T> defaultSend() {
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   229
                                return millis(r.nextInt(100), result());
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   230
                            }
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   231
                        };
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   232
                    }
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   233
                });
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   234
        int NUM_MESSAGES = 512;
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   235
        CompletableFuture<WebSocket> current = CompletableFuture.completedFuture(ws);
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   236
        for (int i = 0; i < NUM_MESSAGES; i++) {
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   237
            current = current.thenCompose(w -> w.sendText(" ", true));
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   238
        }
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   239
        current.join();
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   240
        MockTransport<WebSocket> transport = (MockTransport<WebSocket>) ws.transport();
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   241
        assertEquals(transport.invocations().size(), NUM_MESSAGES);
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   242
    }
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   243
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   244
    @Test
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   245
    public void testManyBinaryMessages() {
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   246
        WebSocketImpl ws = newInstance(
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   247
                new MockListener(1),
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   248
                new TransportFactory() {
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   249
                    @Override
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   250
                    public <T> Transport<T> createTransport(Supplier<T> sendResultSupplier,
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   251
                                                            MessageStreamConsumer consumer) {
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   252
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   253
                        final Random r = new Random();
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   254
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   255
                        return new MockTransport<>(sendResultSupplier, consumer) {
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   256
                            @Override
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   257
                            protected CompletableFuture<T> defaultSend() {
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   258
                                return millis(r.nextInt(150), result());
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   259
                            }
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   260
                        };
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   261
                    }
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   262
                });
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   263
        CompletableFuture<WebSocket> start = new CompletableFuture<>();
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   264
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   265
        int NUM_MESSAGES = 512;
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   266
        CompletableFuture<WebSocket> current = start;
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   267
        for (int i = 0; i < NUM_MESSAGES; i++) {
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   268
            current = current.thenComposeAsync(w -> w.sendBinary(ByteBuffer.allocate(1), true));
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   269
        }
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   270
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   271
        start.completeAsync(() -> ws);
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   272
        current.join();
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   273
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   274
        MockTransport<WebSocket> transport = (MockTransport<WebSocket>) ws.transport();
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   275
        assertEquals(transport.invocations().size(), NUM_MESSAGES);
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   276
    }
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   277
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   278
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   279
    @Test
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   280
    public void sendTextImmediately() {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   281
        WebSocketImpl ws = newInstance(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   282
                new MockListener(1),
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   283
                new TransportFactory() {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   284
                    @Override
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   285
                    public <T> Transport<T> createTransport(Supplier<T> sendResultSupplier,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   286
                                                            MessageStreamConsumer consumer) {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   287
                        return new MockTransport<>(sendResultSupplier, consumer);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   288
                    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   289
                });
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   290
        CompletableFuture.completedFuture(ws)
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   291
                .thenCompose(w -> w.sendText("1", true))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   292
                .thenCompose(w -> w.sendText("2", true))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   293
                .thenCompose(w -> w.sendText("3", true))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   294
                .join();
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   295
        MockTransport<WebSocket> transport = (MockTransport<WebSocket>) ws.transport();
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   296
        assertEquals(transport.invocations().size(), 3);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   297
    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   298
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   299
    @Test
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   300
    public void sendTextWithDelay() {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   301
        MockListener listener = new MockListener(1);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   302
        WebSocketImpl ws = newInstance(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   303
                listener,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   304
                new TransportFactory() {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   305
                    @Override
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   306
                    public <T> Transport<T> createTransport(Supplier<T> sendResultSupplier,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   307
                                                            MessageStreamConsumer consumer) {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   308
                        return new MockTransport<>(sendResultSupplier, consumer) {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   309
                            @Override
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   310
                            protected CompletableFuture<T> defaultSend() {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   311
                                return seconds(1, result());
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   312
                            }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   313
                        };
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   314
                    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   315
                });
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   316
        CompletableFuture.completedFuture(ws)
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   317
                .thenCompose(w -> w.sendText("1", true))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   318
                .thenCompose(w -> w.sendText("2", true))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   319
                .thenCompose(w -> w.sendText("3", true))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   320
                .join();
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   321
        assertEquals(listener.invocations(), List.of(onOpen(ws)));
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   322
        MockTransport<WebSocket> transport = (MockTransport<WebSocket>) ws.transport();
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   323
        assertEquals(transport.invocations().size(), 3);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   324
    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   325
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   326
    @Test
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   327
    public void sendTextMixedDelay() {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   328
        MockListener listener = new MockListener(1);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   329
        WebSocketImpl ws = newInstance(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   330
                listener,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   331
                new TransportFactory() {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   332
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   333
                    final Random r = new Random();
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   334
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   335
                    @Override
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   336
                    public <T> Transport<T> createTransport(Supplier<T> sendResultSupplier,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   337
                                                            MessageStreamConsumer consumer) {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   338
                        return new MockTransport<>(sendResultSupplier, consumer) {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   339
                            @Override
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   340
                            protected CompletableFuture<T> defaultSend() {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   341
                                return r.nextBoolean()
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   342
                                        ? seconds(1, result())
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   343
                                        : now(result());
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   344
                            }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   345
                        };
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   346
                    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   347
                });
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   348
        CompletableFuture.completedFuture(ws)
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   349
                .thenCompose(w -> w.sendText("1", true))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   350
                .thenCompose(w -> w.sendText("2", true))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   351
                .thenCompose(w -> w.sendText("3", true))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   352
                .thenCompose(w -> w.sendText("4", true))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   353
                .thenCompose(w -> w.sendText("5", true))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   354
                .thenCompose(w -> w.sendText("6", true))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   355
                .thenCompose(w -> w.sendText("7", true))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   356
                .thenCompose(w -> w.sendText("8", true))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   357
                .thenCompose(w -> w.sendText("9", true))
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   358
                .join();
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   359
        assertEquals(listener.invocations(), List.of(onOpen(ws)));
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   360
        MockTransport<WebSocket> transport = (MockTransport<WebSocket>) ws.transport();
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   361
        assertEquals(transport.invocations().size(), 9);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   362
    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   363
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   364
    @Test(enabled = false) // temporarily disabled
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   365
    public void sendControlMessagesConcurrently() {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   366
        MockListener listener = new MockListener(1);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   367
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   368
        CompletableFuture<?> first = new CompletableFuture<>(); // barrier
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   369
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   370
        WebSocketImpl ws = newInstance(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   371
                listener,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   372
                new TransportFactory() {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   373
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   374
                    final AtomicInteger i = new AtomicInteger();
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   375
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   376
                    @Override
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   377
                    public <T> Transport<T> createTransport(Supplier<T> sendResultSupplier,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   378
                                                            MessageStreamConsumer consumer) {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   379
                        return new MockTransport<>(sendResultSupplier, consumer) {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   380
                            @Override
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   381
                            protected CompletableFuture<T> defaultSend() {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   382
                                if (i.incrementAndGet() == 1) {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   383
                                    return first.thenApply(o -> result());
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   384
                                } else {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   385
                                    return now(result());
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   386
                                }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   387
                            }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   388
                        };
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   389
                    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   390
                });
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   391
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   392
        CompletableFuture<?> cf1 = ws.sendPing(ByteBuffer.allocate(0));
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   393
        CompletableFuture<?> cf2 = ws.sendPong(ByteBuffer.allocate(0));
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   394
        CompletableFuture<?> cf3 = ws.sendClose(NORMAL_CLOSURE, "");
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   395
        CompletableFuture<?> cf4 = ws.sendClose(NORMAL_CLOSURE, "");
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   396
        CompletableFuture<?> cf5 = ws.sendPing(ByteBuffer.allocate(0));
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   397
        CompletableFuture<?> cf6 = ws.sendPong(ByteBuffer.allocate(0));
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   398
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   399
        first.complete(null);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   400
        // Don't care about exceptional completion, only that all of them have
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   401
        // completed
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   402
        CompletableFuture.allOf(cf1, cf2, cf3, cf4, cf5, cf6)
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   403
                .handle((v, e) -> null).join();
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   404
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   405
        cf3.join(); /* Check that sendClose has completed normally */
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   406
        cf4.join(); /* Check that repeated sendClose has completed normally */
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   407
        assertCompletesExceptionally(IllegalStateException.class, cf5);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   408
        assertCompletesExceptionally(IllegalStateException.class, cf6);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   409
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   410
        assertEquals(listener.invocations(), List.of(onOpen(ws)));
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   411
        MockTransport<WebSocket> transport = (MockTransport<WebSocket>) ws.transport();
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   412
        assertEquals(transport.invocations().size(), 3); // 6 minus 3 that were not accepted
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   413
    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   414
55992
10cefe168d6d http-client-branch: (WebSocket) refactoring
prappo
parents: 55989
diff changeset
   415
    private static <T> CompletableFuture<T> seconds(long val, T result) {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   416
        return new CompletableFuture<T>()
55992
10cefe168d6d http-client-branch: (WebSocket) refactoring
prappo
parents: 55989
diff changeset
   417
                .completeOnTimeout(result, val, TimeUnit.SECONDS);
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   418
    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   419
55992
10cefe168d6d http-client-branch: (WebSocket) refactoring
prappo
parents: 55989
diff changeset
   420
    private static <T> CompletableFuture<T> millis(long val, T result) {
55989
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   421
        return new CompletableFuture<T>()
55992
10cefe168d6d http-client-branch: (WebSocket) refactoring
prappo
parents: 55989
diff changeset
   422
                .completeOnTimeout(result, val, TimeUnit.MILLISECONDS);
55989
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   423
    }
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
   424
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   425
    private static <T> CompletableFuture<T> now(T result) {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   426
        return CompletableFuture.completedFuture(result);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   427
    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   428
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   429
    private static WebSocketImpl newInstance(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   430
            WebSocket.Listener listener,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   431
            Collection<CompletableFuture<Consumer<MessageStreamConsumer>>> input) {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   432
        TransportFactory factory = new TransportFactory() {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   433
            @Override
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   434
            public <T> Transport<T> createTransport(Supplier<T> sendResultSupplier,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   435
                                                    MessageStreamConsumer consumer) {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   436
                return new MockTransport<T>(sendResultSupplier, consumer) {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   437
                    @Override
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   438
                    protected Collection<CompletableFuture<Consumer<MessageStreamConsumer>>> receive() {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   439
                        return input;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   440
                    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   441
                };
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   442
            }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   443
        };
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   444
        return newInstance(listener, factory);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   445
    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   446
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   447
    private static WebSocketImpl newInstance(WebSocket.Listener listener,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   448
                                             TransportFactory factory) {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   449
        URI uri = URI.create("ws://localhost");
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   450
        String subprotocol = "";
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   451
        return WebSocketImpl.newInstance(uri, subprotocol, listener, factory);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   452
    }
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
   453
}