48083
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
package jdk.incubator.http.internal.websocket;
|
|
25 |
|
|
26 |
import jdk.incubator.http.WebSocket;
|
|
27 |
import org.testng.annotations.Test;
|
|
28 |
|
|
29 |
import java.net.URI;
|
|
30 |
import java.nio.ByteBuffer;
|
|
31 |
import java.util.List;
|
|
32 |
import java.util.concurrent.CompletableFuture;
|
|
33 |
import java.util.concurrent.CompletionStage;
|
|
34 |
import java.util.concurrent.TimeUnit;
|
|
35 |
|
|
36 |
import static java.util.concurrent.CompletableFuture.completedStage;
|
|
37 |
import static jdk.incubator.http.WebSocket.MessagePart.FIRST;
|
|
38 |
import static jdk.incubator.http.WebSocket.MessagePart.LAST;
|
|
39 |
import static jdk.incubator.http.WebSocket.MessagePart.PART;
|
|
40 |
import static jdk.incubator.http.WebSocket.MessagePart.WHOLE;
|
|
41 |
import static jdk.incubator.http.WebSocket.NORMAL_CLOSURE;
|
|
42 |
import static jdk.incubator.http.internal.common.Pair.pair;
|
|
43 |
import static jdk.incubator.http.internal.websocket.MockListener.ListenerInvocation.onClose;
|
|
44 |
import static jdk.incubator.http.internal.websocket.MockListener.ListenerInvocation.onError;
|
|
45 |
import static jdk.incubator.http.internal.websocket.MockListener.ListenerInvocation.onOpen;
|
|
46 |
import static jdk.incubator.http.internal.websocket.MockListener.ListenerInvocation.onPing;
|
|
47 |
import static jdk.incubator.http.internal.websocket.MockListener.ListenerInvocation.onPong;
|
|
48 |
import static jdk.incubator.http.internal.websocket.MockListener.ListenerInvocation.onText;
|
|
49 |
import static org.testng.Assert.assertEquals;
|
|
50 |
|
|
51 |
public class ReceivingTest {
|
|
52 |
|
|
53 |
// TODO: request in onClose/onError
|
|
54 |
// TODO: throw exception in onClose/onError
|
|
55 |
// TODO: exception is thrown from request()
|
|
56 |
|
|
57 |
@Test
|
|
58 |
public void testNonPositiveRequest() throws Exception {
|
|
59 |
MockListener listener = new MockListener(Long.MAX_VALUE) {
|
|
60 |
@Override
|
|
61 |
protected void onOpen0(WebSocket webSocket) {
|
|
62 |
webSocket.request(0);
|
|
63 |
}
|
|
64 |
};
|
|
65 |
MockTransport transport = new MockTransport() {
|
|
66 |
@Override
|
|
67 |
protected Receiver newReceiver(MessageStreamConsumer consumer) {
|
|
68 |
return new MockReceiver(consumer, channel, pair(now(), m -> m.onText("1", WHOLE)));
|
|
69 |
}
|
|
70 |
};
|
|
71 |
WebSocket ws = newInstance(listener, transport);
|
|
72 |
listener.onCloseOrOnErrorCalled().get(10, TimeUnit.SECONDS);
|
|
73 |
List<MockListener.ListenerInvocation> invocations = listener.invocations();
|
|
74 |
assertEquals(invocations, List.of(onOpen(ws), onError(ws, IllegalArgumentException.class)));
|
|
75 |
}
|
|
76 |
|
|
77 |
@Test
|
|
78 |
public void testText1() throws Exception {
|
|
79 |
MockListener listener = new MockListener(Long.MAX_VALUE);
|
|
80 |
MockTransport transport = new MockTransport() {
|
|
81 |
@Override
|
|
82 |
protected Receiver newReceiver(MessageStreamConsumer consumer) {
|
|
83 |
return new MockReceiver(consumer, channel,
|
|
84 |
pair(now(), m -> m.onText("1", FIRST)),
|
|
85 |
pair(now(), m -> m.onText("2", PART)),
|
|
86 |
pair(now(), m -> m.onText("3", LAST)),
|
|
87 |
pair(now(), m -> m.onClose(NORMAL_CLOSURE, "no reason")));
|
|
88 |
}
|
|
89 |
};
|
|
90 |
WebSocket ws = newInstance(listener, transport);
|
|
91 |
listener.onCloseOrOnErrorCalled().get(10, TimeUnit.SECONDS);
|
|
92 |
List<MockListener.ListenerInvocation> invocations = listener.invocations();
|
|
93 |
assertEquals(invocations, List.of(onOpen(ws),
|
|
94 |
onText(ws, "1", FIRST),
|
|
95 |
onText(ws, "2", PART),
|
|
96 |
onText(ws, "3", LAST),
|
|
97 |
onClose(ws, NORMAL_CLOSURE, "no reason")));
|
|
98 |
}
|
|
99 |
|
|
100 |
@Test
|
|
101 |
public void testText2() throws Exception {
|
|
102 |
MockListener listener = new MockListener(Long.MAX_VALUE);
|
|
103 |
MockTransport transport = new MockTransport() {
|
|
104 |
@Override
|
|
105 |
protected Receiver newReceiver(MessageStreamConsumer consumer) {
|
|
106 |
return new MockReceiver(consumer, channel,
|
|
107 |
pair(now(), m -> m.onText("1", FIRST)),
|
|
108 |
pair(seconds(1), m -> m.onText("2", PART)),
|
|
109 |
pair(now(), m -> m.onText("3", LAST)),
|
|
110 |
pair(seconds(1), m -> m.onClose(NORMAL_CLOSURE, "no reason")));
|
|
111 |
}
|
|
112 |
};
|
|
113 |
WebSocket ws = newInstance(listener, transport);
|
|
114 |
listener.onCloseOrOnErrorCalled().get(10, TimeUnit.SECONDS);
|
|
115 |
List<MockListener.ListenerInvocation> invocations = listener.invocations();
|
|
116 |
assertEquals(invocations, List.of(onOpen(ws),
|
|
117 |
onText(ws, "1", FIRST),
|
|
118 |
onText(ws, "2", PART),
|
|
119 |
onText(ws, "3", LAST),
|
|
120 |
onClose(ws, NORMAL_CLOSURE, "no reason")));
|
|
121 |
}
|
|
122 |
|
|
123 |
@Test
|
|
124 |
public void testTextIntermixedWithPongs() throws Exception {
|
|
125 |
MockListener listener = new MockListener(Long.MAX_VALUE);
|
|
126 |
MockTransport transport = new MockTransport() {
|
|
127 |
@Override
|
|
128 |
protected Receiver newReceiver(MessageStreamConsumer consumer) {
|
|
129 |
return new MockReceiver(consumer, channel,
|
|
130 |
pair(now(), m -> m.onText("1", FIRST)),
|
|
131 |
pair(now(), m -> m.onText("2", PART)),
|
|
132 |
pair(now(), m -> m.onPong(ByteBuffer.allocate(16))),
|
|
133 |
pair(seconds(1), m -> m.onPong(ByteBuffer.allocate(32))),
|
|
134 |
pair(now(), m -> m.onText("3", LAST)),
|
|
135 |
pair(now(), m -> m.onPong(ByteBuffer.allocate(64))),
|
|
136 |
pair(now(), m -> m.onClose(NORMAL_CLOSURE, "no reason")));
|
|
137 |
}
|
|
138 |
};
|
|
139 |
WebSocket ws = newInstance(listener, transport);
|
|
140 |
listener.onCloseOrOnErrorCalled().get(10, TimeUnit.SECONDS);
|
|
141 |
List<MockListener.ListenerInvocation> invocations = listener.invocations();
|
|
142 |
assertEquals(invocations, List.of(onOpen(ws),
|
|
143 |
onText(ws, "1", FIRST),
|
|
144 |
onText(ws, "2", PART),
|
|
145 |
onPong(ws, ByteBuffer.allocate(16)),
|
|
146 |
onPong(ws, ByteBuffer.allocate(32)),
|
|
147 |
onText(ws, "3", LAST),
|
|
148 |
onPong(ws, ByteBuffer.allocate(64)),
|
|
149 |
onClose(ws, NORMAL_CLOSURE, "no reason")));
|
|
150 |
}
|
|
151 |
|
|
152 |
@Test
|
|
153 |
public void testTextIntermixedWithPings() throws Exception {
|
|
154 |
MockListener listener = new MockListener(Long.MAX_VALUE);
|
|
155 |
MockTransport transport = new MockTransport() {
|
|
156 |
@Override
|
|
157 |
protected Receiver newReceiver(MessageStreamConsumer consumer) {
|
|
158 |
return new MockReceiver(consumer, channel,
|
|
159 |
pair(now(), m -> m.onText("1", FIRST)),
|
|
160 |
pair(now(), m -> m.onText("2", PART)),
|
|
161 |
pair(now(), m -> m.onPing(ByteBuffer.allocate(16))),
|
|
162 |
pair(seconds(1), m -> m.onPing(ByteBuffer.allocate(32))),
|
|
163 |
pair(now(), m -> m.onText("3", LAST)),
|
|
164 |
pair(now(), m -> m.onPing(ByteBuffer.allocate(64))),
|
|
165 |
pair(now(), m -> m.onClose(NORMAL_CLOSURE, "no reason")));
|
|
166 |
}
|
|
167 |
|
|
168 |
@Override
|
|
169 |
protected Transmitter newTransmitter() {
|
|
170 |
return new MockTransmitter() {
|
|
171 |
@Override
|
|
172 |
protected CompletionStage<?> whenSent() {
|
|
173 |
return now();
|
|
174 |
}
|
|
175 |
};
|
|
176 |
}
|
|
177 |
};
|
|
178 |
WebSocket ws = newInstance(listener, transport);
|
|
179 |
listener.onCloseOrOnErrorCalled().get(10, TimeUnit.SECONDS);
|
|
180 |
List<MockListener.ListenerInvocation> invocations = listener.invocations();
|
|
181 |
System.out.println(invocations);
|
|
182 |
assertEquals(invocations, List.of(onOpen(ws),
|
|
183 |
onText(ws, "1", FIRST),
|
|
184 |
onText(ws, "2", PART),
|
|
185 |
onPing(ws, ByteBuffer.allocate(16)),
|
|
186 |
onPing(ws, ByteBuffer.allocate(32)),
|
|
187 |
onText(ws, "3", LAST),
|
|
188 |
onPing(ws, ByteBuffer.allocate(64)),
|
|
189 |
onClose(ws, NORMAL_CLOSURE, "no reason")));
|
|
190 |
}
|
|
191 |
|
|
192 |
private static CompletionStage<?> seconds(long s) {
|
|
193 |
return new CompletableFuture<>().completeOnTimeout(null, s, TimeUnit.SECONDS);
|
|
194 |
}
|
|
195 |
|
|
196 |
private static CompletionStage<?> now() {
|
|
197 |
return completedStage(null);
|
|
198 |
}
|
|
199 |
|
|
200 |
private static WebSocket newInstance(WebSocket.Listener listener,
|
|
201 |
TransportSupplier transport) {
|
|
202 |
URI uri = URI.create("ws://localhost");
|
|
203 |
String subprotocol = "";
|
|
204 |
return WebSocketImpl.newInstance(uri, subprotocol, listener, transport);
|
|
205 |
}
|
|
206 |
}
|