author | prappo |
Thu, 15 Feb 2018 10:23:00 +0000 | |
branch | http-client-branch |
changeset 56129 | fe88abe462c9 |
parent 56089 | 42208b2f224e |
child 56130 | c99ed32a4d12 |
permissions | -rw-r--r-- |
56052 | 1 |
/* |
2 |
* Copyright (c) 2018, 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 |
/* |
|
25 |
* @test |
|
26 |
* @build DummyWebSocketServer |
|
27 |
* @run testng/othervm -Djdk.httpclient.HttpClient.log=trace WebSocketTest |
|
28 |
*/ |
|
29 |
||
30 |
import org.testng.annotations.Test; |
|
31 |
||
32 |
import java.io.IOException; |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
33 |
import java.net.http.WebSocket; |
56052 | 34 |
import java.nio.ByteBuffer; |
35 |
import java.nio.CharBuffer; |
|
36 |
import java.nio.channels.SocketChannel; |
|
37 |
import java.nio.charset.StandardCharsets; |
|
56058 | 38 |
import java.util.ArrayList; |
39 |
import java.util.List; |
|
56052 | 40 |
import java.util.concurrent.CompletableFuture; |
41 |
import java.util.concurrent.CompletionException; |
|
42 |
import java.util.concurrent.CompletionStage; |
|
43 |
import java.util.concurrent.TimeUnit; |
|
44 |
import java.util.concurrent.TimeoutException; |
|
45 |
||
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56064
diff
changeset
|
46 |
import static java.net.http.HttpClient.newHttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56064
diff
changeset
|
47 |
import static java.net.http.WebSocket.NORMAL_CLOSURE; |
56052 | 48 |
import static org.testng.Assert.assertEquals; |
49 |
import static org.testng.Assert.assertFalse; |
|
50 |
import static org.testng.Assert.assertThrows; |
|
51 |
import static org.testng.Assert.assertTrue; |
|
56057 | 52 |
import static org.testng.Assert.fail; |
56052 | 53 |
|
54 |
public class WebSocketTest { |
|
55 |
||
56057 | 56 |
private static final Class<NullPointerException> NPE = NullPointerException.class; |
57 |
private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class; |
|
58 |
private static final Class<IllegalStateException> ISE = IllegalStateException.class; |
|
59 |
private static final Class<IOException> IOE = IOException.class; |
|
56052 | 60 |
|
61 |
@Test |
|
56057 | 62 |
public void abort() throws Exception { |
63 |
try (DummyWebSocketServer server = serverWithCannedData(0x81, 0x00, 0x88, 0x00)) { |
|
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
64 |
server.open(); |
56057 | 65 |
CompletableFuture<Void> messageReceived = new CompletableFuture<>(); |
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
66 |
WebSocket ws = newHttpClient() |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
67 |
.newWebSocketBuilder() |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
68 |
.buildAsync(server.getURI(), new WebSocket.Listener() { |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
69 |
@Override |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
70 |
public void onOpen(WebSocket webSocket) { /* no initial request */ } |
56057 | 71 |
|
72 |
@Override |
|
73 |
public CompletionStage<?> onText(WebSocket webSocket, |
|
74 |
CharSequence message, |
|
75 |
WebSocket.MessagePart part) { |
|
76 |
messageReceived.complete(null); |
|
77 |
return null; |
|
78 |
} |
|
79 |
||
80 |
@Override |
|
81 |
public CompletionStage<?> onBinary(WebSocket webSocket, |
|
82 |
ByteBuffer message, |
|
83 |
WebSocket.MessagePart part) { |
|
84 |
messageReceived.complete(null); |
|
85 |
return null; |
|
86 |
} |
|
87 |
||
88 |
@Override |
|
89 |
public CompletionStage<?> onPing(WebSocket webSocket, |
|
90 |
ByteBuffer message) { |
|
91 |
messageReceived.complete(null); |
|
92 |
return null; |
|
93 |
} |
|
94 |
||
95 |
@Override |
|
96 |
public CompletionStage<?> onPong(WebSocket webSocket, |
|
97 |
ByteBuffer message) { |
|
98 |
messageReceived.complete(null); |
|
99 |
return null; |
|
100 |
} |
|
101 |
||
102 |
@Override |
|
103 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
104 |
int statusCode, |
|
105 |
String reason) { |
|
106 |
messageReceived.complete(null); |
|
107 |
return null; |
|
108 |
} |
|
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
109 |
}) |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
110 |
.join(); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
111 |
|
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
112 |
ws.abort(); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
113 |
// Each consecutive abort MUST be a no-op: |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
114 |
ws.abort(); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
115 |
assertTrue(ws.isInputClosed()); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
116 |
assertTrue(ws.isOutputClosed()); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
117 |
assertEquals(ws.getSubprotocol(), ""); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
118 |
ws.abort(); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
119 |
assertTrue(ws.isInputClosed()); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
120 |
assertTrue(ws.isOutputClosed()); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
121 |
assertEquals(ws.getSubprotocol(), ""); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
122 |
// At this point request MUST be a no-op: |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
123 |
ws.request(1); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
124 |
ws.request(Long.MAX_VALUE); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
125 |
// Throws IAE regardless of whether WebSocket is closed or not: |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
126 |
assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE)); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
127 |
assertThrows(IAE, () -> ws.request(Long.MIN_VALUE)); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
128 |
assertThrows(IAE, () -> ws.request(-1)); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
129 |
assertThrows(IAE, () -> ws.request(0)); |
56057 | 130 |
// Even though there is a bunch of messages readily available on the |
131 |
// wire we shouldn't have received any of them as we aborted before |
|
132 |
// the first request |
|
133 |
try { |
|
134 |
messageReceived.get(10, TimeUnit.SECONDS); |
|
135 |
fail(); |
|
136 |
} catch (TimeoutException expected) { } |
|
56058 | 137 |
// TODO: No send operations MUST succeed |
138 |
// assertCompletesExceptionally(IOE, ws.sendText("text!", false)); |
|
139 |
// assertCompletesExceptionally(IOE, ws.sendText("text!", true)); |
|
140 |
// assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(16), false)); |
|
141 |
// assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(16), true)); |
|
142 |
// assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(16))); |
|
143 |
// assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(16))); |
|
144 |
// assertCompletesExceptionally(IOE, ws.sendClose(NORMAL_CLOSURE, "a reason")); |
|
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
145 |
} |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
146 |
} |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
147 |
|
56064 | 148 |
private static DummyWebSocketServer serverWithCannedData(int... data) { |
149 |
byte[] copy = new byte[data.length]; |
|
150 |
for (int i = 0; i < data.length; i++) { |
|
151 |
copy[i] = (byte) data[i]; |
|
152 |
} |
|
153 |
return new DummyWebSocketServer() { |
|
154 |
@Override |
|
155 |
protected void serve(SocketChannel channel) throws IOException { |
|
156 |
ByteBuffer closeMessage = ByteBuffer.wrap(copy); |
|
157 |
int wrote = channel.write(closeMessage); |
|
158 |
System.out.println("Wrote bytes: " + wrote); |
|
159 |
super.serve(channel); |
|
160 |
} |
|
161 |
}; |
|
162 |
} |
|
163 |
||
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
164 |
@Test |
56052 | 165 |
public void testNull() throws IOException { |
166 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
|
167 |
server.open(); |
|
168 |
WebSocket ws = newHttpClient() |
|
169 |
.newWebSocketBuilder() |
|
170 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
171 |
.join(); |
|
172 |
||
173 |
assertThrows(NPE, () -> ws.sendText(null, false)); |
|
174 |
assertThrows(NPE, () -> ws.sendText(null, true)); |
|
175 |
assertThrows(NPE, () -> ws.sendBinary(null, false)); |
|
176 |
assertThrows(NPE, () -> ws.sendBinary(null, true)); |
|
177 |
assertThrows(NPE, () -> ws.sendPing(null)); |
|
178 |
assertThrows(NPE, () -> ws.sendPong(null)); |
|
179 |
assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null)); |
|
180 |
} |
|
181 |
} |
|
182 |
||
183 |
@Test |
|
184 |
public void testSendClose1() throws IOException { |
|
185 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
|
186 |
server.open(); |
|
187 |
WebSocket ws = newHttpClient() |
|
188 |
.newWebSocketBuilder() |
|
189 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
190 |
.join(); |
|
191 |
ws.sendClose(NORMAL_CLOSURE, "").join(); |
|
192 |
assertTrue(ws.isOutputClosed()); |
|
193 |
assertFalse(ws.isInputClosed()); |
|
194 |
assertEquals(ws.getSubprotocol(), ""); |
|
195 |
ws.request(1); // No exceptions must be thrown |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
@Test |
|
200 |
public void testSendClose2() throws Exception { |
|
201 |
try (DummyWebSocketServer server = notReadingServer()) { |
|
202 |
server.open(); |
|
203 |
WebSocket ws = newHttpClient() |
|
204 |
.newWebSocketBuilder() |
|
205 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
206 |
.join(); |
|
207 |
ByteBuffer data = ByteBuffer.allocate(65536); |
|
208 |
for (int i = 0; ; i++) { |
|
209 |
System.out.println("cycle #" + i); |
|
210 |
try { |
|
211 |
ws.sendBinary(data, true).get(10, TimeUnit.SECONDS); |
|
212 |
data.clear(); |
|
213 |
} catch (TimeoutException e) { |
|
214 |
break; |
|
215 |
} |
|
216 |
} |
|
217 |
CompletableFuture<WebSocket> cf = ws.sendClose(NORMAL_CLOSURE, ""); |
|
218 |
assertTrue(ws.isOutputClosed()); |
|
219 |
assertFalse(ws.isInputClosed()); |
|
220 |
assertEquals(ws.getSubprotocol(), ""); |
|
221 |
// The output closes regardless of whether or not the Close message |
|
222 |
// has been sent |
|
223 |
assertFalse(cf.isDone()); |
|
224 |
} |
|
225 |
} |
|
226 |
||
227 |
/* |
|
228 |
* This server does not read from the wire, allowing its client to fill up |
|
229 |
* their send buffer. Used to test scenarios with outstanding send |
|
230 |
* operations. |
|
231 |
*/ |
|
232 |
private static DummyWebSocketServer notReadingServer() { |
|
233 |
return new DummyWebSocketServer() { |
|
234 |
@Override |
|
235 |
protected void serve(SocketChannel channel) throws IOException { |
|
236 |
try { |
|
237 |
Thread.sleep(Long.MAX_VALUE); |
|
238 |
} catch (InterruptedException e) { |
|
239 |
throw new IOException(e); |
|
240 |
} |
|
241 |
} |
|
242 |
}; |
|
243 |
} |
|
244 |
||
245 |
@Test |
|
246 |
public void testIllegalArgument() throws IOException { |
|
247 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
|
248 |
server.open(); |
|
249 |
WebSocket ws = newHttpClient() |
|
250 |
.newWebSocketBuilder() |
|
251 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
252 |
.join(); |
|
253 |
||
254 |
assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(126))); |
|
255 |
assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(127))); |
|
256 |
assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(128))); |
|
257 |
assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(129))); |
|
258 |
assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(256))); |
|
259 |
||
260 |
assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(126))); |
|
261 |
assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(127))); |
|
262 |
assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(128))); |
|
263 |
assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(129))); |
|
264 |
assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(256))); |
|
265 |
||
266 |
assertCompletesExceptionally(IAE, ws.sendText(incompleteString(), true)); |
|
267 |
assertCompletesExceptionally(IAE, ws.sendText(incompleteString(), false)); |
|
268 |
assertCompletesExceptionally(IAE, ws.sendText(malformedString(), true)); |
|
269 |
assertCompletesExceptionally(IAE, ws.sendText(malformedString(), false)); |
|
270 |
||
271 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(124))); |
|
272 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(125))); |
|
273 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(128))); |
|
274 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(256))); |
|
275 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(257))); |
|
276 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWith2NBytes((123 / 2) + 1))); |
|
277 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, malformedString())); |
|
278 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, incompleteString())); |
|
279 |
||
280 |
assertCompletesExceptionally(IAE, ws.sendClose(-2, "a reason")); |
|
281 |
assertCompletesExceptionally(IAE, ws.sendClose(-1, "a reason")); |
|
282 |
assertCompletesExceptionally(IAE, ws.sendClose(0, "a reason")); |
|
283 |
assertCompletesExceptionally(IAE, ws.sendClose(1, "a reason")); |
|
284 |
assertCompletesExceptionally(IAE, ws.sendClose(500, "a reason")); |
|
285 |
assertCompletesExceptionally(IAE, ws.sendClose(998, "a reason")); |
|
286 |
assertCompletesExceptionally(IAE, ws.sendClose(999, "a reason")); |
|
287 |
assertCompletesExceptionally(IAE, ws.sendClose(1002, "a reason")); |
|
288 |
assertCompletesExceptionally(IAE, ws.sendClose(1003, "a reason")); |
|
289 |
assertCompletesExceptionally(IAE, ws.sendClose(1006, "a reason")); |
|
290 |
assertCompletesExceptionally(IAE, ws.sendClose(1007, "a reason")); |
|
291 |
assertCompletesExceptionally(IAE, ws.sendClose(1009, "a reason")); |
|
292 |
assertCompletesExceptionally(IAE, ws.sendClose(1010, "a reason")); |
|
293 |
assertCompletesExceptionally(IAE, ws.sendClose(1012, "a reason")); |
|
294 |
assertCompletesExceptionally(IAE, ws.sendClose(1013, "a reason")); |
|
295 |
assertCompletesExceptionally(IAE, ws.sendClose(1015, "a reason")); |
|
296 |
assertCompletesExceptionally(IAE, ws.sendClose(5000, "a reason")); |
|
297 |
assertCompletesExceptionally(IAE, ws.sendClose(32768, "a reason")); |
|
298 |
assertCompletesExceptionally(IAE, ws.sendClose(65535, "a reason")); |
|
299 |
assertCompletesExceptionally(IAE, ws.sendClose(65536, "a reason")); |
|
300 |
assertCompletesExceptionally(IAE, ws.sendClose(Integer.MAX_VALUE, "a reason")); |
|
301 |
assertCompletesExceptionally(IAE, ws.sendClose(Integer.MIN_VALUE, "a reason")); |
|
302 |
||
303 |
assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE)); |
|
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
304 |
assertThrows(IAE, () -> ws.request(Long.MIN_VALUE)); |
56052 | 305 |
assertThrows(IAE, () -> ws.request(-1)); |
306 |
assertThrows(IAE, () -> ws.request(0)); |
|
307 |
} |
|
308 |
} |
|
309 |
||
310 |
private static String malformedString() { |
|
311 |
return new String(new char[]{0xDC00, 0xD800}); |
|
312 |
} |
|
313 |
||
314 |
private static String incompleteString() { |
|
315 |
return new String(new char[]{0xD800}); |
|
316 |
} |
|
317 |
||
318 |
private static String stringWithNBytes(int n) { |
|
319 |
StringBuilder sb = new StringBuilder(n); |
|
320 |
for (int i = 0; i < n; i++) { |
|
321 |
sb.append("A"); |
|
322 |
} |
|
323 |
return sb.toString(); |
|
324 |
} |
|
325 |
||
326 |
private static String stringWith2NBytes(int n) { |
|
327 |
// Russian alphabet repeated cyclically |
|
328 |
char FIRST = '\u0410'; |
|
329 |
char LAST = '\u042F'; |
|
330 |
StringBuilder sb = new StringBuilder(n); |
|
331 |
char c = FIRST; |
|
332 |
for (int i = 0; i < n; i++) { |
|
333 |
if (++c > LAST) { |
|
334 |
c = FIRST; |
|
335 |
} |
|
336 |
sb.append(c); |
|
337 |
} |
|
338 |
String s = sb.toString(); |
|
339 |
assert s.length() == n && s.getBytes(StandardCharsets.UTF_8).length == 2 * n; |
|
340 |
return s; |
|
341 |
} |
|
342 |
||
343 |
private static void assertCompletesExceptionally(Class<? extends Throwable> clazz, |
|
344 |
CompletableFuture<?> stage) { |
|
345 |
stage.handle((result, error) -> { |
|
346 |
if (error instanceof CompletionException) { |
|
347 |
Throwable cause = error.getCause(); |
|
348 |
if (cause == null) { |
|
349 |
throw new AssertionError("Unexpected null cause: " + error); |
|
350 |
} |
|
351 |
assertException(clazz, cause); |
|
352 |
} else { |
|
353 |
assertException(clazz, error); |
|
354 |
} |
|
355 |
return null; |
|
356 |
}).join(); |
|
357 |
} |
|
358 |
||
359 |
private static void assertException(Class<? extends Throwable> clazz, |
|
360 |
Throwable t) { |
|
361 |
if (t == null) { |
|
362 |
throw new AssertionError("Expected " + clazz + ", caught nothing"); |
|
363 |
} |
|
364 |
if (!clazz.isInstance(t)) { |
|
365 |
throw new AssertionError("Expected " + clazz + ", caught " + t); |
|
366 |
} |
|
367 |
} |
|
368 |
||
369 |
@Test |
|
370 |
public void testIllegalStateOutstanding1() throws Exception { |
|
371 |
try (DummyWebSocketServer server = notReadingServer()) { |
|
372 |
server.open(); |
|
373 |
WebSocket ws = newHttpClient() |
|
374 |
.newWebSocketBuilder() |
|
375 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
376 |
.join(); |
|
377 |
||
378 |
ByteBuffer data = ByteBuffer.allocate(65536); |
|
379 |
for (int i = 0; ; i++) { |
|
380 |
System.out.println("cycle #" + i); |
|
381 |
try { |
|
382 |
ws.sendBinary(data, true).get(10, TimeUnit.SECONDS); |
|
383 |
data.clear(); |
|
384 |
} catch (TimeoutException e) { |
|
385 |
break; |
|
386 |
} |
|
387 |
} |
|
388 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
|
389 |
assertCompletesExceptionally(ISE, ws.sendText("", true)); |
|
390 |
} |
|
391 |
} |
|
392 |
||
393 |
@Test |
|
394 |
public void testIllegalStateOutstanding2() throws Exception { |
|
395 |
try (DummyWebSocketServer server = notReadingServer()) { |
|
396 |
server.open(); |
|
397 |
WebSocket ws = newHttpClient() |
|
398 |
.newWebSocketBuilder() |
|
399 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
400 |
.join(); |
|
401 |
||
402 |
CharBuffer data = CharBuffer.allocate(65536); |
|
403 |
for (int i = 0; ; i++) { |
|
404 |
System.out.println("cycle #" + i); |
|
405 |
try { |
|
406 |
ws.sendText(data, true).get(10, TimeUnit.SECONDS); |
|
407 |
data.clear(); |
|
408 |
} catch (TimeoutException e) { |
|
409 |
break; |
|
410 |
} |
|
411 |
} |
|
412 |
assertCompletesExceptionally(ISE, ws.sendText("", true)); |
|
413 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
|
414 |
} |
|
415 |
} |
|
416 |
||
417 |
@Test |
|
418 |
public void testIllegalStateIntermixed1() throws IOException { |
|
419 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
|
420 |
server.open(); |
|
421 |
WebSocket ws = newHttpClient() |
|
422 |
.newWebSocketBuilder() |
|
423 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
424 |
.join(); |
|
425 |
||
426 |
ws.sendBinary(ByteBuffer.allocate(16), false).join(); |
|
427 |
assertCompletesExceptionally(ISE, ws.sendText("text", false)); |
|
428 |
assertCompletesExceptionally(ISE, ws.sendText("text", true)); |
|
429 |
} |
|
430 |
} |
|
431 |
||
432 |
@Test |
|
433 |
public void testIllegalStateIntermixed2() throws IOException { |
|
434 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
|
435 |
server.open(); |
|
436 |
WebSocket ws = newHttpClient() |
|
437 |
.newWebSocketBuilder() |
|
438 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
439 |
.join(); |
|
440 |
||
441 |
ws.sendText("text", false).join(); |
|
442 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(16), false)); |
|
443 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(16), true)); |
|
444 |
} |
|
445 |
} |
|
446 |
||
447 |
@Test |
|
448 |
public void testIllegalStateSendClose() throws IOException { |
|
449 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
|
450 |
server.open(); |
|
451 |
WebSocket ws = newHttpClient() |
|
452 |
.newWebSocketBuilder() |
|
453 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
454 |
.join(); |
|
455 |
||
456 |
ws.sendClose(NORMAL_CLOSURE, "normal close").join(); |
|
457 |
||
458 |
assertCompletesExceptionally(ISE, ws.sendText("", true)); |
|
459 |
assertCompletesExceptionally(ISE, ws.sendText("", false)); |
|
460 |
assertCompletesExceptionally(ISE, ws.sendText("abc", true)); |
|
461 |
assertCompletesExceptionally(ISE, ws.sendText("abc", false)); |
|
462 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
|
463 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(0), false)); |
|
464 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(1), true)); |
|
465 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(1), false)); |
|
466 |
||
467 |
assertCompletesExceptionally(ISE, ws.sendPing(ByteBuffer.allocate(125))); |
|
468 |
assertCompletesExceptionally(ISE, ws.sendPing(ByteBuffer.allocate(124))); |
|
469 |
assertCompletesExceptionally(ISE, ws.sendPing(ByteBuffer.allocate(1))); |
|
470 |
assertCompletesExceptionally(ISE, ws.sendPing(ByteBuffer.allocate(0))); |
|
471 |
||
472 |
assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(125))); |
|
473 |
assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(124))); |
|
474 |
assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(1))); |
|
475 |
assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(0))); |
|
476 |
} |
|
477 |
} |
|
478 |
||
479 |
@Test |
|
480 |
public void testIllegalStateOnClose() throws Exception { |
|
56057 | 481 |
try (DummyWebSocketServer server = serverWithCannedData(0x88, 0x00)) { |
56052 | 482 |
server.open(); |
483 |
CompletableFuture<Void> onCloseCalled = new CompletableFuture<>(); |
|
484 |
CompletableFuture<Void> canClose = new CompletableFuture<>(); |
|
485 |
||
486 |
WebSocket ws = newHttpClient() |
|
487 |
.newWebSocketBuilder() |
|
488 |
.buildAsync(server.getURI(), new WebSocket.Listener() { |
|
489 |
@Override |
|
490 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
491 |
int statusCode, |
|
492 |
String reason) { |
|
493 |
System.out.println("onClose(" + statusCode + ")"); |
|
494 |
onCloseCalled.complete(null); |
|
495 |
return canClose; |
|
496 |
} |
|
497 |
||
498 |
@Override |
|
499 |
public void onError(WebSocket webSocket, Throwable error) { |
|
500 |
System.out.println("onError(" + error + ")"); |
|
501 |
error.printStackTrace(); |
|
502 |
} |
|
503 |
}) |
|
504 |
.join(); |
|
505 |
||
506 |
onCloseCalled.join(); // Wait for onClose to be called |
|
507 |
TimeUnit.SECONDS.sleep(5); // Give canClose some time to reach the WebSocket |
|
508 |
canClose.complete(null); // Signal to the WebSocket it can close the output |
|
509 |
||
510 |
assertCompletesExceptionally(ISE, ws.sendText("", true)); |
|
511 |
assertCompletesExceptionally(ISE, ws.sendText("", false)); |
|
512 |
assertCompletesExceptionally(ISE, ws.sendText("abc", true)); |
|
513 |
assertCompletesExceptionally(ISE, ws.sendText("abc", false)); |
|
514 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
|
515 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(0), false)); |
|
516 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(1), true)); |
|
517 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(1), false)); |
|
518 |
||
519 |
assertCompletesExceptionally(ISE, ws.sendPing(ByteBuffer.allocate(125))); |
|
520 |
assertCompletesExceptionally(ISE, ws.sendPing(ByteBuffer.allocate(124))); |
|
521 |
assertCompletesExceptionally(ISE, ws.sendPing(ByteBuffer.allocate(1))); |
|
522 |
assertCompletesExceptionally(ISE, ws.sendPing(ByteBuffer.allocate(0))); |
|
523 |
||
524 |
assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(125))); |
|
525 |
assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(124))); |
|
526 |
assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(1))); |
|
527 |
assertCompletesExceptionally(ISE, ws.sendPong(ByteBuffer.allocate(0))); |
|
528 |
} |
|
529 |
} |
|
56058 | 530 |
|
531 |
@Test |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
532 |
public void simpleAggregatingMessages() throws IOException { |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
533 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
534 |
List<String> expected = List.of("alpha", "beta", "gamma", "delta"); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
535 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
536 |
int[] binary = new int[]{ |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
537 |
0x81, 0x05, 0x61, 0x6c, 0x70, 0x68, 0x61, // "alpha" |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
538 |
0x01, 0x02, 0x62, 0x65, // "be |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
539 |
0x80, 0x02, 0x74, 0x61, // ta" |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
540 |
0x01, 0x01, 0x67, // "g |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
541 |
0x00, 0x01, 0x61, // a |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
542 |
0x00, 0x00, // |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
543 |
0x00, 0x00, // |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
544 |
0x00, 0x01, 0x6d, // m |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
545 |
0x00, 0x01, 0x6d, // m |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
546 |
0x80, 0x01, 0x61, // a" |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
547 |
0x8a, 0x00, // <PONG> |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
548 |
0x01, 0x04, 0x64, 0x65, 0x6c, 0x74, // "delt |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
549 |
0x00, 0x01, 0x61, // a |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
550 |
0x80, 0x00, // " |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
551 |
0x88, 0x00 // <CLOSE> |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
552 |
}; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
553 |
CompletableFuture<List<String>> actual = new CompletableFuture<>(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
554 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
555 |
try (DummyWebSocketServer server = serverWithCannedData(binary)) { |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
556 |
server.open(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
557 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
558 |
WebSocket.Listener listener = new WebSocket.Listener() { |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
559 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
560 |
StringBuilder text = new StringBuilder(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
561 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
562 |
@Override |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
563 |
public CompletionStage<?> onText(WebSocket webSocket, |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
564 |
CharSequence message, |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
565 |
WebSocket.MessagePart part) { |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
566 |
webSocket.request(1); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
567 |
String str = null; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
568 |
switch (part) { |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
569 |
case FIRST: |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
570 |
case PART: |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
571 |
text.append(message); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
572 |
return null; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
573 |
case LAST: |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
574 |
text.append(message); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
575 |
str = text.toString(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
576 |
text.setLength(0); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
577 |
break; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
578 |
case WHOLE: |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
579 |
str = message.toString(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
580 |
break; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
581 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
582 |
processWholeText(str); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
583 |
return null; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
584 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
585 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
586 |
private void processWholeText(String string) { |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
587 |
// -- your code here -- |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
588 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
589 |
}; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
590 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
591 |
newHttpClient().newWebSocketBuilder() |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
592 |
.buildAsync(server.getURI(), listener) |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
593 |
.join(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
594 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
595 |
List<String> a = actual.join(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
596 |
assertEquals(a, expected); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
597 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
598 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
599 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
600 |
@Test |
56058 | 601 |
public void aggregatingMessages() throws IOException { |
602 |
||
603 |
List<String> expected = List.of("alpha", "beta", "gamma", "delta"); |
|
604 |
||
605 |
int[] binary = new int[]{ |
|
606 |
0x81, 0x05, 0x61, 0x6c, 0x70, 0x68, 0x61, // "alpha" |
|
607 |
0x01, 0x02, 0x62, 0x65, // "be |
|
608 |
0x80, 0x02, 0x74, 0x61, // ta" |
|
609 |
0x01, 0x01, 0x67, // "g |
|
610 |
0x00, 0x01, 0x61, // a |
|
611 |
0x00, 0x00, // |
|
612 |
0x00, 0x00, // |
|
613 |
0x00, 0x01, 0x6d, // m |
|
614 |
0x00, 0x01, 0x6d, // m |
|
615 |
0x80, 0x01, 0x61, // a" |
|
616 |
0x8a, 0x00, // <PONG> |
|
617 |
0x01, 0x04, 0x64, 0x65, 0x6c, 0x74, // "delt |
|
618 |
0x00, 0x01, 0x61, // a |
|
619 |
0x80, 0x00, // " |
|
620 |
0x88, 0x00 // <CLOSE> |
|
621 |
}; |
|
622 |
CompletableFuture<List<String>> actual = new CompletableFuture<>(); |
|
623 |
||
624 |
||
625 |
try (DummyWebSocketServer server = serverWithCannedData(binary)) { |
|
626 |
server.open(); |
|
627 |
||
628 |
WebSocket.Listener listener = new WebSocket.Listener() { |
|
629 |
||
630 |
List<CharSequence> parts = new ArrayList<>(); |
|
631 |
CompletableFuture<?> currentCf; |
|
632 |
List<String> collected = new ArrayList<>(); |
|
633 |
||
634 |
@Override |
|
635 |
public CompletionStage<?> onText(WebSocket webSocket, |
|
636 |
CharSequence message, |
|
637 |
WebSocket.MessagePart part) { |
|
638 |
switch (part) { |
|
639 |
case WHOLE: |
|
640 |
CompletableFuture<?> cf = new CompletableFuture<>(); |
|
641 |
cf.thenRun(() -> webSocket.request(1)); |
|
642 |
processWholeMessage(List.of(message), cf); |
|
643 |
return cf; |
|
644 |
case FIRST: |
|
645 |
parts.add(message); |
|
646 |
currentCf = new CompletableFuture<>(); |
|
647 |
currentCf.thenRun(() -> webSocket.request(1)); |
|
648 |
webSocket.request(1); |
|
649 |
break; |
|
650 |
case PART: |
|
651 |
parts.add(message); |
|
652 |
webSocket.request(1); |
|
653 |
break; |
|
654 |
case LAST: |
|
655 |
parts.add(message); |
|
656 |
List<CharSequence> copy = List.copyOf(parts); |
|
657 |
parts.clear(); |
|
658 |
CompletableFuture<?> cf1 = currentCf; |
|
659 |
currentCf = null; |
|
660 |
processWholeMessage(copy, cf1); |
|
661 |
return cf1; |
|
662 |
} |
|
663 |
return currentCf; |
|
664 |
} |
|
665 |
||
666 |
@Override |
|
667 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
668 |
int statusCode, |
|
669 |
String reason) { |
|
670 |
actual.complete(collected); |
|
671 |
return null; |
|
672 |
} |
|
673 |
||
674 |
public void processWholeMessage(List<CharSequence> data, |
|
675 |
CompletableFuture<?> cf) { |
|
676 |
StringBuilder b = new StringBuilder(); |
|
677 |
data.forEach(b::append); |
|
678 |
String s = b.toString(); |
|
679 |
System.out.println(s); |
|
680 |
cf.complete(null); |
|
681 |
collected.add(s); |
|
682 |
} |
|
683 |
}; |
|
684 |
WebSocket ws = newHttpClient() |
|
685 |
.newWebSocketBuilder() |
|
686 |
.buildAsync(server.getURI(), listener) |
|
687 |
.join(); |
|
688 |
||
689 |
List<String> a = actual.join(); |
|
690 |
assertEquals(a, expected); |
|
691 |
} |
|
692 |
} |
|
56052 | 693 |
} |