author | prappo |
Fri, 16 Mar 2018 01:28:51 +0000 | |
branch | http-client-branch |
changeset 56314 | f92e7a8a189f |
parent 56307 | 5f07fa266686 |
child 56317 | 52a9f6b74e43 |
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 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
27 |
* @run testng/othervm/timeout=600 |
56307
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 |
* WebSocketTest |
56052 | 30 |
*/ |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
31 |
import org.testng.annotations.DataProvider; |
56052 | 32 |
import org.testng.annotations.Test; |
33 |
||
34 |
import java.io.IOException; |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
35 |
import java.net.http.WebSocket; |
56052 | 36 |
import java.nio.ByteBuffer; |
37 |
import java.nio.CharBuffer; |
|
38 |
import java.nio.channels.SocketChannel; |
|
39 |
import java.nio.charset.StandardCharsets; |
|
56058 | 40 |
import java.util.ArrayList; |
56263 | 41 |
import java.util.Arrays; |
56058 | 42 |
import java.util.List; |
56052 | 43 |
import java.util.concurrent.CompletableFuture; |
44 |
import java.util.concurrent.CompletionStage; |
|
45 |
import java.util.concurrent.TimeUnit; |
|
46 |
import java.util.concurrent.TimeoutException; |
|
56263 | 47 |
import java.util.stream.Collectors; |
56052 | 48 |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56064
diff
changeset
|
49 |
import static java.net.http.HttpClient.newHttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56064
diff
changeset
|
50 |
import static java.net.http.WebSocket.NORMAL_CLOSURE; |
56052 | 51 |
import static org.testng.Assert.assertEquals; |
52 |
import static org.testng.Assert.assertFalse; |
|
53 |
import static org.testng.Assert.assertThrows; |
|
54 |
import static org.testng.Assert.assertTrue; |
|
56057 | 55 |
import static org.testng.Assert.fail; |
56052 | 56 |
|
57 |
public class WebSocketTest { |
|
58 |
||
56057 | 59 |
private static final Class<NullPointerException> NPE = NullPointerException.class; |
60 |
private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class; |
|
61 |
private static final Class<IllegalStateException> ISE = IllegalStateException.class; |
|
62 |
private static final Class<IOException> IOE = IOException.class; |
|
56052 | 63 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
64 |
/* |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
65 |
* Examines WebSocket behaviour after a call to abort() |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
66 |
*/ |
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
67 |
@Test |
56263 | 68 |
public void immediateAbort() throws Exception { |
56057 | 69 |
try (DummyWebSocketServer server = serverWithCannedData(0x81, 0x00, 0x88, 0x00)) { |
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
70 |
server.open(); |
56057 | 71 |
CompletableFuture<Void> messageReceived = new CompletableFuture<>(); |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
72 |
WebSocket.Listener listener = new WebSocket.Listener() { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
73 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
74 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
75 |
public void onOpen(WebSocket webSocket) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
76 |
/* no initial request */ |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
77 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
78 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
79 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
80 |
public CompletionStage<?> onText(WebSocket webSocket, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
81 |
CharSequence message, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
82 |
WebSocket.MessagePart part) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
83 |
messageReceived.complete(null); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
84 |
return null; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
85 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
86 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
87 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
88 |
public CompletionStage<?> onBinary(WebSocket webSocket, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
89 |
ByteBuffer message, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
90 |
WebSocket.MessagePart part) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
91 |
messageReceived.complete(null); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
92 |
return null; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
93 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
94 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
95 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
96 |
public CompletionStage<?> onPing(WebSocket webSocket, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
97 |
ByteBuffer message) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
98 |
messageReceived.complete(null); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
99 |
return null; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
100 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
101 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
102 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
103 |
public CompletionStage<?> onPong(WebSocket webSocket, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
104 |
ByteBuffer message) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
105 |
messageReceived.complete(null); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
106 |
return null; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
107 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
108 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
109 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
110 |
public CompletionStage<?> onClose(WebSocket webSocket, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
111 |
int statusCode, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
112 |
String reason) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
113 |
messageReceived.complete(null); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
114 |
return null; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
115 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
116 |
}; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
117 |
|
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
118 |
WebSocket ws = newHttpClient() |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
119 |
.newWebSocketBuilder() |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
120 |
.buildAsync(server.getURI(), listener) |
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
121 |
.join(); |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
122 |
for (int i = 0; i < 3; i++) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
123 |
System.out.printf("iteration #%s%n", i); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
124 |
// after the first abort() each consecutive one must be a no-op, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
125 |
// moreover, query methods should continue to return consistent, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
126 |
// permanent values |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
127 |
for (int j = 0; j < 3; j++) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
128 |
System.out.printf("abort #%s%n", j); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
129 |
ws.abort(); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
130 |
assertTrue(ws.isInputClosed()); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
131 |
assertTrue(ws.isOutputClosed()); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
132 |
assertEquals(ws.getSubprotocol(), ""); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
133 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
134 |
// at this point valid requests MUST be a no-op: |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
135 |
for (int j = 0; j < 3; j++) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
136 |
System.out.printf("request #%s%n", j); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
137 |
ws.request(1); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
138 |
ws.request(2); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
139 |
ws.request(8); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
140 |
ws.request(Integer.MAX_VALUE); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
141 |
ws.request(Long.MAX_VALUE); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
142 |
// invalid requests MUST throw IAE: |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
143 |
assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
144 |
assertThrows(IAE, () -> ws.request(Long.MIN_VALUE)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
145 |
assertThrows(IAE, () -> ws.request(-1)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
146 |
assertThrows(IAE, () -> ws.request(0)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
147 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
148 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
149 |
// even though there is a bunch of messages readily available on the |
56057 | 150 |
// wire we shouldn't have received any of them as we aborted before |
151 |
// the first request |
|
152 |
try { |
|
153 |
messageReceived.get(10, TimeUnit.SECONDS); |
|
154 |
fail(); |
|
56263 | 155 |
} catch (TimeoutException expected) { |
156 |
System.out.println("Finished waiting"); |
|
157 |
} |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
158 |
for (int i = 0; i < 3; i++) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
159 |
System.out.printf("send #%s%n", i); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
160 |
assertFails(IOE, ws.sendText("text!", false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
161 |
assertFails(IOE, ws.sendText("text!", true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
162 |
assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
163 |
assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
164 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(16))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
165 |
assertFails(IOE, ws.sendPong(ByteBuffer.allocate(16))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
166 |
assertFails(IOE, ws.sendClose(NORMAL_CLOSURE, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
167 |
assertThrows(NPE, () -> ws.sendText(null, false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
168 |
assertThrows(NPE, () -> ws.sendText(null, true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
169 |
assertThrows(NPE, () -> ws.sendBinary(null, false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
170 |
assertThrows(NPE, () -> ws.sendBinary(null, true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
171 |
assertThrows(NPE, () -> ws.sendPing(null)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
172 |
assertThrows(NPE, () -> ws.sendPong(null)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
173 |
assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
174 |
} |
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
175 |
} |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
176 |
} |
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
177 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
178 |
/* shortcut */ |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
179 |
private static void assertFails(Class<? extends Throwable> clazz, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
180 |
CompletionStage<?> stage) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
181 |
Support.assertCompletesExceptionally(clazz, stage); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
182 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
183 |
|
56064 | 184 |
private static DummyWebSocketServer serverWithCannedData(int... data) { |
185 |
byte[] copy = new byte[data.length]; |
|
186 |
for (int i = 0; i < data.length; i++) { |
|
187 |
copy[i] = (byte) data[i]; |
|
188 |
} |
|
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
189 |
return serverWithCannedData(copy); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
190 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
191 |
|
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
192 |
private static DummyWebSocketServer serverWithCannedData(byte... data) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
193 |
byte[] copy = Arrays.copyOf(data, data.length); |
56064 | 194 |
return new DummyWebSocketServer() { |
195 |
@Override |
|
196 |
protected void serve(SocketChannel channel) throws IOException { |
|
197 |
ByteBuffer closeMessage = ByteBuffer.wrap(copy); |
|
56263 | 198 |
channel.write(closeMessage); |
56064 | 199 |
super.serve(channel); |
200 |
} |
|
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()); |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
331 |
assertFails(IOException.class, cf); |
56263 | 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()); |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
361 |
assertFails(IOException.class, cf); |
56263 | 362 |
} |
363 |
} |
|
364 |
||
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
365 |
private static String stringWith2NBytes(int n) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
366 |
// -- Russian Alphabet (33 characters, 2 bytes per char) -- |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
367 |
char[] abc = { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
368 |
0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0401, 0x0416, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
369 |
0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
370 |
0x041F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
371 |
0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
372 |
0x042F, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
373 |
}; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
374 |
// repeat cyclically |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
375 |
StringBuilder sb = new StringBuilder(n); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
376 |
for (int i = 0, j = 0; i < n; i++, j = (j + 1) % abc.length) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
377 |
sb.append(abc[j]); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
378 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
379 |
String s = sb.toString(); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
380 |
assert s.length() == n && s.getBytes(StandardCharsets.UTF_8).length == 2 * n; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
381 |
return s; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
382 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
383 |
|
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
384 |
@Test |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
385 |
public void sendCloseTimeout() throws Exception { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
386 |
try (DummyWebSocketServer server = notReadingServer()) { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
387 |
server.open(); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
388 |
WebSocket ws = newHttpClient() |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
389 |
.newWebSocketBuilder() |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
390 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
391 |
.join(); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
392 |
String data = stringWith2NBytes(32768); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
393 |
CompletableFuture<WebSocket> cf = null; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
394 |
for (int i = 0; ; i++) { // fill up the send buffer |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
395 |
System.out.printf("begin cycle #%s at %s%n", |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
396 |
i, System.currentTimeMillis()); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
397 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
398 |
cf = ws.sendText(data, true); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
399 |
cf.get(10, TimeUnit.SECONDS); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
400 |
} catch (TimeoutException e) { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
401 |
break; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
402 |
} finally { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
403 |
System.out.printf("end cycle #%s at %s%n", |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
404 |
i, System.currentTimeMillis()); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
405 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
406 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
407 |
long before = System.currentTimeMillis(); |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
408 |
assertFails(IOException.class, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
409 |
ws.sendClose(WebSocket.NORMAL_CLOSURE, "ok")); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
410 |
long after = System.currentTimeMillis(); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
411 |
// default timeout should be 30 seconds |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
412 |
long elapsed = after - before; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
413 |
System.out.printf("Elapsed %s ms%n", elapsed); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
414 |
assertTrue(elapsed >= 29_000, String.valueOf(elapsed)); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
415 |
assertTrue(ws.isOutputClosed()); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
416 |
assertTrue(ws.isInputClosed()); |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
417 |
assertFails(IOException.class, cf); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
418 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
419 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
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 |
||
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
430 |
assertFails(IAE, ws.sendPing(ByteBuffer.allocate(126))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
431 |
assertFails(IAE, ws.sendPing(ByteBuffer.allocate(127))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
432 |
assertFails(IAE, ws.sendPing(ByteBuffer.allocate(128))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
433 |
assertFails(IAE, ws.sendPing(ByteBuffer.allocate(129))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
434 |
assertFails(IAE, ws.sendPing(ByteBuffer.allocate(256))); |
56052 | 435 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
436 |
assertFails(IAE, ws.sendPong(ByteBuffer.allocate(126))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
437 |
assertFails(IAE, ws.sendPong(ByteBuffer.allocate(127))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
438 |
assertFails(IAE, ws.sendPong(ByteBuffer.allocate(128))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
439 |
assertFails(IAE, ws.sendPong(ByteBuffer.allocate(129))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
440 |
assertFails(IAE, ws.sendPong(ByteBuffer.allocate(256))); |
56052 | 441 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
442 |
assertFails(IOE, ws.sendText(incompleteString(), true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
443 |
assertFails(IOE, ws.sendText(incompleteString(), false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
444 |
assertFails(IOE, ws.sendText(malformedString(), true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
445 |
assertFails(IOE, ws.sendText(malformedString(), false)); |
56052 | 446 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
447 |
assertFails(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(124))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
448 |
assertFails(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(125))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
449 |
assertFails(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(128))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
450 |
assertFails(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(256))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
451 |
assertFails(IAE, ws.sendClose(NORMAL_CLOSURE, stringWithNBytes(257))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
452 |
assertFails(IAE, ws.sendClose(NORMAL_CLOSURE, stringWith2NBytes((123 / 2) + 1))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
453 |
assertFails(IAE, ws.sendClose(NORMAL_CLOSURE, malformedString())); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
454 |
assertFails(IAE, ws.sendClose(NORMAL_CLOSURE, incompleteString())); |
56052 | 455 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
456 |
assertFails(IAE, ws.sendClose(-2, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
457 |
assertFails(IAE, ws.sendClose(-1, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
458 |
assertFails(IAE, ws.sendClose(0, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
459 |
assertFails(IAE, ws.sendClose(1, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
460 |
assertFails(IAE, ws.sendClose(500, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
461 |
assertFails(IAE, ws.sendClose(998, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
462 |
assertFails(IAE, ws.sendClose(999, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
463 |
assertFails(IAE, ws.sendClose(1002, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
464 |
assertFails(IAE, ws.sendClose(1003, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
465 |
assertFails(IAE, ws.sendClose(1006, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
466 |
assertFails(IAE, ws.sendClose(1007, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
467 |
assertFails(IAE, ws.sendClose(1009, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
468 |
assertFails(IAE, ws.sendClose(1010, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
469 |
assertFails(IAE, ws.sendClose(1012, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
470 |
assertFails(IAE, ws.sendClose(1013, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
471 |
assertFails(IAE, ws.sendClose(1015, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
472 |
assertFails(IAE, ws.sendClose(5000, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
473 |
assertFails(IAE, ws.sendClose(32768, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
474 |
assertFails(IAE, ws.sendClose(65535, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
475 |
assertFails(IAE, ws.sendClose(65536, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
476 |
assertFails(IAE, ws.sendClose(Integer.MAX_VALUE, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
477 |
assertFails(IAE, ws.sendClose(Integer.MIN_VALUE, "a reason")); |
56052 | 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 |
} |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
519 |
assertFails(ISE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
520 |
assertFails(ISE, ws.sendText("", true)); |
56052 | 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 |
} |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
547 |
assertFails(ISE, ws.sendText("", true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
548 |
assertFails(ISE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
56052 | 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(); |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
562 |
assertFails(ISE, ws.sendText("text", false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
563 |
assertFails(ISE, ws.sendText("text", true)); |
56052 | 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(); |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
577 |
assertFails(ISE, ws.sendBinary(ByteBuffer.allocate(16), false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
578 |
assertFails(ISE, ws.sendBinary(ByteBuffer.allocate(16), true)); |
56052 | 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 |
||
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
593 |
assertFails(IOE, ws.sendClose(WebSocket.NORMAL_CLOSURE, "ok")); |
56052 | 594 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
595 |
assertFails(IOE, ws.sendText("", true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
596 |
assertFails(IOE, ws.sendText("", false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
597 |
assertFails(IOE, ws.sendText("abc", true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
598 |
assertFails(IOE, ws.sendText("abc", false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
599 |
assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
600 |
assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(0), false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
601 |
assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(1), true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
602 |
assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(1), false)); |
56052 | 603 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
604 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(125))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
605 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(124))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
606 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(1))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
607 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(0))); |
56052 | 608 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
609 |
assertFails(IOE, ws.sendPong(ByteBuffer.allocate(125))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
610 |
assertFails(IOE, ws.sendPong(ByteBuffer.allocate(124))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
611 |
assertFails(IOE, ws.sendPong(ByteBuffer.allocate(1))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
612 |
assertFails(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 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
647 |
assertFails(IOE, ws.sendClose(WebSocket.NORMAL_CLOSURE, "ok")); |
56052 | 648 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
649 |
assertFails(IOE, ws.sendText("", true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
650 |
assertFails(IOE, ws.sendText("", false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
651 |
assertFails(IOE, ws.sendText("abc", true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
652 |
assertFails(IOE, ws.sendText("abc", false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
653 |
assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
654 |
assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(0), false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
655 |
assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(1), true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
656 |
assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(1), false)); |
56052 | 657 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
658 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(125))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
659 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(124))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
660 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(1))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
661 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(0))); |
56052 | 662 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
663 |
assertFails(IOE, ws.sendPong(ByteBuffer.allocate(125))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
664 |
assertFails(IOE, ws.sendPong(ByteBuffer.allocate(124))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
665 |
assertFails(IOE, ws.sendPong(ByteBuffer.allocate(1))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
666 |
assertFails(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 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
966 |
/* |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
967 |
* The server sends Pong messages. The WebSocket replies to messages automatically. |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
968 |
* According to RFC 6455 The WebSocket is free |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
969 |
*/ |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
970 |
@Test(dataProvider = "nPings") |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
971 |
public void automaticPongs(int nPings) throws Exception { |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
972 |
// big enough to not bother with resize |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
973 |
ByteBuffer buffer = ByteBuffer.allocate(16384); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
974 |
Frame.HeaderWriter w = new Frame.HeaderWriter(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
975 |
for (int i = 0; i < nPings; i++) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
976 |
w.fin(true) |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
977 |
.opcode(Frame.Opcode.PING) |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
978 |
.noMask() |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
979 |
.payloadLen(4) |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
980 |
.write(buffer); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
981 |
buffer.putInt(i); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
982 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
983 |
w.fin(true) |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
984 |
.opcode(Frame.Opcode.CLOSE) |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
985 |
.noMask() |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
986 |
.payloadLen(2) |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
987 |
.write(buffer); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
988 |
buffer.putChar((char) 1000); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
989 |
buffer.flip(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
990 |
try (DummyWebSocketServer server = serverWithCannedData(buffer.array())) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
991 |
MockListener listener = new MockListener(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
992 |
server.open(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
993 |
WebSocket ws = newHttpClient() |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
994 |
.newWebSocketBuilder() |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
995 |
.buildAsync(server.getURI(), listener) |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
996 |
.join(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
997 |
List<MockListener.Invocation> inv = listener.invocations(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
998 |
assertEquals(inv.size(), nPings + 2); // onOpen + onClose + n*onPing |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
999 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1000 |
ByteBuffer data = server.read(); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1001 |
Frame.Reader reader = new Frame.Reader(); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1002 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1003 |
Frame.Consumer consumer = new Frame.Consumer() { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1004 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1005 |
ByteBuffer number = ByteBuffer.allocate(4); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1006 |
Frame.Masker masker = new Frame.Masker(); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1007 |
int i = -1; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1008 |
boolean closed; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1009 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1010 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1011 |
public void fin(boolean value) { assertTrue(value); } |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1012 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1013 |
public void rsv1(boolean value) { assertFalse(value); } |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1014 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1015 |
public void rsv2(boolean value) { assertFalse(value); } |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1016 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1017 |
public void rsv3(boolean value) { assertFalse(value); } |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1018 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1019 |
public void opcode(Frame.Opcode value) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1020 |
if (value == Frame.Opcode.CLOSE) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1021 |
closed = true; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1022 |
return; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1023 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1024 |
assertEquals(value, Frame.Opcode.PONG); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1025 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1026 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1027 |
public void mask(boolean value) { assertTrue(value); } |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1028 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1029 |
public void payloadLen(long value) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1030 |
if (!closed) |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1031 |
assertEquals(value, 4); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1032 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1033 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1034 |
public void maskingKey(int value) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1035 |
masker.mask(value); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1036 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1037 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1038 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1039 |
public void payloadData(ByteBuffer src) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1040 |
masker.transferMasking(src, number); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1041 |
if (closed) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1042 |
return; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1043 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1044 |
number.flip(); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1045 |
int n = number.getInt(); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1046 |
System.out.printf("pong number=%s%n", n); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1047 |
number.clear(); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1048 |
if (i >= n) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1049 |
fail(String.format("i=%s, n=%s", i, n)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1050 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1051 |
i = n; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1052 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1053 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1054 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1055 |
public void endFrame() { } |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1056 |
}; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1057 |
while (data.hasRemaining()) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1058 |
reader.readFrame(data, consumer); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
1059 |
} |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
1060 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
1061 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
1062 |
|
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
1063 |
@DataProvider(name = "nPings") |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
1064 |
public Object[][] nPings() { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
1065 |
return new Object[][]{{1}, {2}, {4}, {8}, {9}, {1023}}; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
1066 |
} |
56052 | 1067 |
} |