author | prappo |
Tue, 13 Mar 2018 17:10:20 +0000 | |
branch | http-client-branch |
changeset 56294 | 181bc33917e4 |
parent 56269 | 234813fd33bc |
child 56303 | a82058c084ef |
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(); |
|
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
251 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
252 |
ByteBuffer data = ByteBuffer.allocate(65536); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
253 |
for (int i = 0; ; i++) { // fill up the send buffer |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
254 |
System.out.printf("begin cycle #%s at %s%n", |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
255 |
i, System.currentTimeMillis()); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
256 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
257 |
ws.sendBinary(data, true).get(10, TimeUnit.SECONDS); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
258 |
data.clear(); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
259 |
} catch (TimeoutException e) { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
260 |
break; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
261 |
} finally { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
262 |
System.out.printf("end cycle #%s at %s%n", |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
263 |
i, System.currentTimeMillis()); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
264 |
} |
56052 | 265 |
} |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
266 |
CompletableFuture<WebSocket> cf = ws.sendClose(NORMAL_CLOSURE, ""); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
267 |
// The output closes even if the Close message has not been sent |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
268 |
assertFalse(cf.isDone()); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
269 |
assertTrue(ws.isOutputClosed()); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
270 |
assertEquals(ws.getSubprotocol(), ""); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
271 |
} finally { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
272 |
ws.abort(); |
56052 | 273 |
} |
274 |
} |
|
275 |
} |
|
276 |
||
277 |
/* |
|
278 |
* This server does not read from the wire, allowing its client to fill up |
|
279 |
* their send buffer. Used to test scenarios with outstanding send |
|
280 |
* operations. |
|
281 |
*/ |
|
282 |
private static DummyWebSocketServer notReadingServer() { |
|
283 |
return new DummyWebSocketServer() { |
|
284 |
@Override |
|
285 |
protected void serve(SocketChannel channel) throws IOException { |
|
286 |
try { |
|
287 |
Thread.sleep(Long.MAX_VALUE); |
|
288 |
} catch (InterruptedException e) { |
|
289 |
throw new IOException(e); |
|
290 |
} |
|
291 |
} |
|
292 |
}; |
|
293 |
} |
|
294 |
||
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
295 |
@Test |
56263 | 296 |
public void abortPendingSendBinary() throws Exception { |
297 |
try (DummyWebSocketServer server = notReadingServer()) { |
|
298 |
server.open(); |
|
299 |
WebSocket ws = newHttpClient() |
|
300 |
.newWebSocketBuilder() |
|
301 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
302 |
.join(); |
|
303 |
ByteBuffer data = ByteBuffer.allocate(65536); |
|
304 |
CompletableFuture<WebSocket> cf = null; |
|
305 |
for (int i = 0; ; i++) { // fill up the send buffer |
|
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
306 |
System.out.printf("begin cycle #%s at %s%n", |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
307 |
i, System.currentTimeMillis()); |
56263 | 308 |
try { |
309 |
cf = ws.sendBinary(data, true); |
|
310 |
cf.get(10, TimeUnit.SECONDS); |
|
311 |
data.clear(); |
|
312 |
} catch (TimeoutException e) { |
|
313 |
break; |
|
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
314 |
} finally { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
315 |
System.out.printf("end cycle #%s at %s%n", |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
316 |
i, System.currentTimeMillis()); |
56263 | 317 |
} |
318 |
} |
|
319 |
ws.abort(); |
|
320 |
assertTrue(ws.isOutputClosed()); |
|
321 |
assertTrue(ws.isInputClosed()); |
|
322 |
assertCompletesExceptionally(IOException.class, cf); |
|
323 |
} |
|
324 |
} |
|
325 |
||
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
326 |
@Test |
56263 | 327 |
public void abortPendingSendText() throws Exception { |
328 |
try (DummyWebSocketServer server = notReadingServer()) { |
|
329 |
server.open(); |
|
330 |
WebSocket ws = newHttpClient() |
|
331 |
.newWebSocketBuilder() |
|
332 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
333 |
.join(); |
|
334 |
String data = stringWith2NBytes(32768); |
|
335 |
CompletableFuture<WebSocket> cf = null; |
|
336 |
for (int i = 0; ; i++) { // fill up the send buffer |
|
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
337 |
System.out.printf("begin cycle #%s at %s%n", |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
338 |
i, System.currentTimeMillis()); |
56263 | 339 |
try { |
340 |
cf = ws.sendText(data, true); |
|
341 |
cf.get(10, TimeUnit.SECONDS); |
|
342 |
} catch (TimeoutException e) { |
|
343 |
break; |
|
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
344 |
} finally { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
345 |
System.out.printf("end cycle #%s at %s%n", |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
346 |
i, System.currentTimeMillis()); |
56263 | 347 |
} |
348 |
} |
|
349 |
ws.abort(); |
|
350 |
assertTrue(ws.isOutputClosed()); |
|
351 |
assertTrue(ws.isInputClosed()); |
|
352 |
assertCompletesExceptionally(IOException.class, cf); |
|
353 |
} |
|
354 |
} |
|
355 |
||
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
356 |
@Test |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
357 |
public void sendCloseTimeout() throws Exception { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
358 |
try (DummyWebSocketServer server = notReadingServer()) { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
359 |
server.open(); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
360 |
WebSocket ws = newHttpClient() |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
361 |
.newWebSocketBuilder() |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
362 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
363 |
.join(); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
364 |
String data = stringWith2NBytes(32768); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
365 |
CompletableFuture<WebSocket> cf = null; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
366 |
for (int i = 0; ; i++) { // fill up the send buffer |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
367 |
System.out.printf("begin cycle #%s at %s%n", |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
368 |
i, System.currentTimeMillis()); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
369 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
370 |
cf = ws.sendText(data, true); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
371 |
cf.get(10, TimeUnit.SECONDS); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
372 |
} catch (TimeoutException e) { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
373 |
break; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
374 |
} finally { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
375 |
System.out.printf("end cycle #%s at %s%n", |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
376 |
i, System.currentTimeMillis()); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
377 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
378 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
379 |
long before = System.currentTimeMillis(); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
380 |
assertCompletesExceptionally(IOException.class, |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
381 |
ws.sendClose(WebSocket.NORMAL_CLOSURE, "ok")); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
382 |
long after = System.currentTimeMillis(); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
383 |
// default timeout should be 30 seconds |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
384 |
long elapsed = after - before; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
385 |
System.out.printf("Elapsed %s ms%n", elapsed); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
386 |
assertTrue(elapsed >= 29_000, String.valueOf(elapsed)); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
387 |
assertTrue(ws.isOutputClosed()); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
388 |
assertTrue(ws.isInputClosed()); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
389 |
assertCompletesExceptionally(IOException.class, cf); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
390 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
391 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
392 |
|
56263 | 393 |
private static String stringWith2NBytes(int n) { |
394 |
// -- Russian Alphabet (33 characters, 2 bytes per char) -- |
|
395 |
char[] abc = { |
|
396 |
0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0401, 0x0416, |
|
397 |
0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, |
|
398 |
0x041F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, |
|
399 |
0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, |
|
400 |
0x042F, |
|
401 |
}; |
|
402 |
// repeat cyclically |
|
403 |
StringBuilder sb = new StringBuilder(n); |
|
404 |
for (int i = 0, j = 0; i < n; i++, j = (j + 1) % abc.length) { |
|
405 |
sb.append(abc[j]); |
|
406 |
} |
|
407 |
String s = sb.toString(); |
|
408 |
assert s.length() == n && s.getBytes(StandardCharsets.UTF_8).length == 2 * n; |
|
409 |
return s; |
|
410 |
} |
|
411 |
||
56052 | 412 |
@Test |
413 |
public void testIllegalArgument() throws IOException { |
|
414 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
|
415 |
server.open(); |
|
416 |
WebSocket ws = newHttpClient() |
|
417 |
.newWebSocketBuilder() |
|
418 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
419 |
.join(); |
|
420 |
||
421 |
assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(126))); |
|
422 |
assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(127))); |
|
423 |
assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(128))); |
|
424 |
assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(129))); |
|
425 |
assertCompletesExceptionally(IAE, ws.sendPing(ByteBuffer.allocate(256))); |
|
426 |
||
427 |
assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(126))); |
|
428 |
assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(127))); |
|
429 |
assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(128))); |
|
430 |
assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(129))); |
|
431 |
assertCompletesExceptionally(IAE, ws.sendPong(ByteBuffer.allocate(256))); |
|
432 |
||
56263 | 433 |
assertCompletesExceptionally(IOE, ws.sendText(incompleteString(), true)); |
434 |
assertCompletesExceptionally(IOE, ws.sendText(incompleteString(), false)); |
|
435 |
assertCompletesExceptionally(IOE, ws.sendText(malformedString(), true)); |
|
436 |
assertCompletesExceptionally(IOE, ws.sendText(malformedString(), false)); |
|
56052 | 437 |
|
438 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(124))); |
|
439 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(125))); |
|
440 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(128))); |
|
441 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(256))); |
|
442 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(257))); |
|
443 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, stringWith2NBytes((123 / 2) + 1))); |
|
444 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, malformedString())); |
|
445 |
assertCompletesExceptionally(IAE, ws.sendClose(NORMAL_CLOSURE, incompleteString())); |
|
446 |
||
447 |
assertCompletesExceptionally(IAE, ws.sendClose(-2, "a reason")); |
|
448 |
assertCompletesExceptionally(IAE, ws.sendClose(-1, "a reason")); |
|
449 |
assertCompletesExceptionally(IAE, ws.sendClose(0, "a reason")); |
|
450 |
assertCompletesExceptionally(IAE, ws.sendClose(1, "a reason")); |
|
451 |
assertCompletesExceptionally(IAE, ws.sendClose(500, "a reason")); |
|
452 |
assertCompletesExceptionally(IAE, ws.sendClose(998, "a reason")); |
|
453 |
assertCompletesExceptionally(IAE, ws.sendClose(999, "a reason")); |
|
454 |
assertCompletesExceptionally(IAE, ws.sendClose(1002, "a reason")); |
|
455 |
assertCompletesExceptionally(IAE, ws.sendClose(1003, "a reason")); |
|
456 |
assertCompletesExceptionally(IAE, ws.sendClose(1006, "a reason")); |
|
457 |
assertCompletesExceptionally(IAE, ws.sendClose(1007, "a reason")); |
|
458 |
assertCompletesExceptionally(IAE, ws.sendClose(1009, "a reason")); |
|
459 |
assertCompletesExceptionally(IAE, ws.sendClose(1010, "a reason")); |
|
460 |
assertCompletesExceptionally(IAE, ws.sendClose(1012, "a reason")); |
|
461 |
assertCompletesExceptionally(IAE, ws.sendClose(1013, "a reason")); |
|
462 |
assertCompletesExceptionally(IAE, ws.sendClose(1015, "a reason")); |
|
463 |
assertCompletesExceptionally(IAE, ws.sendClose(5000, "a reason")); |
|
464 |
assertCompletesExceptionally(IAE, ws.sendClose(32768, "a reason")); |
|
465 |
assertCompletesExceptionally(IAE, ws.sendClose(65535, "a reason")); |
|
466 |
assertCompletesExceptionally(IAE, ws.sendClose(65536, "a reason")); |
|
467 |
assertCompletesExceptionally(IAE, ws.sendClose(Integer.MAX_VALUE, "a reason")); |
|
468 |
assertCompletesExceptionally(IAE, ws.sendClose(Integer.MIN_VALUE, "a reason")); |
|
469 |
||
470 |
assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE)); |
|
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
471 |
assertThrows(IAE, () -> ws.request(Long.MIN_VALUE)); |
56052 | 472 |
assertThrows(IAE, () -> ws.request(-1)); |
473 |
assertThrows(IAE, () -> ws.request(0)); |
|
474 |
} |
|
475 |
} |
|
476 |
||
477 |
private static String malformedString() { |
|
478 |
return new String(new char[]{0xDC00, 0xD800}); |
|
479 |
} |
|
480 |
||
481 |
private static String incompleteString() { |
|
482 |
return new String(new char[]{0xD800}); |
|
483 |
} |
|
484 |
||
485 |
private static String stringWithNBytes(int n) { |
|
56263 | 486 |
char[] chars = new char[n]; |
487 |
Arrays.fill(chars, 'A'); |
|
488 |
return new String(chars); |
|
56052 | 489 |
} |
490 |
||
491 |
@Test |
|
56263 | 492 |
public void outstanding1() throws Exception { |
56052 | 493 |
try (DummyWebSocketServer server = notReadingServer()) { |
494 |
server.open(); |
|
495 |
WebSocket ws = newHttpClient() |
|
496 |
.newWebSocketBuilder() |
|
497 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
498 |
.join(); |
|
499 |
||
500 |
ByteBuffer data = ByteBuffer.allocate(65536); |
|
56263 | 501 |
for (int i = 0; ; i++) { // fill up the send buffer |
56052 | 502 |
System.out.println("cycle #" + i); |
503 |
try { |
|
504 |
ws.sendBinary(data, true).get(10, TimeUnit.SECONDS); |
|
505 |
data.clear(); |
|
506 |
} catch (TimeoutException e) { |
|
507 |
break; |
|
508 |
} |
|
509 |
} |
|
510 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
|
511 |
assertCompletesExceptionally(ISE, ws.sendText("", true)); |
|
512 |
} |
|
513 |
} |
|
514 |
||
515 |
@Test |
|
56263 | 516 |
public void outstanding2() throws Exception { |
56052 | 517 |
try (DummyWebSocketServer server = notReadingServer()) { |
518 |
server.open(); |
|
519 |
WebSocket ws = newHttpClient() |
|
520 |
.newWebSocketBuilder() |
|
521 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
522 |
.join(); |
|
523 |
||
524 |
CharBuffer data = CharBuffer.allocate(65536); |
|
56263 | 525 |
for (int i = 0; ; i++) { // fill up the send buffer |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
526 |
System.out.printf("begin cycle #%s at %s%n", |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
527 |
i, System.currentTimeMillis()); |
56052 | 528 |
try { |
529 |
ws.sendText(data, true).get(10, TimeUnit.SECONDS); |
|
530 |
data.clear(); |
|
531 |
} catch (TimeoutException e) { |
|
532 |
break; |
|
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
533 |
} finally { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
534 |
System.out.printf("end cycle #%s at %s%n", |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
535 |
i, System.currentTimeMillis()); |
56052 | 536 |
} |
537 |
} |
|
538 |
assertCompletesExceptionally(ISE, ws.sendText("", true)); |
|
539 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
|
540 |
} |
|
541 |
} |
|
542 |
||
543 |
@Test |
|
56263 | 544 |
public void interleavingTypes1() throws IOException { |
56052 | 545 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
546 |
server.open(); |
|
547 |
WebSocket ws = newHttpClient() |
|
548 |
.newWebSocketBuilder() |
|
549 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
550 |
.join(); |
|
551 |
||
552 |
ws.sendBinary(ByteBuffer.allocate(16), false).join(); |
|
553 |
assertCompletesExceptionally(ISE, ws.sendText("text", false)); |
|
554 |
assertCompletesExceptionally(ISE, ws.sendText("text", true)); |
|
555 |
} |
|
556 |
} |
|
557 |
||
558 |
@Test |
|
56263 | 559 |
public void interleavingTypes2() throws IOException { |
56052 | 560 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
561 |
server.open(); |
|
562 |
WebSocket ws = newHttpClient() |
|
563 |
.newWebSocketBuilder() |
|
564 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
565 |
.join(); |
|
566 |
||
567 |
ws.sendText("text", false).join(); |
|
568 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(16), false)); |
|
569 |
assertCompletesExceptionally(ISE, ws.sendBinary(ByteBuffer.allocate(16), true)); |
|
570 |
} |
|
571 |
} |
|
572 |
||
573 |
@Test |
|
56263 | 574 |
public void sendMethodsThrowIOE1() throws IOException { |
56052 | 575 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
576 |
server.open(); |
|
577 |
WebSocket ws = newHttpClient() |
|
578 |
.newWebSocketBuilder() |
|
579 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
580 |
.join(); |
|
581 |
||
56263 | 582 |
ws.sendClose(NORMAL_CLOSURE, "ok").join(); |
583 |
||
584 |
assertCompletesExceptionally(IOE, ws.sendClose(WebSocket.NORMAL_CLOSURE, "ok")); |
|
56052 | 585 |
|
56263 | 586 |
assertCompletesExceptionally(IOE, ws.sendText("", true)); |
587 |
assertCompletesExceptionally(IOE, ws.sendText("", false)); |
|
588 |
assertCompletesExceptionally(IOE, ws.sendText("abc", true)); |
|
589 |
assertCompletesExceptionally(IOE, ws.sendText("abc", false)); |
|
590 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
|
591 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(0), false)); |
|
592 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(1), true)); |
|
593 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(1), false)); |
|
56052 | 594 |
|
56263 | 595 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(125))); |
596 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(124))); |
|
597 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(1))); |
|
598 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(0))); |
|
56052 | 599 |
|
56263 | 600 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(125))); |
601 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(124))); |
|
602 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(1))); |
|
603 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(0))); |
|
56052 | 604 |
} |
605 |
} |
|
606 |
||
607 |
@Test |
|
56263 | 608 |
public void sendMethodsThrowIOE2() throws Exception { |
56057 | 609 |
try (DummyWebSocketServer server = serverWithCannedData(0x88, 0x00)) { |
56052 | 610 |
server.open(); |
611 |
CompletableFuture<Void> onCloseCalled = new CompletableFuture<>(); |
|
612 |
CompletableFuture<Void> canClose = new CompletableFuture<>(); |
|
613 |
||
614 |
WebSocket ws = newHttpClient() |
|
615 |
.newWebSocketBuilder() |
|
616 |
.buildAsync(server.getURI(), new WebSocket.Listener() { |
|
617 |
@Override |
|
618 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
619 |
int statusCode, |
|
620 |
String reason) { |
|
56263 | 621 |
System.out.printf("onClose(%s, '%s')%n", statusCode, reason); |
56052 | 622 |
onCloseCalled.complete(null); |
623 |
return canClose; |
|
624 |
} |
|
625 |
||
626 |
@Override |
|
627 |
public void onError(WebSocket webSocket, Throwable error) { |
|
628 |
System.out.println("onError(" + error + ")"); |
|
56263 | 629 |
onCloseCalled.completeExceptionally(error); |
56052 | 630 |
} |
631 |
}) |
|
632 |
.join(); |
|
633 |
||
634 |
onCloseCalled.join(); // Wait for onClose to be called |
|
56263 | 635 |
canClose.complete(null); // Signal to the WebSocket it can close the output |
56052 | 636 |
TimeUnit.SECONDS.sleep(5); // Give canClose some time to reach the WebSocket |
56263 | 637 |
|
638 |
assertCompletesExceptionally(IOE, ws.sendClose(WebSocket.NORMAL_CLOSURE, "ok")); |
|
56052 | 639 |
|
56263 | 640 |
assertCompletesExceptionally(IOE, ws.sendText("", true)); |
641 |
assertCompletesExceptionally(IOE, ws.sendText("", false)); |
|
642 |
assertCompletesExceptionally(IOE, ws.sendText("abc", true)); |
|
643 |
assertCompletesExceptionally(IOE, ws.sendText("abc", false)); |
|
644 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
|
645 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(0), false)); |
|
646 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(1), true)); |
|
647 |
assertCompletesExceptionally(IOE, ws.sendBinary(ByteBuffer.allocate(1), false)); |
|
56052 | 648 |
|
56263 | 649 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(125))); |
650 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(124))); |
|
651 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(1))); |
|
652 |
assertCompletesExceptionally(IOE, ws.sendPing(ByteBuffer.allocate(0))); |
|
56052 | 653 |
|
56263 | 654 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(125))); |
655 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(124))); |
|
656 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(1))); |
|
657 |
assertCompletesExceptionally(IOE, ws.sendPong(ByteBuffer.allocate(0))); |
|
56052 | 658 |
} |
659 |
} |
|
56058 | 660 |
|
661 |
@Test |
|
56263 | 662 |
public void simpleAggregatingBinaryMessages() throws IOException { |
663 |
List<byte[]> expected = List.of("alpha", "beta", "gamma", "delta") |
|
664 |
.stream() |
|
665 |
.map(s -> s.getBytes(StandardCharsets.US_ASCII)) |
|
666 |
.collect(Collectors.toList()); |
|
667 |
int[] binary = new int[]{ |
|
668 |
0x82, 0x05, 0x61, 0x6c, 0x70, 0x68, 0x61, // [alpha] |
|
669 |
0x02, 0x02, 0x62, 0x65, // [be |
|
670 |
0x80, 0x02, 0x74, 0x61, // ta] |
|
671 |
0x02, 0x01, 0x67, // [g |
|
672 |
0x00, 0x01, 0x61, // a |
|
673 |
0x00, 0x00, // |
|
674 |
0x00, 0x00, // |
|
675 |
0x00, 0x01, 0x6d, // m |
|
676 |
0x00, 0x01, 0x6d, // m |
|
677 |
0x80, 0x01, 0x61, // a] |
|
678 |
0x8a, 0x00, // <PONG> |
|
679 |
0x02, 0x04, 0x64, 0x65, 0x6c, 0x74, // [delt |
|
680 |
0x00, 0x01, 0x61, // a |
|
681 |
0x80, 0x00, // ] |
|
682 |
0x88, 0x00 // <CLOSE> |
|
683 |
}; |
|
684 |
CompletableFuture<List<byte[]>> actual = new CompletableFuture<>(); |
|
685 |
||
686 |
try (DummyWebSocketServer server = serverWithCannedData(binary)) { |
|
687 |
server.open(); |
|
688 |
||
689 |
WebSocket.Listener listener = new WebSocket.Listener() { |
|
690 |
||
691 |
List<byte[]> collectedBytes = new ArrayList<>(); |
|
692 |
ByteBuffer binary; |
|
693 |
||
694 |
@Override |
|
695 |
public CompletionStage<?> onBinary(WebSocket webSocket, |
|
696 |
ByteBuffer message, |
|
697 |
WebSocket.MessagePart part) { |
|
698 |
System.out.printf("onBinary(%s, %s)%n", message, part); |
|
699 |
webSocket.request(1); |
|
700 |
byte[] bytes = null; |
|
701 |
switch (part) { |
|
702 |
case FIRST: |
|
703 |
binary = ByteBuffer.allocate(message.remaining() * 2); |
|
704 |
case PART: |
|
705 |
append(message); |
|
706 |
return null; |
|
707 |
case LAST: |
|
708 |
append(message); |
|
709 |
binary.flip(); |
|
710 |
bytes = new byte[binary.remaining()]; |
|
711 |
binary.get(bytes); |
|
712 |
binary.clear(); |
|
713 |
break; |
|
714 |
case WHOLE: |
|
715 |
bytes = new byte[message.remaining()]; |
|
716 |
message.get(bytes); |
|
717 |
break; |
|
718 |
} |
|
719 |
processWholeBinary(bytes); |
|
720 |
return null; |
|
721 |
} |
|
722 |
||
723 |
private void append(ByteBuffer message) { |
|
724 |
if (binary.remaining() < message.remaining()) { |
|
725 |
assert message.remaining() > 0; |
|
726 |
int cap = (binary.capacity() + message.remaining()) * 2; |
|
727 |
ByteBuffer b = ByteBuffer.allocate(cap); |
|
728 |
b.put(binary.flip()); |
|
729 |
binary = b; |
|
730 |
} |
|
731 |
binary.put(message); |
|
732 |
} |
|
733 |
||
734 |
private void processWholeBinary(byte[] bytes) { |
|
735 |
String stringBytes = new String(bytes, StandardCharsets.UTF_8); |
|
736 |
System.out.println("processWholeBinary: " + stringBytes); |
|
737 |
collectedBytes.add(bytes); |
|
738 |
} |
|
739 |
||
740 |
@Override |
|
741 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
742 |
int statusCode, |
|
743 |
String reason) { |
|
744 |
actual.complete(collectedBytes); |
|
745 |
return null; |
|
746 |
} |
|
747 |
||
748 |
@Override |
|
749 |
public void onError(WebSocket webSocket, Throwable error) { |
|
750 |
actual.completeExceptionally(error); |
|
751 |
} |
|
752 |
}; |
|
753 |
||
754 |
newHttpClient().newWebSocketBuilder() |
|
755 |
.buildAsync(server.getURI(), listener) |
|
756 |
.join(); |
|
757 |
||
758 |
List<byte[]> a = actual.join(); |
|
759 |
System.out.println("joined"); |
|
760 |
assertEquals(a, expected); |
|
761 |
} |
|
762 |
} |
|
763 |
||
764 |
@Test |
|
765 |
public void simpleAggregatingTextMessages() throws IOException { |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
766 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
767 |
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
|
768 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
769 |
int[] binary = new int[]{ |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
770 |
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
|
771 |
0x01, 0x02, 0x62, 0x65, // "be |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
772 |
0x80, 0x02, 0x74, 0x61, // ta" |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
773 |
0x01, 0x01, 0x67, // "g |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
774 |
0x00, 0x01, 0x61, // a |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
775 |
0x00, 0x00, // |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
776 |
0x00, 0x00, // |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
777 |
0x00, 0x01, 0x6d, // m |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
778 |
0x00, 0x01, 0x6d, // m |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
779 |
0x80, 0x01, 0x61, // a" |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
780 |
0x8a, 0x00, // <PONG> |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
781 |
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
|
782 |
0x00, 0x01, 0x61, // a |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
783 |
0x80, 0x00, // " |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
784 |
0x88, 0x00 // <CLOSE> |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
785 |
}; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
786 |
CompletableFuture<List<String>> actual = new CompletableFuture<>(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
787 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
788 |
try (DummyWebSocketServer server = serverWithCannedData(binary)) { |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
789 |
server.open(); |
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 |
WebSocket.Listener listener = new WebSocket.Listener() { |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
792 |
|
56263 | 793 |
List<String> collectedStrings = new ArrayList<>(); |
794 |
StringBuilder text; |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
795 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
796 |
@Override |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
797 |
public CompletionStage<?> onText(WebSocket webSocket, |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
798 |
CharSequence message, |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
799 |
WebSocket.MessagePart part) { |
56263 | 800 |
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
|
801 |
webSocket.request(1); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
802 |
String str = null; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
803 |
switch (part) { |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
804 |
case FIRST: |
56263 | 805 |
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
|
806 |
case PART: |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
807 |
text.append(message); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
808 |
return null; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
809 |
case LAST: |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
810 |
text.append(message); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
811 |
str = text.toString(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
812 |
break; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
813 |
case WHOLE: |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
814 |
str = message.toString(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
815 |
break; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
816 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
817 |
processWholeText(str); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
818 |
return null; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
819 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
820 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
821 |
private void processWholeText(String string) { |
56130
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
822 |
System.out.println(string); |
56263 | 823 |
collectedStrings.add(string); |
56130
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
824 |
} |
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
825 |
|
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
826 |
@Override |
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
827 |
public CompletionStage<?> onClose(WebSocket webSocket, |
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
828 |
int statusCode, |
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
829 |
String reason) { |
56263 | 830 |
actual.complete(collectedStrings); |
56130
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
831 |
return null; |
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
832 |
} |
56263 | 833 |
|
834 |
@Override |
|
835 |
public void onError(WebSocket webSocket, Throwable error) { |
|
836 |
actual.completeExceptionally(error); |
|
837 |
} |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
838 |
}; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
839 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
840 |
newHttpClient().newWebSocketBuilder() |
56263 | 841 |
.buildAsync(server.getURI(), listener) |
842 |
.join(); |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
843 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
844 |
List<String> a = actual.join(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
845 |
assertEquals(a, expected); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
846 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
847 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
848 |
|
56263 | 849 |
/* |
850 |
* Exercises the scenario where requests for more messages are made prior to |
|
851 |
* completing the returned CompletionStage instances. |
|
852 |
*/ |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
853 |
@Test |
56263 | 854 |
public void aggregatingTextMessages() throws IOException { |
56058 | 855 |
|
856 |
List<String> expected = List.of("alpha", "beta", "gamma", "delta"); |
|
857 |
||
858 |
int[] binary = new int[]{ |
|
859 |
0x81, 0x05, 0x61, 0x6c, 0x70, 0x68, 0x61, // "alpha" |
|
860 |
0x01, 0x02, 0x62, 0x65, // "be |
|
861 |
0x80, 0x02, 0x74, 0x61, // ta" |
|
862 |
0x01, 0x01, 0x67, // "g |
|
863 |
0x00, 0x01, 0x61, // a |
|
864 |
0x00, 0x00, // |
|
865 |
0x00, 0x00, // |
|
866 |
0x00, 0x01, 0x6d, // m |
|
867 |
0x00, 0x01, 0x6d, // m |
|
868 |
0x80, 0x01, 0x61, // a" |
|
869 |
0x8a, 0x00, // <PONG> |
|
870 |
0x01, 0x04, 0x64, 0x65, 0x6c, 0x74, // "delt |
|
871 |
0x00, 0x01, 0x61, // a |
|
872 |
0x80, 0x00, // " |
|
873 |
0x88, 0x00 // <CLOSE> |
|
874 |
}; |
|
875 |
CompletableFuture<List<String>> actual = new CompletableFuture<>(); |
|
876 |
||
877 |
||
878 |
try (DummyWebSocketServer server = serverWithCannedData(binary)) { |
|
879 |
server.open(); |
|
880 |
||
881 |
WebSocket.Listener listener = new WebSocket.Listener() { |
|
882 |
||
56263 | 883 |
List<CharSequence> parts; |
884 |
/* |
|
885 |
* A CompletableFuture which will complete once the current |
|
886 |
* message has been fully assembled (LAST/WHOLE). Until then |
|
887 |
* the listener returns this instance for every call. |
|
888 |
*/ |
|
56058 | 889 |
CompletableFuture<?> currentCf; |
890 |
List<String> collected = new ArrayList<>(); |
|
891 |
||
892 |
@Override |
|
893 |
public CompletionStage<?> onText(WebSocket webSocket, |
|
894 |
CharSequence message, |
|
895 |
WebSocket.MessagePart part) { |
|
896 |
switch (part) { |
|
897 |
case WHOLE: |
|
898 |
CompletableFuture<?> cf = new CompletableFuture<>(); |
|
899 |
cf.thenRun(() -> webSocket.request(1)); |
|
900 |
processWholeMessage(List.of(message), cf); |
|
901 |
return cf; |
|
902 |
case FIRST: |
|
56263 | 903 |
parts = new ArrayList<>(); |
56058 | 904 |
parts.add(message); |
905 |
currentCf = new CompletableFuture<>(); |
|
906 |
currentCf.thenRun(() -> webSocket.request(1)); |
|
907 |
webSocket.request(1); |
|
908 |
break; |
|
909 |
case PART: |
|
910 |
parts.add(message); |
|
911 |
webSocket.request(1); |
|
912 |
break; |
|
913 |
case LAST: |
|
914 |
parts.add(message); |
|
56263 | 915 |
CompletableFuture<?> copyCf = this.currentCf; |
916 |
processWholeMessage(parts, copyCf); |
|
56058 | 917 |
currentCf = null; |
56263 | 918 |
parts = null; |
919 |
return copyCf; |
|
56058 | 920 |
} |
921 |
return currentCf; |
|
922 |
} |
|
923 |
||
924 |
@Override |
|
925 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
926 |
int statusCode, |
|
927 |
String reason) { |
|
928 |
actual.complete(collected); |
|
929 |
return null; |
|
930 |
} |
|
931 |
||
56263 | 932 |
@Override |
933 |
public void onError(WebSocket webSocket, Throwable error) { |
|
934 |
actual.completeExceptionally(error); |
|
935 |
} |
|
936 |
||
56058 | 937 |
public void processWholeMessage(List<CharSequence> data, |
938 |
CompletableFuture<?> cf) { |
|
939 |
StringBuilder b = new StringBuilder(); |
|
940 |
data.forEach(b::append); |
|
941 |
String s = b.toString(); |
|
942 |
System.out.println(s); |
|
943 |
cf.complete(null); |
|
944 |
collected.add(s); |
|
945 |
} |
|
946 |
}; |
|
56263 | 947 |
|
948 |
newHttpClient().newWebSocketBuilder() |
|
949 |
.buildAsync(server.getURI(), listener) |
|
950 |
.join(); |
|
56058 | 951 |
|
952 |
List<String> a = actual.join(); |
|
953 |
assertEquals(a, expected); |
|
954 |
} |
|
955 |
} |
|
56052 | 956 |
} |