author | prappo |
Fri, 09 Mar 2018 16:47:00 +0000 | |
branch | http-client-branch |
changeset 56269 | 234813fd33bc |
parent 56263 | 4933a477d628 |
child 56294 | 181bc33917e4 |
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 |
|
56263 | 27 |
* @run testng/othervm WebSocketTest |
56052 | 28 |
*/ |
29 |
import org.testng.annotations.Test; |
|
30 |
||
31 |
import java.io.IOException; |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
32 |
import java.net.http.WebSocket; |
56052 | 33 |
import java.nio.ByteBuffer; |
34 |
import java.nio.CharBuffer; |
|
35 |
import java.nio.channels.SocketChannel; |
|
36 |
import java.nio.charset.StandardCharsets; |
|
56058 | 37 |
import java.util.ArrayList; |
56263 | 38 |
import java.util.Arrays; |
56058 | 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; |
|
56263 | 45 |
import java.util.stream.Collectors; |
56052 | 46 |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56064
diff
changeset
|
47 |
import static java.net.http.HttpClient.newHttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56064
diff
changeset
|
48 |
import static java.net.http.WebSocket.NORMAL_CLOSURE; |
56052 | 49 |
import static org.testng.Assert.assertEquals; |
50 |
import static org.testng.Assert.assertFalse; |
|
51 |
import static org.testng.Assert.assertThrows; |
|
52 |
import static org.testng.Assert.assertTrue; |
|
56057 | 53 |
import static org.testng.Assert.fail; |
56052 | 54 |
|
55 |
public class WebSocketTest { |
|
56 |
||
56057 | 57 |
private static final Class<NullPointerException> NPE = NullPointerException.class; |
58 |
private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class; |
|
59 |
private static final Class<IllegalStateException> ISE = IllegalStateException.class; |
|
60 |
private static final Class<IOException> IOE = IOException.class; |
|
56052 | 61 |
|
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
62 |
@Test |
56263 | 63 |
public void immediateAbort() throws Exception { |
56057 | 64 |
try (DummyWebSocketServer server = serverWithCannedData(0x81, 0x00, 0x88, 0x00)) { |
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
65 |
server.open(); |
56057 | 66 |
CompletableFuture<Void> messageReceived = new CompletableFuture<>(); |
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
67 |
WebSocket ws = newHttpClient() |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
68 |
.newWebSocketBuilder() |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
69 |
.buildAsync(server.getURI(), new WebSocket.Listener() { |
56263 | 70 |
|
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
71 |
@Override |
56263 | 72 |
public void onOpen(WebSocket webSocket) { |
73 |
/* no initial request */ |
|
74 |
} |
|
56057 | 75 |
|
76 |
@Override |
|
77 |
public CompletionStage<?> onText(WebSocket webSocket, |
|
78 |
CharSequence message, |
|
79 |
WebSocket.MessagePart part) { |
|
80 |
messageReceived.complete(null); |
|
81 |
return null; |
|
82 |
} |
|
83 |
||
84 |
@Override |
|
85 |
public CompletionStage<?> onBinary(WebSocket webSocket, |
|
86 |
ByteBuffer message, |
|
87 |
WebSocket.MessagePart part) { |
|
88 |
messageReceived.complete(null); |
|
89 |
return null; |
|
90 |
} |
|
91 |
||
92 |
@Override |
|
93 |
public CompletionStage<?> onPing(WebSocket webSocket, |
|
94 |
ByteBuffer message) { |
|
95 |
messageReceived.complete(null); |
|
96 |
return null; |
|
97 |
} |
|
98 |
||
99 |
@Override |
|
100 |
public CompletionStage<?> onPong(WebSocket webSocket, |
|
101 |
ByteBuffer message) { |
|
102 |
messageReceived.complete(null); |
|
103 |
return null; |
|
104 |
} |
|
105 |
||
106 |
@Override |
|
107 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
108 |
int statusCode, |
|
109 |
String reason) { |
|
110 |
messageReceived.complete(null); |
|
111 |
return null; |
|
112 |
} |
|
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
113 |
}) |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
114 |
.join(); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
115 |
|
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
116 |
ws.abort(); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
117 |
// Each consecutive abort MUST be a no-op: |
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 |
ws.abort(); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
123 |
assertTrue(ws.isInputClosed()); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
124 |
assertTrue(ws.isOutputClosed()); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
125 |
assertEquals(ws.getSubprotocol(), ""); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
126 |
// At this point request MUST be a no-op: |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
127 |
ws.request(1); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
128 |
ws.request(Long.MAX_VALUE); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
129 |
// Throws IAE regardless of whether WebSocket is closed or not: |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
130 |
assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE)); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
131 |
assertThrows(IAE, () -> ws.request(Long.MIN_VALUE)); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
132 |
assertThrows(IAE, () -> ws.request(-1)); |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
133 |
assertThrows(IAE, () -> ws.request(0)); |
56057 | 134 |
// Even though there is a bunch of messages readily available on the |
135 |
// wire we shouldn't have received any of them as we aborted before |
|
136 |
// the first request |
|
137 |
try { |
|
138 |
messageReceived.get(10, TimeUnit.SECONDS); |
|
139 |
fail(); |
|
56263 | 140 |
} catch (TimeoutException expected) { |
141 |
System.out.println("Finished waiting"); |
|
142 |
} |
|
143 |
assertCompletesExceptionally(IOE, ws.sendText("text!", false)); |
|
144 |
assertCompletesExceptionally(IOE, ws.sendText("text!", true)); |
|
145 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(16), false)); |
|
146 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(16), true)); |
|
147 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(16))); |
|
148 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(16))); |
|
149 |
// Checked last because it changes the state of WebSocket |
|
150 |
assertCompletesExceptionally(IOE, ws.sendClose(NORMAL_CLOSURE, "a reason")); |
|
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
151 |
} |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
152 |
} |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
153 |
|
56064 | 154 |
private static DummyWebSocketServer serverWithCannedData(int... data) { |
155 |
byte[] copy = new byte[data.length]; |
|
156 |
for (int i = 0; i < data.length; i++) { |
|
157 |
copy[i] = (byte) data[i]; |
|
158 |
} |
|
159 |
return new DummyWebSocketServer() { |
|
160 |
@Override |
|
161 |
protected void serve(SocketChannel channel) throws IOException { |
|
162 |
ByteBuffer closeMessage = ByteBuffer.wrap(copy); |
|
56263 | 163 |
channel.write(closeMessage); |
56064 | 164 |
super.serve(channel); |
165 |
} |
|
166 |
}; |
|
167 |
} |
|
168 |
||
56263 | 169 |
private static void assertCompletesExceptionally(Class<? extends Throwable> clazz, |
170 |
CompletableFuture<?> stage) { |
|
171 |
stage.handle((result, error) -> { |
|
172 |
if (error instanceof CompletionException) { |
|
173 |
Throwable cause = error.getCause(); |
|
174 |
if (cause == null) { |
|
175 |
throw new AssertionError("Unexpected null cause: " + error); |
|
176 |
} |
|
177 |
assertException(clazz, cause); |
|
178 |
} else { |
|
179 |
assertException(clazz, error); |
|
180 |
} |
|
181 |
return null; |
|
182 |
}).join(); |
|
183 |
} |
|
184 |
||
185 |
private static void assertException(Class<? extends Throwable> clazz, |
|
186 |
Throwable t) { |
|
187 |
if (t == null) { |
|
188 |
throw new AssertionError("Expected " + clazz + ", caught nothing"); |
|
189 |
} |
|
190 |
if (!clazz.isInstance(t)) { |
|
191 |
throw new AssertionError("Expected " + clazz + ", caught " + t); |
|
192 |
} |
|
193 |
} |
|
194 |
||
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
195 |
@Test |
56263 | 196 |
public void sendMethodsThrowNPE() throws IOException { |
56052 | 197 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
198 |
server.open(); |
|
199 |
WebSocket ws = newHttpClient() |
|
200 |
.newWebSocketBuilder() |
|
201 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
202 |
.join(); |
|
203 |
||
204 |
assertThrows(NPE, () -> ws.sendText(null, false)); |
|
205 |
assertThrows(NPE, () -> ws.sendText(null, true)); |
|
206 |
assertThrows(NPE, () -> ws.sendBinary(null, false)); |
|
207 |
assertThrows(NPE, () -> ws.sendBinary(null, true)); |
|
208 |
assertThrows(NPE, () -> ws.sendPing(null)); |
|
209 |
assertThrows(NPE, () -> ws.sendPong(null)); |
|
210 |
assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null)); |
|
56263 | 211 |
|
212 |
ws.abort(); |
|
213 |
||
214 |
assertThrows(NPE, () -> ws.sendText(null, false)); |
|
215 |
assertThrows(NPE, () -> ws.sendText(null, true)); |
|
216 |
assertThrows(NPE, () -> ws.sendBinary(null, false)); |
|
217 |
assertThrows(NPE, () -> ws.sendBinary(null, true)); |
|
218 |
assertThrows(NPE, () -> ws.sendPing(null)); |
|
219 |
assertThrows(NPE, () -> ws.sendPong(null)); |
|
220 |
assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null)); |
|
56052 | 221 |
} |
222 |
} |
|
223 |
||
56263 | 224 |
// TODO: request in onClose/onError |
225 |
// TODO: throw exception in onClose/onError |
|
226 |
// TODO: exception is thrown from request() |
|
227 |
||
56052 | 228 |
@Test |
56263 | 229 |
public void sendCloseCompleted() throws IOException { |
56052 | 230 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
231 |
server.open(); |
|
232 |
WebSocket ws = newHttpClient() |
|
233 |
.newWebSocketBuilder() |
|
234 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
235 |
.join(); |
|
236 |
ws.sendClose(NORMAL_CLOSURE, "").join(); |
|
237 |
assertTrue(ws.isOutputClosed()); |
|
238 |
assertEquals(ws.getSubprotocol(), ""); |
|
239 |
ws.request(1); // No exceptions must be thrown |
|
240 |
} |
|
241 |
} |
|
242 |
||
243 |
@Test |
|
56263 | 244 |
public void sendClosePending() throws Exception { |
56052 | 245 |
try (DummyWebSocketServer server = notReadingServer()) { |
246 |
server.open(); |
|
247 |
WebSocket ws = newHttpClient() |
|
248 |
.newWebSocketBuilder() |
|
249 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
250 |
.join(); |
|
251 |
ByteBuffer data = ByteBuffer.allocate(65536); |
|
56263 | 252 |
for (int i = 0; ; i++) { // fill up the send buffer |
56052 | 253 |
System.out.println("cycle #" + i); |
254 |
try { |
|
255 |
ws.sendBinary(data, true).get(10, TimeUnit.SECONDS); |
|
256 |
data.clear(); |
|
257 |
} catch (TimeoutException e) { |
|
258 |
break; |
|
259 |
} |
|
260 |
} |
|
261 |
CompletableFuture<WebSocket> cf = ws.sendClose(NORMAL_CLOSURE, ""); |
|
56263 | 262 |
// The output closes even if the Close message has not been sent |
263 |
assertFalse(cf.isDone()); |
|
56052 | 264 |
assertTrue(ws.isOutputClosed()); |
265 |
assertEquals(ws.getSubprotocol(), ""); |
|
266 |
} |
|
267 |
} |
|
268 |
||
269 |
/* |
|
270 |
* This server does not read from the wire, allowing its client to fill up |
|
271 |
* their send buffer. Used to test scenarios with outstanding send |
|
272 |
* operations. |
|
273 |
*/ |
|
274 |
private static DummyWebSocketServer notReadingServer() { |
|
275 |
return new DummyWebSocketServer() { |
|
276 |
@Override |
|
277 |
protected void serve(SocketChannel channel) throws IOException { |
|
278 |
try { |
|
279 |
Thread.sleep(Long.MAX_VALUE); |
|
280 |
} catch (InterruptedException e) { |
|
281 |
throw new IOException(e); |
|
282 |
} |
|
283 |
} |
|
284 |
}; |
|
285 |
} |
|
286 |
||
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
287 |
@Test |
56263 | 288 |
public void abortPendingSendBinary() throws Exception { |
289 |
try (DummyWebSocketServer server = notReadingServer()) { |
|
290 |
server.open(); |
|
291 |
WebSocket ws = newHttpClient() |
|
292 |
.newWebSocketBuilder() |
|
293 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
294 |
.join(); |
|
295 |
ByteBuffer data = ByteBuffer.allocate(65536); |
|
296 |
CompletableFuture<WebSocket> cf = null; |
|
297 |
for (int i = 0; ; i++) { // fill up the send buffer |
|
298 |
System.out.println("cycle #" + i); |
|
299 |
try { |
|
300 |
cf = ws.sendBinary(data, true); |
|
301 |
cf.get(10, TimeUnit.SECONDS); |
|
302 |
data.clear(); |
|
303 |
} catch (TimeoutException e) { |
|
304 |
break; |
|
305 |
} |
|
306 |
} |
|
307 |
ws.abort(); |
|
308 |
assertTrue(ws.isOutputClosed()); |
|
309 |
assertTrue(ws.isInputClosed()); |
|
310 |
assertCompletesExceptionally(IOException.class, cf); |
|
311 |
} |
|
312 |
} |
|
313 |
||
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
314 |
@Test |
56263 | 315 |
public void abortPendingSendText() throws Exception { |
316 |
try (DummyWebSocketServer server = notReadingServer()) { |
|
317 |
server.open(); |
|
318 |
WebSocket ws = newHttpClient() |
|
319 |
.newWebSocketBuilder() |
|
320 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
321 |
.join(); |
|
322 |
String data = stringWith2NBytes(32768); |
|
323 |
CompletableFuture<WebSocket> cf = null; |
|
324 |
for (int i = 0; ; i++) { // fill up the send buffer |
|
325 |
System.out.println("cycle #" + i); |
|
326 |
try { |
|
327 |
cf = ws.sendText(data, true); |
|
328 |
cf.get(10, TimeUnit.SECONDS); |
|
329 |
} catch (TimeoutException e) { |
|
330 |
break; |
|
331 |
} |
|
332 |
} |
|
333 |
ws.abort(); |
|
334 |
assertTrue(ws.isOutputClosed()); |
|
335 |
assertTrue(ws.isInputClosed()); |
|
336 |
assertCompletesExceptionally(IOException.class, cf); |
|
337 |
} |
|
338 |
} |
|
339 |
||
340 |
private static String stringWith2NBytes(int n) { |
|
341 |
// -- Russian Alphabet (33 characters, 2 bytes per char) -- |
|
342 |
char[] abc = { |
|
343 |
0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0401, 0x0416, |
|
344 |
0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, |
|
345 |
0x041F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, |
|
346 |
0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, |
|
347 |
0x042F, |
|
348 |
}; |
|
349 |
// repeat cyclically |
|
350 |
StringBuilder sb = new StringBuilder(n); |
|
351 |
for (int i = 0, j = 0; i < n; i++, j = (j + 1) % abc.length) { |
|
352 |
sb.append(abc[j]); |
|
353 |
} |
|
354 |
String s = sb.toString(); |
|
355 |
assert s.length() == n && s.getBytes(StandardCharsets.UTF_8).length == 2 * n; |
|
356 |
return s; |
|
357 |
} |
|
358 |
||
56052 | 359 |
@Test |
360 |
public void testIllegalArgument() throws IOException { |
|
361 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
|
362 |
server.open(); |
|
363 |
WebSocket ws = newHttpClient() |
|
364 |
.newWebSocketBuilder() |
|
365 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
366 |
.join(); |
|
367 |
||
368 |
assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(126))); |
|
369 |
assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(127))); |
|
370 |
assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(128))); |
|
371 |
assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(129))); |
|
372 |
assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(256))); |
|
373 |
||
374 |
assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(126))); |
|
375 |
assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(127))); |
|
376 |
assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(128))); |
|
377 |
assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(129))); |
|
378 |
assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(256))); |
|
379 |
||
56263 | 380 |
assertCompletesExceptionally(IOE, ws.sendText(incompleteString(), true)); |
381 |
assertCompletesExceptionally(IOE, ws.sendText(incompleteString(), false)); |
|
382 |
assertCompletesExceptionally(IOE, ws.sendText(malformedString(), true)); |
|
383 |
assertCompletesExceptionally(IOE, ws.sendText(malformedString(), false)); |
|
56052 | 384 |
|
385 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(124))); |
|
386 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(125))); |
|
387 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(128))); |
|
388 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(256))); |
|
389 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(257))); |
|
390 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWith2NBytes((123 / 2) + 1))); |
|
391 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, malformedString())); |
|
392 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, incompleteString())); |
|
393 |
||
394 |
assertCompletesExceptionally(IAE, ws.sendClose(-2, "a reason")); |
|
395 |
assertCompletesExceptionally(IAE, ws.sendClose(-1, "a reason")); |
|
396 |
assertCompletesExceptionally(IAE, ws.sendClose(0, "a reason")); |
|
397 |
assertCompletesExceptionally(IAE, ws.sendClose(1, "a reason")); |
|
398 |
assertCompletesExceptionally(IAE, ws.sendClose(500, "a reason")); |
|
399 |
assertCompletesExceptionally(IAE, ws.sendClose(998, "a reason")); |
|
400 |
assertCompletesExceptionally(IAE, ws.sendClose(999, "a reason")); |
|
401 |
assertCompletesExceptionally(IAE, ws.sendClose(1002, "a reason")); |
|
402 |
assertCompletesExceptionally(IAE, ws.sendClose(1003, "a reason")); |
|
403 |
assertCompletesExceptionally(IAE, ws.sendClose(1006, "a reason")); |
|
404 |
assertCompletesExceptionally(IAE, ws.sendClose(1007, "a reason")); |
|
405 |
assertCompletesExceptionally(IAE, ws.sendClose(1009, "a reason")); |
|
406 |
assertCompletesExceptionally(IAE, ws.sendClose(1010, "a reason")); |
|
407 |
assertCompletesExceptionally(IAE, ws.sendClose(1012, "a reason")); |
|
408 |
assertCompletesExceptionally(IAE, ws.sendClose(1013, "a reason")); |
|
409 |
assertCompletesExceptionally(IAE, ws.sendClose(1015, "a reason")); |
|
410 |
assertCompletesExceptionally(IAE, ws.sendClose(5000, "a reason")); |
|
411 |
assertCompletesExceptionally(IAE, ws.sendClose(32768, "a reason")); |
|
412 |
assertCompletesExceptionally(IAE, ws.sendClose(65535, "a reason")); |
|
413 |
assertCompletesExceptionally(IAE, ws.sendClose(65536, "a reason")); |
|
414 |
assertCompletesExceptionally(IAE, ws.sendClose(Integer.MAX_VALUE, "a reason")); |
|
415 |
assertCompletesExceptionally(IAE, ws.sendClose(Integer.MIN_VALUE, "a reason")); |
|
416 |
||
417 |
assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE)); |
|
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
418 |
assertThrows(IAE, () -> ws.request(Long.MIN_VALUE)); |
56052 | 419 |
assertThrows(IAE, () -> ws.request(-1)); |
420 |
assertThrows(IAE, () -> ws.request(0)); |
|
421 |
} |
|
422 |
} |
|
423 |
||
424 |
private static String malformedString() { |
|
425 |
return new String(new char[]{0xDC00, 0xD800}); |
|
426 |
} |
|
427 |
||
428 |
private static String incompleteString() { |
|
429 |
return new String(new char[]{0xD800}); |
|
430 |
} |
|
431 |
||
432 |
private static String stringWithNBytes(int n) { |
|
56263 | 433 |
char[] chars = new char[n]; |
434 |
Arrays.fill(chars, 'A'); |
|
435 |
return new String(chars); |
|
56052 | 436 |
} |
437 |
||
438 |
@Test |
|
56263 | 439 |
public void outstanding1() throws Exception { |
56052 | 440 |
try (DummyWebSocketServer server = notReadingServer()) { |
441 |
server.open(); |
|
442 |
WebSocket ws = newHttpClient() |
|
443 |
.newWebSocketBuilder() |
|
444 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
445 |
.join(); |
|
446 |
||
447 |
ByteBuffer data = ByteBuffer.allocate(65536); |
|
56263 | 448 |
for (int i = 0; ; i++) { // fill up the send buffer |
56052 | 449 |
System.out.println("cycle #" + i); |
450 |
try { |
|
451 |
ws.sendBinary(data, true).get(10, TimeUnit.SECONDS); |
|
452 |
data.clear(); |
|
453 |
} catch (TimeoutException e) { |
|
454 |
break; |
|
455 |
} |
|
456 |
} |
|
457 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
|
458 |
assertCompletesExceptionally(ISE, ws.sendText("", true)); |
|
459 |
} |
|
460 |
} |
|
461 |
||
462 |
@Test |
|
56263 | 463 |
public void outstanding2() throws Exception { |
56052 | 464 |
try (DummyWebSocketServer server = notReadingServer()) { |
465 |
server.open(); |
|
466 |
WebSocket ws = newHttpClient() |
|
467 |
.newWebSocketBuilder() |
|
468 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
469 |
.join(); |
|
470 |
||
471 |
CharBuffer data = CharBuffer.allocate(65536); |
|
56263 | 472 |
for (int i = 0; ; i++) { // fill up the send buffer |
56052 | 473 |
System.out.println("cycle #" + i); |
474 |
try { |
|
475 |
ws.sendText(data, true).get(10, TimeUnit.SECONDS); |
|
476 |
data.clear(); |
|
477 |
} catch (TimeoutException e) { |
|
478 |
break; |
|
479 |
} |
|
480 |
} |
|
481 |
assertCompletesExceptionally(ISE, ws.sendText("", true)); |
|
482 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
|
483 |
} |
|
484 |
} |
|
485 |
||
486 |
@Test |
|
56263 | 487 |
public void interleavingTypes1() throws IOException { |
56052 | 488 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
489 |
server.open(); |
|
490 |
WebSocket ws = newHttpClient() |
|
491 |
.newWebSocketBuilder() |
|
492 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
493 |
.join(); |
|
494 |
||
495 |
ws.sendBinary(ByteBuffer.allocate(16), false).join(); |
|
496 |
assertCompletesExceptionally(ISE, ws.sendText("text", false)); |
|
497 |
assertCompletesExceptionally(ISE, ws.sendText("text", true)); |
|
498 |
} |
|
499 |
} |
|
500 |
||
501 |
@Test |
|
56263 | 502 |
public void interleavingTypes2() throws IOException { |
56052 | 503 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
504 |
server.open(); |
|
505 |
WebSocket ws = newHttpClient() |
|
506 |
.newWebSocketBuilder() |
|
507 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
508 |
.join(); |
|
509 |
||
510 |
ws.sendText("text", false).join(); |
|
511 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(16), false)); |
|
512 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(16), true)); |
|
513 |
} |
|
514 |
} |
|
515 |
||
516 |
@Test |
|
56263 | 517 |
public void sendMethodsThrowIOE1() throws IOException { |
56052 | 518 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
519 |
server.open(); |
|
520 |
WebSocket ws = newHttpClient() |
|
521 |
.newWebSocketBuilder() |
|
522 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
523 |
.join(); |
|
524 |
||
56263 | 525 |
ws.sendClose(NORMAL_CLOSURE, "ok").join(); |
526 |
||
527 |
assertCompletesExceptionally(IOE, ws.sendClose(WebSocket.NORMAL_CLOSURE, "ok")); |
|
56052 | 528 |
|
56263 | 529 |
assertCompletesExceptionally(IOE, ws.sendText("", true)); |
530 |
assertCompletesExceptionally(IOE, ws.sendText("", false)); |
|
531 |
assertCompletesExceptionally(IOE, ws.sendText("abc", true)); |
|
532 |
assertCompletesExceptionally(IOE, ws.sendText("abc", false)); |
|
533 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
|
534 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(0), false)); |
|
535 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(1), true)); |
|
536 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(1), false)); |
|
56052 | 537 |
|
56263 | 538 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(125))); |
539 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(124))); |
|
540 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(1))); |
|
541 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(0))); |
|
56052 | 542 |
|
56263 | 543 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(125))); |
544 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(124))); |
|
545 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(1))); |
|
546 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(0))); |
|
56052 | 547 |
} |
548 |
} |
|
549 |
||
550 |
@Test |
|
56263 | 551 |
public void sendMethodsThrowIOE2() throws Exception { |
56057 | 552 |
try (DummyWebSocketServer server = serverWithCannedData(0x88, 0x00)) { |
56052 | 553 |
server.open(); |
554 |
CompletableFuture<Void> onCloseCalled = new CompletableFuture<>(); |
|
555 |
CompletableFuture<Void> canClose = new CompletableFuture<>(); |
|
556 |
||
557 |
WebSocket ws = newHttpClient() |
|
558 |
.newWebSocketBuilder() |
|
559 |
.buildAsync(server.getURI(), new WebSocket.Listener() { |
|
560 |
@Override |
|
561 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
562 |
int statusCode, |
|
563 |
String reason) { |
|
56263 | 564 |
System.out.printf("onClose(%s, '%s')%n", statusCode, reason); |
56052 | 565 |
onCloseCalled.complete(null); |
566 |
return canClose; |
|
567 |
} |
|
568 |
||
569 |
@Override |
|
570 |
public void onError(WebSocket webSocket, Throwable error) { |
|
571 |
System.out.println("onError(" + error + ")"); |
|
56263 | 572 |
onCloseCalled.completeExceptionally(error); |
56052 | 573 |
} |
574 |
}) |
|
575 |
.join(); |
|
576 |
||
577 |
onCloseCalled.join(); // Wait for onClose to be called |
|
56263 | 578 |
canClose.complete(null); // Signal to the WebSocket it can close the output |
56052 | 579 |
TimeUnit.SECONDS.sleep(5); // Give canClose some time to reach the WebSocket |
56263 | 580 |
|
581 |
assertCompletesExceptionally(IOE, ws.sendClose(WebSocket.NORMAL_CLOSURE, "ok")); |
|
56052 | 582 |
|
56263 | 583 |
assertCompletesExceptionally(IOE, ws.sendText("", true)); |
584 |
assertCompletesExceptionally(IOE, ws.sendText("", false)); |
|
585 |
assertCompletesExceptionally(IOE, ws.sendText("abc", true)); |
|
586 |
assertCompletesExceptionally(IOE, ws.sendText("abc", false)); |
|
587 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
|
588 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(0), false)); |
|
589 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(1), true)); |
|
590 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(1), false)); |
|
56052 | 591 |
|
56263 | 592 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(125))); |
593 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(124))); |
|
594 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(1))); |
|
595 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(0))); |
|
56052 | 596 |
|
56263 | 597 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(125))); |
598 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(124))); |
|
599 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(1))); |
|
600 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(0))); |
|
56052 | 601 |
} |
602 |
} |
|
56058 | 603 |
|
604 |
@Test |
|
56263 | 605 |
public void simpleAggregatingBinaryMessages() throws IOException { |
606 |
List<byte[]> expected = List.of("alpha", "beta", "gamma", "delta") |
|
607 |
.stream() |
|
608 |
.map(s -> s.getBytes(StandardCharsets.US_ASCII)) |
|
609 |
.collect(Collectors.toList()); |
|
610 |
int[] binary = new int[]{ |
|
611 |
0x82, 0x05, 0x61, 0x6c, 0x70, 0x68, 0x61, // [alpha] |
|
612 |
0x02, 0x02, 0x62, 0x65, // [be |
|
613 |
0x80, 0x02, 0x74, 0x61, // ta] |
|
614 |
0x02, 0x01, 0x67, // [g |
|
615 |
0x00, 0x01, 0x61, // a |
|
616 |
0x00, 0x00, // |
|
617 |
0x00, 0x00, // |
|
618 |
0x00, 0x01, 0x6d, // m |
|
619 |
0x00, 0x01, 0x6d, // m |
|
620 |
0x80, 0x01, 0x61, // a] |
|
621 |
0x8a, 0x00, // <PONG> |
|
622 |
0x02, 0x04, 0x64, 0x65, 0x6c, 0x74, // [delt |
|
623 |
0x00, 0x01, 0x61, // a |
|
624 |
0x80, 0x00, // ] |
|
625 |
0x88, 0x00 // <CLOSE> |
|
626 |
}; |
|
627 |
CompletableFuture<List<byte[]>> actual = new CompletableFuture<>(); |
|
628 |
||
629 |
try (DummyWebSocketServer server = serverWithCannedData(binary)) { |
|
630 |
server.open(); |
|
631 |
||
632 |
WebSocket.Listener listener = new WebSocket.Listener() { |
|
633 |
||
634 |
List<byte[]> collectedBytes = new ArrayList<>(); |
|
635 |
ByteBuffer binary; |
|
636 |
||
637 |
@Override |
|
638 |
public CompletionStage<?> onBinary(WebSocket webSocket, |
|
639 |
ByteBuffer message, |
|
640 |
WebSocket.MessagePart part) { |
|
641 |
System.out.printf("onBinary(%s, %s)%n", message, part); |
|
642 |
webSocket.request(1); |
|
643 |
byte[] bytes = null; |
|
644 |
switch (part) { |
|
645 |
case FIRST: |
|
646 |
binary = ByteBuffer.allocate(message.remaining() * 2); |
|
647 |
case PART: |
|
648 |
append(message); |
|
649 |
return null; |
|
650 |
case LAST: |
|
651 |
append(message); |
|
652 |
binary.flip(); |
|
653 |
bytes = new byte[binary.remaining()]; |
|
654 |
binary.get(bytes); |
|
655 |
binary.clear(); |
|
656 |
break; |
|
657 |
case WHOLE: |
|
658 |
bytes = new byte[message.remaining()]; |
|
659 |
message.get(bytes); |
|
660 |
break; |
|
661 |
} |
|
662 |
processWholeBinary(bytes); |
|
663 |
return null; |
|
664 |
} |
|
665 |
||
666 |
private void append(ByteBuffer message) { |
|
667 |
if (binary.remaining() < message.remaining()) { |
|
668 |
assert message.remaining() > 0; |
|
669 |
int cap = (binary.capacity() + message.remaining()) * 2; |
|
670 |
ByteBuffer b = ByteBuffer.allocate(cap); |
|
671 |
b.put(binary.flip()); |
|
672 |
binary = b; |
|
673 |
} |
|
674 |
binary.put(message); |
|
675 |
} |
|
676 |
||
677 |
private void processWholeBinary(byte[] bytes) { |
|
678 |
String stringBytes = new String(bytes, StandardCharsets.UTF_8); |
|
679 |
System.out.println("processWholeBinary: " + stringBytes); |
|
680 |
collectedBytes.add(bytes); |
|
681 |
} |
|
682 |
||
683 |
@Override |
|
684 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
685 |
int statusCode, |
|
686 |
String reason) { |
|
687 |
actual.complete(collectedBytes); |
|
688 |
return null; |
|
689 |
} |
|
690 |
||
691 |
@Override |
|
692 |
public void onError(WebSocket webSocket, Throwable error) { |
|
693 |
actual.completeExceptionally(error); |
|
694 |
} |
|
695 |
}; |
|
696 |
||
697 |
newHttpClient().newWebSocketBuilder() |
|
698 |
.buildAsync(server.getURI(), listener) |
|
699 |
.join(); |
|
700 |
||
701 |
List<byte[]> a = actual.join(); |
|
702 |
System.out.println("joined"); |
|
703 |
assertEquals(a, expected); |
|
704 |
} |
|
705 |
} |
|
706 |
||
707 |
@Test |
|
708 |
public void simpleAggregatingTextMessages() throws IOException { |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
709 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
710 |
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
|
711 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
712 |
int[] binary = new int[]{ |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
713 |
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
|
714 |
0x01, 0x02, 0x62, 0x65, // "be |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
715 |
0x80, 0x02, 0x74, 0x61, // ta" |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
716 |
0x01, 0x01, 0x67, // "g |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
717 |
0x00, 0x01, 0x61, // a |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
718 |
0x00, 0x00, // |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
719 |
0x00, 0x00, // |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
720 |
0x00, 0x01, 0x6d, // m |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
721 |
0x00, 0x01, 0x6d, // m |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
722 |
0x80, 0x01, 0x61, // a" |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
723 |
0x8a, 0x00, // <PONG> |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
724 |
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
|
725 |
0x00, 0x01, 0x61, // a |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
726 |
0x80, 0x00, // " |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
727 |
0x88, 0x00 // <CLOSE> |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
728 |
}; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
729 |
CompletableFuture<List<String>> actual = new CompletableFuture<>(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
730 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
731 |
try (DummyWebSocketServer server = serverWithCannedData(binary)) { |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
732 |
server.open(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
733 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
734 |
WebSocket.Listener listener = new WebSocket.Listener() { |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
735 |
|
56263 | 736 |
List<String> collectedStrings = new ArrayList<>(); |
737 |
StringBuilder text; |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
738 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
739 |
@Override |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
740 |
public CompletionStage<?> onText(WebSocket webSocket, |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
741 |
CharSequence message, |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
742 |
WebSocket.MessagePart part) { |
56263 | 743 |
System.out.printf("onText(%s, %s)%n", message, part); |
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
744 |
webSocket.request(1); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
745 |
String str = null; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
746 |
switch (part) { |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
747 |
case FIRST: |
56263 | 748 |
text = new StringBuilder(message.length() * 2); |
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
749 |
case PART: |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
750 |
text.append(message); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
751 |
return null; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
752 |
case LAST: |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
753 |
text.append(message); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
754 |
str = text.toString(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
755 |
break; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
756 |
case WHOLE: |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
757 |
str = message.toString(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
758 |
break; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
759 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
760 |
processWholeText(str); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
761 |
return null; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
762 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
763 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
764 |
private void processWholeText(String string) { |
56130
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
765 |
System.out.println(string); |
56263 | 766 |
collectedStrings.add(string); |
56130
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
767 |
} |
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
768 |
|
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
769 |
@Override |
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
770 |
public CompletionStage<?> onClose(WebSocket webSocket, |
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
771 |
int statusCode, |
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
772 |
String reason) { |
56263 | 773 |
actual.complete(collectedStrings); |
56130
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
774 |
return null; |
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
775 |
} |
56263 | 776 |
|
777 |
@Override |
|
778 |
public void onError(WebSocket webSocket, Throwable error) { |
|
779 |
actual.completeExceptionally(error); |
|
780 |
} |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
781 |
}; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
782 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
783 |
newHttpClient().newWebSocketBuilder() |
56263 | 784 |
.buildAsync(server.getURI(), listener) |
785 |
.join(); |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
786 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
787 |
List<String> a = actual.join(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
788 |
assertEquals(a, expected); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
789 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
790 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
791 |
|
56263 | 792 |
/* |
793 |
* Exercises the scenario where requests for more messages are made prior to |
|
794 |
* completing the returned CompletionStage instances. |
|
795 |
*/ |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
796 |
@Test |
56263 | 797 |
public void aggregatingTextMessages() throws IOException { |
56058 | 798 |
|
799 |
List<String> expected = List.of("alpha", "beta", "gamma", "delta"); |
|
800 |
||
801 |
int[] binary = new int[]{ |
|
802 |
0x81, 0x05, 0x61, 0x6c, 0x70, 0x68, 0x61, // "alpha" |
|
803 |
0x01, 0x02, 0x62, 0x65, // "be |
|
804 |
0x80, 0x02, 0x74, 0x61, // ta" |
|
805 |
0x01, 0x01, 0x67, // "g |
|
806 |
0x00, 0x01, 0x61, // a |
|
807 |
0x00, 0x00, // |
|
808 |
0x00, 0x00, // |
|
809 |
0x00, 0x01, 0x6d, // m |
|
810 |
0x00, 0x01, 0x6d, // m |
|
811 |
0x80, 0x01, 0x61, // a" |
|
812 |
0x8a, 0x00, // <PONG> |
|
813 |
0x01, 0x04, 0x64, 0x65, 0x6c, 0x74, // "delt |
|
814 |
0x00, 0x01, 0x61, // a |
|
815 |
0x80, 0x00, // " |
|
816 |
0x88, 0x00 // <CLOSE> |
|
817 |
}; |
|
818 |
CompletableFuture<List<String>> actual = new CompletableFuture<>(); |
|
819 |
||
820 |
||
821 |
try (DummyWebSocketServer server = serverWithCannedData(binary)) { |
|
822 |
server.open(); |
|
823 |
||
824 |
WebSocket.Listener listener = new WebSocket.Listener() { |
|
825 |
||
56263 | 826 |
List<CharSequence> parts; |
827 |
/* |
|
828 |
* A CompletableFuture which will complete once the current |
|
829 |
* message has been fully assembled (LAST/WHOLE). Until then |
|
830 |
* the listener returns this instance for every call. |
|
831 |
*/ |
|
56058 | 832 |
CompletableFuture<?> currentCf; |
833 |
List<String> collected = new ArrayList<>(); |
|
834 |
||
835 |
@Override |
|
836 |
public CompletionStage<?> onText(WebSocket webSocket, |
|
837 |
CharSequence message, |
|
838 |
WebSocket.MessagePart part) { |
|
839 |
switch (part) { |
|
840 |
case WHOLE: |
|
841 |
CompletableFuture<?> cf = new CompletableFuture<>(); |
|
842 |
cf.thenRun(() -> webSocket.request(1)); |
|
843 |
processWholeMessage(List.of(message), cf); |
|
844 |
return cf; |
|
845 |
case FIRST: |
|
56263 | 846 |
parts = new ArrayList<>(); |
56058 | 847 |
parts.add(message); |
848 |
currentCf = new CompletableFuture<>(); |
|
849 |
currentCf.thenRun(() -> webSocket.request(1)); |
|
850 |
webSocket.request(1); |
|
851 |
break; |
|
852 |
case PART: |
|
853 |
parts.add(message); |
|
854 |
webSocket.request(1); |
|
855 |
break; |
|
856 |
case LAST: |
|
857 |
parts.add(message); |
|
56263 | 858 |
CompletableFuture<?> copyCf = this.currentCf; |
859 |
processWholeMessage(parts, copyCf); |
|
56058 | 860 |
currentCf = null; |
56263 | 861 |
parts = null; |
862 |
return copyCf; |
|
56058 | 863 |
} |
864 |
return currentCf; |
|
865 |
} |
|
866 |
||
867 |
@Override |
|
868 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
869 |
int statusCode, |
|
870 |
String reason) { |
|
871 |
actual.complete(collected); |
|
872 |
return null; |
|
873 |
} |
|
874 |
||
56263 | 875 |
@Override |
876 |
public void onError(WebSocket webSocket, Throwable error) { |
|
877 |
actual.completeExceptionally(error); |
|
878 |
} |
|
879 |
||
56058 | 880 |
public void processWholeMessage(List<CharSequence> data, |
881 |
CompletableFuture<?> cf) { |
|
882 |
StringBuilder b = new StringBuilder(); |
|
883 |
data.forEach(b::append); |
|
884 |
String s = b.toString(); |
|
885 |
System.out.println(s); |
|
886 |
cf.complete(null); |
|
887 |
collected.add(s); |
|
888 |
} |
|
889 |
}; |
|
56263 | 890 |
|
891 |
newHttpClient().newWebSocketBuilder() |
|
892 |
.buildAsync(server.getURI(), listener) |
|
893 |
.join(); |
|
56058 | 894 |
|
895 |
List<String> a = actual.join(); |
|
896 |
assertEquals(a, expected); |
|
897 |
} |
|
898 |
} |
|
56052 | 899 |
} |