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