author | chegar |
Sat, 17 Mar 2018 18:01:01 +0000 | |
branch | http-client-branch |
changeset 56317 | 52a9f6b74e43 |
parent 56314 | f92e7a8a189f |
child 56318 | 2a96e88888b2 |
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 |
|
56317
52a9f6b74e43
http-client-branch: split large WebSocket test into three
chegar
parents:
56314
diff
changeset
|
27 |
* @run testng/othervm |
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 |
*/ |
31 |
||
32 |
import java.io.IOException; |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
33 |
import java.net.http.WebSocket; |
56052 | 34 |
import java.nio.ByteBuffer; |
35 |
import java.nio.CharBuffer; |
|
36 |
import java.nio.channels.SocketChannel; |
|
37 |
import java.nio.charset.StandardCharsets; |
|
56058 | 38 |
import java.util.ArrayList; |
56263 | 39 |
import java.util.Arrays; |
56058 | 40 |
import java.util.List; |
56052 | 41 |
import java.util.concurrent.CompletableFuture; |
42 |
import java.util.concurrent.CompletionStage; |
|
43 |
import java.util.concurrent.TimeUnit; |
|
44 |
import java.util.concurrent.TimeoutException; |
|
56263 | 45 |
import java.util.stream.Collectors; |
56317
52a9f6b74e43
http-client-branch: split large WebSocket test into three
chegar
parents:
56314
diff
changeset
|
46 |
import org.testng.annotations.DataProvider; |
52a9f6b74e43
http-client-branch: split large WebSocket test into three
chegar
parents:
56314
diff
changeset
|
47 |
import org.testng.annotations.Test; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56064
diff
changeset
|
48 |
import static java.net.http.HttpClient.newHttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56064
diff
changeset
|
49 |
import static java.net.http.WebSocket.NORMAL_CLOSURE; |
56052 | 50 |
import static org.testng.Assert.assertEquals; |
51 |
import static org.testng.Assert.assertFalse; |
|
52 |
import static org.testng.Assert.assertThrows; |
|
53 |
import static org.testng.Assert.assertTrue; |
|
56057 | 54 |
import static org.testng.Assert.fail; |
56052 | 55 |
|
56 |
public class WebSocketTest { |
|
57 |
||
56057 | 58 |
private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class; |
59 |
private static final Class<IllegalStateException> ISE = IllegalStateException.class; |
|
60 |
private static final Class<IOException> IOE = IOException.class; |
|
56052 | 61 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
62 |
/* shortcut */ |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
63 |
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
|
64 |
CompletionStage<?> stage) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
65 |
Support.assertCompletesExceptionally(clazz, stage); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
66 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
67 |
|
56064 | 68 |
private static DummyWebSocketServer serverWithCannedData(int... data) { |
69 |
byte[] copy = new byte[data.length]; |
|
70 |
for (int i = 0; i < data.length; i++) { |
|
71 |
copy[i] = (byte) data[i]; |
|
72 |
} |
|
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
73 |
return serverWithCannedData(copy); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
74 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
75 |
|
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
76 |
private static DummyWebSocketServer serverWithCannedData(byte... data) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
77 |
byte[] copy = Arrays.copyOf(data, data.length); |
56064 | 78 |
return new DummyWebSocketServer() { |
79 |
@Override |
|
80 |
protected void serve(SocketChannel channel) throws IOException { |
|
81 |
ByteBuffer closeMessage = ByteBuffer.wrap(copy); |
|
56263 | 82 |
channel.write(closeMessage); |
56064 | 83 |
super.serve(channel); |
84 |
} |
|
85 |
}; |
|
86 |
} |
|
87 |
||
56052 | 88 |
/* |
89 |
* This server does not read from the wire, allowing its client to fill up |
|
90 |
* their send buffer. Used to test scenarios with outstanding send |
|
91 |
* operations. |
|
92 |
*/ |
|
93 |
private static DummyWebSocketServer notReadingServer() { |
|
94 |
return new DummyWebSocketServer() { |
|
95 |
@Override |
|
96 |
protected void serve(SocketChannel channel) throws IOException { |
|
97 |
try { |
|
98 |
Thread.sleep(Long.MAX_VALUE); |
|
99 |
} catch (InterruptedException e) { |
|
100 |
throw new IOException(e); |
|
101 |
} |
|
102 |
} |
|
103 |
}; |
|
104 |
} |
|
105 |
||
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
106 |
private static String stringWith2NBytes(int n) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
107 |
// -- 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
|
108 |
char[] abc = { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
109 |
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
|
110 |
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
|
111 |
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
|
112 |
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
|
113 |
0x042F, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
114 |
}; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
115 |
// repeat cyclically |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
116 |
StringBuilder sb = new StringBuilder(n); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
117 |
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
|
118 |
sb.append(abc[j]); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
119 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
120 |
String s = sb.toString(); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
121 |
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
|
122 |
return s; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
123 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
124 |
|
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
125 |
@Test |
56052 | 126 |
public void testIllegalArgument() throws IOException { |
127 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
|
128 |
server.open(); |
|
129 |
WebSocket ws = newHttpClient() |
|
130 |
.newWebSocketBuilder() |
|
131 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
132 |
.join(); |
|
133 |
||
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
134 |
assertFails(IAE, ws.sendPing(ByteBuffer.allocate(126))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
135 |
assertFails(IAE, ws.sendPing(ByteBuffer.allocate(127))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
136 |
assertFails(IAE, ws.sendPing(ByteBuffer.allocate(128))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
137 |
assertFails(IAE, ws.sendPing(ByteBuffer.allocate(129))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
138 |
assertFails(IAE, ws.sendPing(ByteBuffer.allocate(256))); |
56052 | 139 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
140 |
assertFails(IAE, ws.sendPong(ByteBuffer.allocate(126))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
141 |
assertFails(IAE, ws.sendPong(ByteBuffer.allocate(127))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
142 |
assertFails(IAE, ws.sendPong(ByteBuffer.allocate(128))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
143 |
assertFails(IAE, ws.sendPong(ByteBuffer.allocate(129))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
144 |
assertFails(IAE, ws.sendPong(ByteBuffer.allocate(256))); |
56052 | 145 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
146 |
assertFails(IOE, ws.sendText(incompleteString(), true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
147 |
assertFails(IOE, ws.sendText(incompleteString(), false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
148 |
assertFails(IOE, ws.sendText(malformedString(), true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
149 |
assertFails(IOE, ws.sendText(malformedString(), false)); |
56052 | 150 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
151 |
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
|
152 |
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
|
153 |
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
|
154 |
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
|
155 |
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
|
156 |
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
|
157 |
assertFails(IAE, ws.sendClose(NORMAL_CLOSURE, malformedString())); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
158 |
assertFails(IAE, ws.sendClose(NORMAL_CLOSURE, incompleteString())); |
56052 | 159 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
160 |
assertFails(IAE, ws.sendClose(-2, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
161 |
assertFails(IAE, ws.sendClose(-1, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
162 |
assertFails(IAE, ws.sendClose(0, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
163 |
assertFails(IAE, ws.sendClose(1, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
164 |
assertFails(IAE, ws.sendClose(500, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
165 |
assertFails(IAE, ws.sendClose(998, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
166 |
assertFails(IAE, ws.sendClose(999, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
167 |
assertFails(IAE, ws.sendClose(1002, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
168 |
assertFails(IAE, ws.sendClose(1003, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
169 |
assertFails(IAE, ws.sendClose(1006, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
170 |
assertFails(IAE, ws.sendClose(1007, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
171 |
assertFails(IAE, ws.sendClose(1009, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
172 |
assertFails(IAE, ws.sendClose(1010, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
173 |
assertFails(IAE, ws.sendClose(1012, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
174 |
assertFails(IAE, ws.sendClose(1013, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
175 |
assertFails(IAE, ws.sendClose(1015, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
176 |
assertFails(IAE, ws.sendClose(5000, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
177 |
assertFails(IAE, ws.sendClose(32768, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
178 |
assertFails(IAE, ws.sendClose(65535, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
179 |
assertFails(IAE, ws.sendClose(65536, "a reason")); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
180 |
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
|
181 |
assertFails(IAE, ws.sendClose(Integer.MIN_VALUE, "a reason")); |
56052 | 182 |
|
183 |
assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE)); |
|
56055
7d387d151a3e
http-client-branch: (WebSocket) tiny spec simplification + test
prappo
parents:
56052
diff
changeset
|
184 |
assertThrows(IAE, () -> ws.request(Long.MIN_VALUE)); |
56052 | 185 |
assertThrows(IAE, () -> ws.request(-1)); |
186 |
assertThrows(IAE, () -> ws.request(0)); |
|
187 |
} |
|
188 |
} |
|
189 |
||
190 |
private static String malformedString() { |
|
191 |
return new String(new char[]{0xDC00, 0xD800}); |
|
192 |
} |
|
193 |
||
194 |
private static String incompleteString() { |
|
195 |
return new String(new char[]{0xD800}); |
|
196 |
} |
|
197 |
||
198 |
private static String stringWithNBytes(int n) { |
|
56263 | 199 |
char[] chars = new char[n]; |
200 |
Arrays.fill(chars, 'A'); |
|
201 |
return new String(chars); |
|
56052 | 202 |
} |
203 |
||
204 |
@Test |
|
56263 | 205 |
public void outstanding1() throws Exception { |
56052 | 206 |
try (DummyWebSocketServer server = notReadingServer()) { |
207 |
server.open(); |
|
208 |
WebSocket ws = newHttpClient() |
|
209 |
.newWebSocketBuilder() |
|
210 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
211 |
.join(); |
|
212 |
||
213 |
ByteBuffer data = ByteBuffer.allocate(65536); |
|
56263 | 214 |
for (int i = 0; ; i++) { // fill up the send buffer |
56052 | 215 |
System.out.println("cycle #" + i); |
216 |
try { |
|
217 |
ws.sendBinary(data, true).get(10, TimeUnit.SECONDS); |
|
218 |
data.clear(); |
|
219 |
} catch (TimeoutException e) { |
|
220 |
break; |
|
221 |
} |
|
222 |
} |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
223 |
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
|
224 |
assertFails(ISE, ws.sendText("", true)); |
56052 | 225 |
} |
226 |
} |
|
227 |
||
228 |
@Test |
|
56263 | 229 |
public void outstanding2() throws Exception { |
56052 | 230 |
try (DummyWebSocketServer server = notReadingServer()) { |
231 |
server.open(); |
|
232 |
WebSocket ws = newHttpClient() |
|
233 |
.newWebSocketBuilder() |
|
234 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
235 |
.join(); |
|
236 |
||
237 |
CharBuffer data = CharBuffer.allocate(65536); |
|
56263 | 238 |
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
|
239 |
System.out.printf("begin cycle #%s at %s%n", |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
240 |
i, System.currentTimeMillis()); |
56052 | 241 |
try { |
242 |
ws.sendText(data, true).get(10, TimeUnit.SECONDS); |
|
243 |
data.clear(); |
|
244 |
} catch (TimeoutException e) { |
|
245 |
break; |
|
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
246 |
} finally { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
247 |
System.out.printf("end cycle #%s at %s%n", |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56269
diff
changeset
|
248 |
i, System.currentTimeMillis()); |
56052 | 249 |
} |
250 |
} |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
251 |
assertFails(ISE, ws.sendText("", true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
252 |
assertFails(ISE, ws.sendBinary(ByteBuffer.allocate(0), true)); |
56052 | 253 |
} |
254 |
} |
|
255 |
||
256 |
@Test |
|
56263 | 257 |
public void interleavingTypes1() throws IOException { |
56052 | 258 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
259 |
server.open(); |
|
260 |
WebSocket ws = newHttpClient() |
|
261 |
.newWebSocketBuilder() |
|
262 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
263 |
.join(); |
|
264 |
||
265 |
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
|
266 |
assertFails(ISE, ws.sendText("text", false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
267 |
assertFails(ISE, ws.sendText("text", true)); |
56052 | 268 |
} |
269 |
} |
|
270 |
||
271 |
@Test |
|
56263 | 272 |
public void interleavingTypes2() throws IOException { |
56052 | 273 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
274 |
server.open(); |
|
275 |
WebSocket ws = newHttpClient() |
|
276 |
.newWebSocketBuilder() |
|
277 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
278 |
.join(); |
|
279 |
||
280 |
ws.sendText("text", false).join(); |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
281 |
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
|
282 |
assertFails(ISE, ws.sendBinary(ByteBuffer.allocate(16), true)); |
56052 | 283 |
} |
284 |
} |
|
285 |
||
286 |
@Test |
|
56263 | 287 |
public void sendMethodsThrowIOE1() throws IOException { |
56052 | 288 |
try (DummyWebSocketServer server = new DummyWebSocketServer()) { |
289 |
server.open(); |
|
290 |
WebSocket ws = newHttpClient() |
|
291 |
.newWebSocketBuilder() |
|
292 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
293 |
.join(); |
|
294 |
||
56263 | 295 |
ws.sendClose(NORMAL_CLOSURE, "ok").join(); |
296 |
||
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
297 |
assertFails(IOE, ws.sendClose(WebSocket.NORMAL_CLOSURE, "ok")); |
56052 | 298 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
299 |
assertFails(IOE, ws.sendText("", true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
300 |
assertFails(IOE, ws.sendText("", false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
301 |
assertFails(IOE, ws.sendText("abc", true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
302 |
assertFails(IOE, ws.sendText("abc", false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
303 |
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
|
304 |
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
|
305 |
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
|
306 |
assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(1), false)); |
56052 | 307 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
308 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(125))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
309 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(124))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
310 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(1))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
311 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(0))); |
56052 | 312 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
313 |
assertFails(IOE, ws.sendPong(ByteBuffer.allocate(125))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
314 |
assertFails(IOE, ws.sendPong(ByteBuffer.allocate(124))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
315 |
assertFails(IOE, ws.sendPong(ByteBuffer.allocate(1))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
316 |
assertFails(IOE, ws.sendPong(ByteBuffer.allocate(0))); |
56052 | 317 |
} |
318 |
} |
|
319 |
||
320 |
@Test |
|
56263 | 321 |
public void sendMethodsThrowIOE2() throws Exception { |
56057 | 322 |
try (DummyWebSocketServer server = serverWithCannedData(0x88, 0x00)) { |
56052 | 323 |
server.open(); |
324 |
CompletableFuture<Void> onCloseCalled = new CompletableFuture<>(); |
|
325 |
CompletableFuture<Void> canClose = new CompletableFuture<>(); |
|
326 |
||
327 |
WebSocket ws = newHttpClient() |
|
328 |
.newWebSocketBuilder() |
|
329 |
.buildAsync(server.getURI(), new WebSocket.Listener() { |
|
330 |
@Override |
|
331 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
332 |
int statusCode, |
|
333 |
String reason) { |
|
56263 | 334 |
System.out.printf("onClose(%s, '%s')%n", statusCode, reason); |
56052 | 335 |
onCloseCalled.complete(null); |
336 |
return canClose; |
|
337 |
} |
|
338 |
||
339 |
@Override |
|
340 |
public void onError(WebSocket webSocket, Throwable error) { |
|
341 |
System.out.println("onError(" + error + ")"); |
|
56263 | 342 |
onCloseCalled.completeExceptionally(error); |
56052 | 343 |
} |
344 |
}) |
|
345 |
.join(); |
|
346 |
||
347 |
onCloseCalled.join(); // Wait for onClose to be called |
|
56263 | 348 |
canClose.complete(null); // Signal to the WebSocket it can close the output |
56052 | 349 |
TimeUnit.SECONDS.sleep(5); // Give canClose some time to reach the WebSocket |
56263 | 350 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
351 |
assertFails(IOE, ws.sendClose(WebSocket.NORMAL_CLOSURE, "ok")); |
56052 | 352 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
353 |
assertFails(IOE, ws.sendText("", true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
354 |
assertFails(IOE, ws.sendText("", false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
355 |
assertFails(IOE, ws.sendText("abc", true)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
356 |
assertFails(IOE, ws.sendText("abc", false)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
357 |
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
|
358 |
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
|
359 |
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
|
360 |
assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(1), false)); |
56052 | 361 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
362 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(125))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
363 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(124))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
364 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(1))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
365 |
assertFails(IOE, ws.sendPing(ByteBuffer.allocate(0))); |
56052 | 366 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
367 |
assertFails(IOE, ws.sendPong(ByteBuffer.allocate(125))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
368 |
assertFails(IOE, ws.sendPong(ByteBuffer.allocate(124))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
369 |
assertFails(IOE, ws.sendPong(ByteBuffer.allocate(1))); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
370 |
assertFails(IOE, ws.sendPong(ByteBuffer.allocate(0))); |
56052 | 371 |
} |
372 |
} |
|
56058 | 373 |
|
374 |
@Test |
|
56263 | 375 |
public void simpleAggregatingBinaryMessages() throws IOException { |
376 |
List<byte[]> expected = List.of("alpha", "beta", "gamma", "delta") |
|
377 |
.stream() |
|
378 |
.map(s -> s.getBytes(StandardCharsets.US_ASCII)) |
|
379 |
.collect(Collectors.toList()); |
|
380 |
int[] binary = new int[]{ |
|
381 |
0x82, 0x05, 0x61, 0x6c, 0x70, 0x68, 0x61, // [alpha] |
|
382 |
0x02, 0x02, 0x62, 0x65, // [be |
|
383 |
0x80, 0x02, 0x74, 0x61, // ta] |
|
384 |
0x02, 0x01, 0x67, // [g |
|
385 |
0x00, 0x01, 0x61, // a |
|
386 |
0x00, 0x00, // |
|
387 |
0x00, 0x00, // |
|
388 |
0x00, 0x01, 0x6d, // m |
|
389 |
0x00, 0x01, 0x6d, // m |
|
390 |
0x80, 0x01, 0x61, // a] |
|
391 |
0x8a, 0x00, // <PONG> |
|
392 |
0x02, 0x04, 0x64, 0x65, 0x6c, 0x74, // [delt |
|
393 |
0x00, 0x01, 0x61, // a |
|
394 |
0x80, 0x00, // ] |
|
395 |
0x88, 0x00 // <CLOSE> |
|
396 |
}; |
|
397 |
CompletableFuture<List<byte[]>> actual = new CompletableFuture<>(); |
|
398 |
||
399 |
try (DummyWebSocketServer server = serverWithCannedData(binary)) { |
|
400 |
server.open(); |
|
401 |
||
402 |
WebSocket.Listener listener = new WebSocket.Listener() { |
|
403 |
||
404 |
List<byte[]> collectedBytes = new ArrayList<>(); |
|
405 |
ByteBuffer binary; |
|
406 |
||
407 |
@Override |
|
408 |
public CompletionStage<?> onBinary(WebSocket webSocket, |
|
409 |
ByteBuffer message, |
|
410 |
WebSocket.MessagePart part) { |
|
411 |
System.out.printf("onBinary(%s, %s)%n", message, part); |
|
412 |
webSocket.request(1); |
|
413 |
byte[] bytes = null; |
|
414 |
switch (part) { |
|
415 |
case FIRST: |
|
416 |
binary = ByteBuffer.allocate(message.remaining() * 2); |
|
417 |
case PART: |
|
418 |
append(message); |
|
419 |
return null; |
|
420 |
case LAST: |
|
421 |
append(message); |
|
422 |
binary.flip(); |
|
423 |
bytes = new byte[binary.remaining()]; |
|
424 |
binary.get(bytes); |
|
425 |
binary.clear(); |
|
426 |
break; |
|
427 |
case WHOLE: |
|
428 |
bytes = new byte[message.remaining()]; |
|
429 |
message.get(bytes); |
|
430 |
break; |
|
431 |
} |
|
432 |
processWholeBinary(bytes); |
|
433 |
return null; |
|
434 |
} |
|
435 |
||
436 |
private void append(ByteBuffer message) { |
|
437 |
if (binary.remaining() < message.remaining()) { |
|
438 |
assert message.remaining() > 0; |
|
439 |
int cap = (binary.capacity() + message.remaining()) * 2; |
|
440 |
ByteBuffer b = ByteBuffer.allocate(cap); |
|
441 |
b.put(binary.flip()); |
|
442 |
binary = b; |
|
443 |
} |
|
444 |
binary.put(message); |
|
445 |
} |
|
446 |
||
447 |
private void processWholeBinary(byte[] bytes) { |
|
448 |
String stringBytes = new String(bytes, StandardCharsets.UTF_8); |
|
449 |
System.out.println("processWholeBinary: " + stringBytes); |
|
450 |
collectedBytes.add(bytes); |
|
451 |
} |
|
452 |
||
453 |
@Override |
|
454 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
455 |
int statusCode, |
|
456 |
String reason) { |
|
457 |
actual.complete(collectedBytes); |
|
458 |
return null; |
|
459 |
} |
|
460 |
||
461 |
@Override |
|
462 |
public void onError(WebSocket webSocket, Throwable error) { |
|
463 |
actual.completeExceptionally(error); |
|
464 |
} |
|
465 |
}; |
|
466 |
||
467 |
newHttpClient().newWebSocketBuilder() |
|
468 |
.buildAsync(server.getURI(), listener) |
|
469 |
.join(); |
|
470 |
||
471 |
List<byte[]> a = actual.join(); |
|
472 |
System.out.println("joined"); |
|
473 |
assertEquals(a, expected); |
|
474 |
} |
|
475 |
} |
|
476 |
||
477 |
@Test |
|
478 |
public void simpleAggregatingTextMessages() throws IOException { |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
479 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
480 |
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
|
481 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
482 |
int[] binary = new int[]{ |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
483 |
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
|
484 |
0x01, 0x02, 0x62, 0x65, // "be |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
485 |
0x80, 0x02, 0x74, 0x61, // ta" |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
486 |
0x01, 0x01, 0x67, // "g |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
487 |
0x00, 0x01, 0x61, // a |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
488 |
0x00, 0x00, // |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
489 |
0x00, 0x00, // |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
490 |
0x00, 0x01, 0x6d, // m |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
491 |
0x00, 0x01, 0x6d, // m |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
492 |
0x80, 0x01, 0x61, // a" |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
493 |
0x8a, 0x00, // <PONG> |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
494 |
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
|
495 |
0x00, 0x01, 0x61, // a |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
496 |
0x80, 0x00, // " |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
497 |
0x88, 0x00 // <CLOSE> |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
498 |
}; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
499 |
CompletableFuture<List<String>> actual = new CompletableFuture<>(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
500 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
501 |
try (DummyWebSocketServer server = serverWithCannedData(binary)) { |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
502 |
server.open(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
503 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
504 |
WebSocket.Listener listener = new WebSocket.Listener() { |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
505 |
|
56263 | 506 |
List<String> collectedStrings = new ArrayList<>(); |
507 |
StringBuilder text; |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
508 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
509 |
@Override |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
510 |
public CompletionStage<?> onText(WebSocket webSocket, |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
511 |
CharSequence message, |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
512 |
WebSocket.MessagePart part) { |
56263 | 513 |
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
|
514 |
webSocket.request(1); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
515 |
String str = null; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
516 |
switch (part) { |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
517 |
case FIRST: |
56263 | 518 |
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
|
519 |
case PART: |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
520 |
text.append(message); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
521 |
return null; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
522 |
case LAST: |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
523 |
text.append(message); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
524 |
str = text.toString(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
525 |
break; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
526 |
case WHOLE: |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
527 |
str = message.toString(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
528 |
break; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
529 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
530 |
processWholeText(str); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
531 |
return null; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
532 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
533 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
534 |
private void processWholeText(String string) { |
56130
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
535 |
System.out.println(string); |
56263 | 536 |
collectedStrings.add(string); |
56130
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
537 |
} |
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
538 |
|
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
539 |
@Override |
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
540 |
public CompletionStage<?> onClose(WebSocket webSocket, |
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
541 |
int statusCode, |
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
542 |
String reason) { |
56263 | 543 |
actual.complete(collectedStrings); |
56130
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
544 |
return null; |
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
545 |
} |
56263 | 546 |
|
547 |
@Override |
|
548 |
public void onError(WebSocket webSocket, Throwable error) { |
|
549 |
actual.completeExceptionally(error); |
|
550 |
} |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
551 |
}; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
552 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
553 |
newHttpClient().newWebSocketBuilder() |
56263 | 554 |
.buildAsync(server.getURI(), listener) |
555 |
.join(); |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
556 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
557 |
List<String> a = actual.join(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
558 |
assertEquals(a, expected); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
559 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
560 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
561 |
|
56263 | 562 |
/* |
563 |
* Exercises the scenario where requests for more messages are made prior to |
|
564 |
* completing the returned CompletionStage instances. |
|
565 |
*/ |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
566 |
@Test |
56263 | 567 |
public void aggregatingTextMessages() throws IOException { |
56058 | 568 |
|
569 |
List<String> expected = List.of("alpha", "beta", "gamma", "delta"); |
|
570 |
||
571 |
int[] binary = new int[]{ |
|
572 |
0x81, 0x05, 0x61, 0x6c, 0x70, 0x68, 0x61, // "alpha" |
|
573 |
0x01, 0x02, 0x62, 0x65, // "be |
|
574 |
0x80, 0x02, 0x74, 0x61, // ta" |
|
575 |
0x01, 0x01, 0x67, // "g |
|
576 |
0x00, 0x01, 0x61, // a |
|
577 |
0x00, 0x00, // |
|
578 |
0x00, 0x00, // |
|
579 |
0x00, 0x01, 0x6d, // m |
|
580 |
0x00, 0x01, 0x6d, // m |
|
581 |
0x80, 0x01, 0x61, // a" |
|
582 |
0x8a, 0x00, // <PONG> |
|
583 |
0x01, 0x04, 0x64, 0x65, 0x6c, 0x74, // "delt |
|
584 |
0x00, 0x01, 0x61, // a |
|
585 |
0x80, 0x00, // " |
|
586 |
0x88, 0x00 // <CLOSE> |
|
587 |
}; |
|
588 |
CompletableFuture<List<String>> actual = new CompletableFuture<>(); |
|
589 |
||
590 |
||
591 |
try (DummyWebSocketServer server = serverWithCannedData(binary)) { |
|
592 |
server.open(); |
|
593 |
||
594 |
WebSocket.Listener listener = new WebSocket.Listener() { |
|
595 |
||
56263 | 596 |
List<CharSequence> parts; |
597 |
/* |
|
598 |
* A CompletableFuture which will complete once the current |
|
599 |
* message has been fully assembled (LAST/WHOLE). Until then |
|
600 |
* the listener returns this instance for every call. |
|
601 |
*/ |
|
56058 | 602 |
CompletableFuture<?> currentCf; |
603 |
List<String> collected = new ArrayList<>(); |
|
604 |
||
605 |
@Override |
|
606 |
public CompletionStage<?> onText(WebSocket webSocket, |
|
607 |
CharSequence message, |
|
608 |
WebSocket.MessagePart part) { |
|
609 |
switch (part) { |
|
610 |
case WHOLE: |
|
611 |
CompletableFuture<?> cf = new CompletableFuture<>(); |
|
612 |
cf.thenRun(() -> webSocket.request(1)); |
|
613 |
processWholeMessage(List.of(message), cf); |
|
614 |
return cf; |
|
615 |
case FIRST: |
|
56263 | 616 |
parts = new ArrayList<>(); |
56058 | 617 |
parts.add(message); |
618 |
currentCf = new CompletableFuture<>(); |
|
619 |
currentCf.thenRun(() -> webSocket.request(1)); |
|
620 |
webSocket.request(1); |
|
621 |
break; |
|
622 |
case PART: |
|
623 |
parts.add(message); |
|
624 |
webSocket.request(1); |
|
625 |
break; |
|
626 |
case LAST: |
|
627 |
parts.add(message); |
|
56263 | 628 |
CompletableFuture<?> copyCf = this.currentCf; |
629 |
processWholeMessage(parts, copyCf); |
|
56058 | 630 |
currentCf = null; |
56263 | 631 |
parts = null; |
632 |
return copyCf; |
|
56058 | 633 |
} |
634 |
return currentCf; |
|
635 |
} |
|
636 |
||
637 |
@Override |
|
638 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
639 |
int statusCode, |
|
640 |
String reason) { |
|
641 |
actual.complete(collected); |
|
642 |
return null; |
|
643 |
} |
|
644 |
||
56263 | 645 |
@Override |
646 |
public void onError(WebSocket webSocket, Throwable error) { |
|
647 |
actual.completeExceptionally(error); |
|
648 |
} |
|
649 |
||
56058 | 650 |
public void processWholeMessage(List<CharSequence> data, |
651 |
CompletableFuture<?> cf) { |
|
652 |
StringBuilder b = new StringBuilder(); |
|
653 |
data.forEach(b::append); |
|
654 |
String s = b.toString(); |
|
655 |
System.out.println(s); |
|
656 |
cf.complete(null); |
|
657 |
collected.add(s); |
|
658 |
} |
|
659 |
}; |
|
56263 | 660 |
|
661 |
newHttpClient().newWebSocketBuilder() |
|
662 |
.buildAsync(server.getURI(), listener) |
|
663 |
.join(); |
|
56058 | 664 |
|
665 |
List<String> a = actual.join(); |
|
666 |
assertEquals(a, expected); |
|
667 |
} |
|
668 |
} |
|
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
669 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
670 |
/* |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
671 |
* 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
|
672 |
* 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
|
673 |
*/ |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
674 |
@Test(dataProvider = "nPings") |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
675 |
public void automaticPongs(int nPings) throws Exception { |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
676 |
// big enough to not bother with resize |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
677 |
ByteBuffer buffer = ByteBuffer.allocate(16384); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
678 |
Frame.HeaderWriter w = new Frame.HeaderWriter(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
679 |
for (int i = 0; i < nPings; i++) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
680 |
w.fin(true) |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
681 |
.opcode(Frame.Opcode.PING) |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
682 |
.noMask() |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
683 |
.payloadLen(4) |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
684 |
.write(buffer); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
685 |
buffer.putInt(i); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
686 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
687 |
w.fin(true) |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
688 |
.opcode(Frame.Opcode.CLOSE) |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
689 |
.noMask() |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
690 |
.payloadLen(2) |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
691 |
.write(buffer); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
692 |
buffer.putChar((char) 1000); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
693 |
buffer.flip(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
694 |
try (DummyWebSocketServer server = serverWithCannedData(buffer.array())) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
695 |
MockListener listener = new MockListener(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
696 |
server.open(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
697 |
WebSocket ws = newHttpClient() |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
698 |
.newWebSocketBuilder() |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
699 |
.buildAsync(server.getURI(), listener) |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
700 |
.join(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
701 |
List<MockListener.Invocation> inv = listener.invocations(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
702 |
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
|
703 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
704 |
ByteBuffer data = server.read(); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
705 |
Frame.Reader reader = new Frame.Reader(); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
706 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
707 |
Frame.Consumer consumer = new Frame.Consumer() { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
708 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
709 |
ByteBuffer number = ByteBuffer.allocate(4); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
710 |
Frame.Masker masker = new Frame.Masker(); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
711 |
int i = -1; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
712 |
boolean closed; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
713 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
714 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
715 |
public void fin(boolean value) { assertTrue(value); } |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
716 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
717 |
public void rsv1(boolean value) { assertFalse(value); } |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
718 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
719 |
public void rsv2(boolean value) { assertFalse(value); } |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
720 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
721 |
public void rsv3(boolean value) { assertFalse(value); } |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
722 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
723 |
public void opcode(Frame.Opcode value) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
724 |
if (value == Frame.Opcode.CLOSE) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
725 |
closed = true; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
726 |
return; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
727 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
728 |
assertEquals(value, Frame.Opcode.PONG); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
729 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
730 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
731 |
public void mask(boolean value) { assertTrue(value); } |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
732 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
733 |
public void payloadLen(long value) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
734 |
if (!closed) |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
735 |
assertEquals(value, 4); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
736 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
737 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
738 |
public void maskingKey(int value) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
739 |
masker.mask(value); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
740 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
741 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
742 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
743 |
public void payloadData(ByteBuffer src) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
744 |
masker.transferMasking(src, number); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
745 |
if (closed) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
746 |
return; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
747 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
748 |
number.flip(); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
749 |
int n = number.getInt(); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
750 |
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
|
751 |
number.clear(); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
752 |
if (i >= n) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
753 |
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
|
754 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
755 |
i = n; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
756 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
757 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
758 |
@Override |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
759 |
public void endFrame() { } |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
760 |
}; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
761 |
while (data.hasRemaining()) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
762 |
reader.readFrame(data, consumer); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
763 |
} |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
764 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
765 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
766 |
|
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
767 |
@DataProvider(name = "nPings") |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
768 |
public Object[][] nPings() { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
769 |
return new Object[][]{{1}, {2}, {4}, {8}, {9}, {1023}}; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
770 |
} |
56052 | 771 |
} |