author | prappo |
Thu, 22 Mar 2018 09:48:27 +0000 | |
branch | http-client-branch |
changeset 56341 | 10fcbe13cd19 |
parent 56337 | 58e16ad3fb98 |
child 56451 | 9585061fdb04 |
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 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
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 |
||
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
32 |
import org.testng.annotations.AfterTest; |
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56317
diff
changeset
|
33 |
import org.testng.annotations.Test; |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56317
diff
changeset
|
34 |
|
56052 | 35 |
import java.io.IOException; |
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
36 |
import java.net.http.WebSocket; |
56052 | 37 |
import java.nio.ByteBuffer; |
38 |
import java.nio.charset.StandardCharsets; |
|
56058 | 39 |
import java.util.ArrayList; |
40 |
import java.util.List; |
|
56052 | 41 |
import java.util.concurrent.CompletableFuture; |
42 |
import java.util.concurrent.CompletionStage; |
|
43 |
import java.util.concurrent.TimeUnit; |
|
56263 | 44 |
import java.util.stream.Collectors; |
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56317
diff
changeset
|
45 |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56064
diff
changeset
|
46 |
import static java.net.http.HttpClient.newHttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56064
diff
changeset
|
47 |
import static java.net.http.WebSocket.NORMAL_CLOSURE; |
56052 | 48 |
import static org.testng.Assert.assertEquals; |
49 |
import static org.testng.Assert.assertThrows; |
|
50 |
||
51 |
public class WebSocketTest { |
|
52 |
||
56057 | 53 |
private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class; |
54 |
private static final Class<IllegalStateException> ISE = IllegalStateException.class; |
|
55 |
private static final Class<IOException> IOE = IOException.class; |
|
56052 | 56 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
57 |
/* shortcut */ |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
58 |
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
|
59 |
CompletionStage<?> stage) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
60 |
Support.assertCompletesExceptionally(clazz, stage); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
61 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56307
diff
changeset
|
62 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
63 |
private DummyWebSocketServer server; |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
64 |
private WebSocket webSocket; |
56052 | 65 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
66 |
@AfterTest |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
67 |
public void cleanup() { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
68 |
server.close(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
69 |
webSocket.abort(); |
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56317
diff
changeset
|
70 |
} |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56317
diff
changeset
|
71 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
72 |
@Test |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
73 |
public void illegalArgument() throws IOException { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
74 |
server = new DummyWebSocketServer(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
75 |
server.open(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
76 |
webSocket = newHttpClient() |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
77 |
.newWebSocketBuilder() |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
78 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
79 |
.join(); |
56052 | 80 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
81 |
assertFails(IAE, webSocket.sendPing(ByteBuffer.allocate(126))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
82 |
assertFails(IAE, webSocket.sendPing(ByteBuffer.allocate(127))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
83 |
assertFails(IAE, webSocket.sendPing(ByteBuffer.allocate(128))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
84 |
assertFails(IAE, webSocket.sendPing(ByteBuffer.allocate(129))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
85 |
assertFails(IAE, webSocket.sendPing(ByteBuffer.allocate(256))); |
56052 | 86 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
87 |
assertFails(IAE, webSocket.sendPong(ByteBuffer.allocate(126))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
88 |
assertFails(IAE, webSocket.sendPong(ByteBuffer.allocate(127))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
89 |
assertFails(IAE, webSocket.sendPong(ByteBuffer.allocate(128))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
90 |
assertFails(IAE, webSocket.sendPong(ByteBuffer.allocate(129))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
91 |
assertFails(IAE, webSocket.sendPong(ByteBuffer.allocate(256))); |
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56317
diff
changeset
|
92 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
93 |
assertFails(IOE, webSocket.sendText(Support.incompleteString(), true)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
94 |
assertFails(IOE, webSocket.sendText(Support.incompleteString(), false)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
95 |
assertFails(IOE, webSocket.sendText(Support.malformedString(), true)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
96 |
assertFails(IOE, webSocket.sendText(Support.malformedString(), false)); |
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56317
diff
changeset
|
97 |
|
56341
10fcbe13cd19
http-client-branch: (WebSocket) from IOE to IAE in sendClose with bad reason
prappo
parents:
56337
diff
changeset
|
98 |
assertFails(IAE, webSocket.sendClose(NORMAL_CLOSURE, Support.stringWithNBytes(124))); |
10fcbe13cd19
http-client-branch: (WebSocket) from IOE to IAE in sendClose with bad reason
prappo
parents:
56337
diff
changeset
|
99 |
assertFails(IAE, webSocket.sendClose(NORMAL_CLOSURE, Support.stringWithNBytes(125))); |
10fcbe13cd19
http-client-branch: (WebSocket) from IOE to IAE in sendClose with bad reason
prappo
parents:
56337
diff
changeset
|
100 |
assertFails(IAE, webSocket.sendClose(NORMAL_CLOSURE, Support.stringWithNBytes(128))); |
10fcbe13cd19
http-client-branch: (WebSocket) from IOE to IAE in sendClose with bad reason
prappo
parents:
56337
diff
changeset
|
101 |
assertFails(IAE, webSocket.sendClose(NORMAL_CLOSURE, Support.stringWithNBytes(256))); |
10fcbe13cd19
http-client-branch: (WebSocket) from IOE to IAE in sendClose with bad reason
prappo
parents:
56337
diff
changeset
|
102 |
assertFails(IAE, webSocket.sendClose(NORMAL_CLOSURE, Support.stringWithNBytes(257))); |
10fcbe13cd19
http-client-branch: (WebSocket) from IOE to IAE in sendClose with bad reason
prappo
parents:
56337
diff
changeset
|
103 |
assertFails(IAE, webSocket.sendClose(NORMAL_CLOSURE, Support.stringWith2NBytes((123 / 2) + 1))); |
10fcbe13cd19
http-client-branch: (WebSocket) from IOE to IAE in sendClose with bad reason
prappo
parents:
56337
diff
changeset
|
104 |
assertFails(IAE, webSocket.sendClose(NORMAL_CLOSURE, Support.malformedString())); |
10fcbe13cd19
http-client-branch: (WebSocket) from IOE to IAE in sendClose with bad reason
prappo
parents:
56337
diff
changeset
|
105 |
assertFails(IAE, webSocket.sendClose(NORMAL_CLOSURE, Support.incompleteString())); |
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56317
diff
changeset
|
106 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
107 |
assertFails(IAE, webSocket.sendClose(-2, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
108 |
assertFails(IAE, webSocket.sendClose(-1, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
109 |
assertFails(IAE, webSocket.sendClose(0, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
110 |
assertFails(IAE, webSocket.sendClose(1, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
111 |
assertFails(IAE, webSocket.sendClose(500, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
112 |
assertFails(IAE, webSocket.sendClose(998, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
113 |
assertFails(IAE, webSocket.sendClose(999, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
114 |
assertFails(IAE, webSocket.sendClose(1002, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
115 |
assertFails(IAE, webSocket.sendClose(1003, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
116 |
assertFails(IAE, webSocket.sendClose(1006, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
117 |
assertFails(IAE, webSocket.sendClose(1007, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
118 |
assertFails(IAE, webSocket.sendClose(1009, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
119 |
assertFails(IAE, webSocket.sendClose(1010, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
120 |
assertFails(IAE, webSocket.sendClose(1012, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
121 |
assertFails(IAE, webSocket.sendClose(1013, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
122 |
assertFails(IAE, webSocket.sendClose(1015, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
123 |
assertFails(IAE, webSocket.sendClose(5000, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
124 |
assertFails(IAE, webSocket.sendClose(32768, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
125 |
assertFails(IAE, webSocket.sendClose(65535, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
126 |
assertFails(IAE, webSocket.sendClose(65536, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
127 |
assertFails(IAE, webSocket.sendClose(Integer.MAX_VALUE, "a reason")); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
128 |
assertFails(IAE, webSocket.sendClose(Integer.MIN_VALUE, "a reason")); |
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56317
diff
changeset
|
129 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
130 |
assertThrows(IAE, () -> webSocket.request(Integer.MIN_VALUE)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
131 |
assertThrows(IAE, () -> webSocket.request(Long.MIN_VALUE)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
132 |
assertThrows(IAE, () -> webSocket.request(-1)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
133 |
assertThrows(IAE, () -> webSocket.request(0)); |
56052 | 134 |
} |
135 |
||
136 |
@Test |
|
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56317
diff
changeset
|
137 |
public void partialBinaryThenText() throws IOException { |
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
138 |
server = new DummyWebSocketServer(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
139 |
server.open(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
140 |
webSocket = newHttpClient().newWebSocketBuilder() |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
141 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
142 |
.join(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
143 |
webSocket.sendBinary(ByteBuffer.allocate(16), false).join(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
144 |
assertFails(ISE, webSocket.sendText("text", false)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
145 |
assertFails(ISE, webSocket.sendText("text", true)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
146 |
// Pings & Pongs are fine |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
147 |
webSocket.sendPing(ByteBuffer.allocate(125)).join(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
148 |
webSocket.sendPong(ByteBuffer.allocate(125)).join(); |
56052 | 149 |
} |
150 |
||
151 |
@Test |
|
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56317
diff
changeset
|
152 |
public void partialTextThenBinary() throws IOException { |
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
153 |
server = new DummyWebSocketServer(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
154 |
server.open(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
155 |
webSocket = newHttpClient().newWebSocketBuilder() |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
156 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
157 |
.join(); |
56052 | 158 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
159 |
webSocket.sendText("text", false).join(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
160 |
assertFails(ISE, webSocket.sendBinary(ByteBuffer.allocate(16), false)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
161 |
assertFails(ISE, webSocket.sendBinary(ByteBuffer.allocate(16), true)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
162 |
// Pings & Pongs are fine |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
163 |
webSocket.sendPing(ByteBuffer.allocate(125)).join(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
164 |
webSocket.sendPong(ByteBuffer.allocate(125)).join(); |
56052 | 165 |
} |
166 |
||
167 |
@Test |
|
56263 | 168 |
public void sendMethodsThrowIOE1() throws IOException { |
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
169 |
server = new DummyWebSocketServer(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
170 |
server.open(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
171 |
webSocket = newHttpClient() |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
172 |
.newWebSocketBuilder() |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
173 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
174 |
.join(); |
56052 | 175 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
176 |
webSocket.sendClose(NORMAL_CLOSURE, "ok").join(); |
56263 | 177 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
178 |
assertFails(IOE, webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "ok")); |
56052 | 179 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
180 |
assertFails(IOE, webSocket.sendText("", true)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
181 |
assertFails(IOE, webSocket.sendText("", false)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
182 |
assertFails(IOE, webSocket.sendText("abc", true)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
183 |
assertFails(IOE, webSocket.sendText("abc", false)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
184 |
assertFails(IOE, webSocket.sendBinary(ByteBuffer.allocate(0), true)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
185 |
assertFails(IOE, webSocket.sendBinary(ByteBuffer.allocate(0), false)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
186 |
assertFails(IOE, webSocket.sendBinary(ByteBuffer.allocate(1), true)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
187 |
assertFails(IOE, webSocket.sendBinary(ByteBuffer.allocate(1), false)); |
56052 | 188 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
189 |
assertFails(IOE, webSocket.sendPing(ByteBuffer.allocate(125))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
190 |
assertFails(IOE, webSocket.sendPing(ByteBuffer.allocate(124))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
191 |
assertFails(IOE, webSocket.sendPing(ByteBuffer.allocate(1))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
192 |
assertFails(IOE, webSocket.sendPing(ByteBuffer.allocate(0))); |
56052 | 193 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
194 |
assertFails(IOE, webSocket.sendPong(ByteBuffer.allocate(125))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
195 |
assertFails(IOE, webSocket.sendPong(ByteBuffer.allocate(124))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
196 |
assertFails(IOE, webSocket.sendPong(ByteBuffer.allocate(1))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
197 |
assertFails(IOE, webSocket.sendPong(ByteBuffer.allocate(0))); |
56052 | 198 |
} |
199 |
||
200 |
@Test |
|
56263 | 201 |
public void sendMethodsThrowIOE2() throws Exception { |
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
202 |
server = Support.serverWithCannedData(0x88, 0x00); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
203 |
server.open(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
204 |
CompletableFuture<Void> onCloseCalled = new CompletableFuture<>(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
205 |
CompletableFuture<Void> canClose = new CompletableFuture<>(); |
56052 | 206 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
207 |
WebSocket.Listener listener = new WebSocket.Listener() { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
208 |
@Override |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
209 |
public CompletionStage<?> onClose(WebSocket webSocket, |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
210 |
int statusCode, |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
211 |
String reason) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
212 |
System.out.printf("onClose(%s, '%s')%n", statusCode, reason); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
213 |
onCloseCalled.complete(null); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
214 |
return canClose; |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
215 |
} |
56052 | 216 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
217 |
@Override |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
218 |
public void onError(WebSocket webSocket, Throwable error) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
219 |
System.out.println("onError(" + error + ")"); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
220 |
onCloseCalled.completeExceptionally(error); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
221 |
} |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
222 |
}; |
56052 | 223 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
224 |
webSocket = newHttpClient().newWebSocketBuilder() |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
225 |
.buildAsync(server.getURI(), listener) |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
226 |
.join(); |
56263 | 227 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
228 |
onCloseCalled.join(); // Wait for onClose to be called |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
229 |
canClose.complete(null); // Signal to the WebSocket it can close the output |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
230 |
TimeUnit.SECONDS.sleep(5); // Give canClose some time to reach the WebSocket |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
231 |
|
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
232 |
assertFails(IOE, webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "ok")); |
56052 | 233 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
234 |
assertFails(IOE, webSocket.sendText("", true)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
235 |
assertFails(IOE, webSocket.sendText("", false)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
236 |
assertFails(IOE, webSocket.sendText("abc", true)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
237 |
assertFails(IOE, webSocket.sendText("abc", false)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
238 |
assertFails(IOE, webSocket.sendBinary(ByteBuffer.allocate(0), true)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
239 |
assertFails(IOE, webSocket.sendBinary(ByteBuffer.allocate(0), false)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
240 |
assertFails(IOE, webSocket.sendBinary(ByteBuffer.allocate(1), true)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
241 |
assertFails(IOE, webSocket.sendBinary(ByteBuffer.allocate(1), false)); |
56052 | 242 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
243 |
assertFails(IOE, webSocket.sendPing(ByteBuffer.allocate(125))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
244 |
assertFails(IOE, webSocket.sendPing(ByteBuffer.allocate(124))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
245 |
assertFails(IOE, webSocket.sendPing(ByteBuffer.allocate(1))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
246 |
assertFails(IOE, webSocket.sendPing(ByteBuffer.allocate(0))); |
56052 | 247 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
248 |
assertFails(IOE, webSocket.sendPong(ByteBuffer.allocate(125))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
249 |
assertFails(IOE, webSocket.sendPong(ByteBuffer.allocate(124))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
250 |
assertFails(IOE, webSocket.sendPong(ByteBuffer.allocate(1))); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
251 |
assertFails(IOE, webSocket.sendPong(ByteBuffer.allocate(0))); |
56052 | 252 |
} |
56058 | 253 |
|
254 |
@Test |
|
56263 | 255 |
public void simpleAggregatingBinaryMessages() throws IOException { |
256 |
List<byte[]> expected = List.of("alpha", "beta", "gamma", "delta") |
|
257 |
.stream() |
|
258 |
.map(s -> s.getBytes(StandardCharsets.US_ASCII)) |
|
259 |
.collect(Collectors.toList()); |
|
260 |
int[] binary = new int[]{ |
|
261 |
0x82, 0x05, 0x61, 0x6c, 0x70, 0x68, 0x61, // [alpha] |
|
262 |
0x02, 0x02, 0x62, 0x65, // [be |
|
263 |
0x80, 0x02, 0x74, 0x61, // ta] |
|
264 |
0x02, 0x01, 0x67, // [g |
|
265 |
0x00, 0x01, 0x61, // a |
|
266 |
0x00, 0x00, // |
|
267 |
0x00, 0x00, // |
|
268 |
0x00, 0x01, 0x6d, // m |
|
269 |
0x00, 0x01, 0x6d, // m |
|
270 |
0x80, 0x01, 0x61, // a] |
|
271 |
0x8a, 0x00, // <PONG> |
|
272 |
0x02, 0x04, 0x64, 0x65, 0x6c, 0x74, // [delt |
|
273 |
0x00, 0x01, 0x61, // a |
|
274 |
0x80, 0x00, // ] |
|
275 |
0x88, 0x00 // <CLOSE> |
|
276 |
}; |
|
277 |
CompletableFuture<List<byte[]>> actual = new CompletableFuture<>(); |
|
278 |
||
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
279 |
server = Support.serverWithCannedData(binary); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
280 |
server.open(); |
56263 | 281 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
282 |
WebSocket.Listener listener = new WebSocket.Listener() { |
56263 | 283 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
284 |
List<byte[]> collectedBytes = new ArrayList<>(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
285 |
ByteBuffer buffer = ByteBuffer.allocate(1024); |
56263 | 286 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
287 |
@Override |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
288 |
public CompletionStage<?> onBinary(WebSocket webSocket, |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
289 |
ByteBuffer message, |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
290 |
boolean last) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
291 |
System.out.printf("onBinary(%s, %s)%n", message, last); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
292 |
webSocket.request(1); |
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
293 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
294 |
append(message); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
295 |
if (last) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
296 |
buffer.flip(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
297 |
byte[] bytes = new byte[buffer.remaining()]; |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
298 |
buffer.get(bytes); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
299 |
buffer.clear(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
300 |
processWholeBinary(bytes); |
56263 | 301 |
} |
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
302 |
return null; |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
303 |
} |
56263 | 304 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
305 |
private void append(ByteBuffer message) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
306 |
if (buffer.remaining() < message.remaining()) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
307 |
assert message.remaining() > 0; |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
308 |
int cap = (buffer.capacity() + message.remaining()) * 2; |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
309 |
ByteBuffer b = ByteBuffer.allocate(cap); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
310 |
b.put(buffer.flip()); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
311 |
buffer = b; |
56263 | 312 |
} |
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
313 |
buffer.put(message); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
314 |
} |
56263 | 315 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
316 |
private void processWholeBinary(byte[] bytes) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
317 |
String stringBytes = new String(bytes, StandardCharsets.UTF_8); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
318 |
System.out.println("processWholeBinary: " + stringBytes); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
319 |
collectedBytes.add(bytes); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
320 |
} |
56263 | 321 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
322 |
@Override |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
323 |
public CompletionStage<?> onClose(WebSocket webSocket, |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
324 |
int statusCode, |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
325 |
String reason) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
326 |
actual.complete(collectedBytes); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
327 |
return null; |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
328 |
} |
56263 | 329 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
330 |
@Override |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
331 |
public void onError(WebSocket webSocket, Throwable error) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
332 |
actual.completeExceptionally(error); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
333 |
} |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
334 |
}; |
56263 | 335 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
336 |
webSocket = newHttpClient().newWebSocketBuilder() |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
337 |
.buildAsync(server.getURI(), listener) |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
338 |
.join(); |
56263 | 339 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
340 |
List<byte[]> a = actual.join(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
341 |
assertEquals(a, expected); |
56263 | 342 |
} |
343 |
||
344 |
@Test |
|
345 |
public void simpleAggregatingTextMessages() throws IOException { |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
346 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
347 |
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
|
348 |
|
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
349 |
int[] binary = new int[]{ |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
350 |
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
|
351 |
0x01, 0x02, 0x62, 0x65, // "be |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
352 |
0x80, 0x02, 0x74, 0x61, // ta" |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
353 |
0x01, 0x01, 0x67, // "g |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
354 |
0x00, 0x01, 0x61, // a |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
355 |
0x00, 0x00, // |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
356 |
0x00, 0x00, // |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
357 |
0x00, 0x01, 0x6d, // m |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
358 |
0x00, 0x01, 0x6d, // m |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
359 |
0x80, 0x01, 0x61, // a" |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
360 |
0x8a, 0x00, // <PONG> |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
361 |
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
|
362 |
0x00, 0x01, 0x61, // a |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
363 |
0x80, 0x00, // " |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
364 |
0x88, 0x00 // <CLOSE> |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
365 |
}; |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
366 |
CompletableFuture<List<String>> actual = new CompletableFuture<>(); |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
367 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
368 |
server = Support.serverWithCannedData(binary); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
369 |
server.open(); |
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
370 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
371 |
WebSocket.Listener listener = new WebSocket.Listener() { |
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
372 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
373 |
List<String> collectedStrings = new ArrayList<>(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
374 |
StringBuilder text = new StringBuilder(); |
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
375 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
376 |
@Override |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
377 |
public CompletionStage<?> onText(WebSocket webSocket, |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
378 |
CharSequence message, |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
379 |
boolean last) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
380 |
System.out.printf("onText(%s, %s)%n", message, last); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
381 |
webSocket.request(1); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
382 |
text.append(message); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
383 |
if (last) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
384 |
String str = text.toString(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
385 |
text.setLength(0); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
386 |
processWholeText(str); |
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
387 |
} |
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
388 |
return null; |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
389 |
} |
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
390 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
391 |
private void processWholeText(String string) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
392 |
System.out.println(string); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
393 |
collectedStrings.add(string); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
394 |
} |
56130
c99ed32a4d12
http-client-branch: (WebSocket) fixing sloppy commit
prappo
parents:
56129
diff
changeset
|
395 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
396 |
@Override |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
397 |
public CompletionStage<?> onClose(WebSocket webSocket, |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
398 |
int statusCode, |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
399 |
String reason) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
400 |
actual.complete(collectedStrings); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
401 |
return null; |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
402 |
} |
56263 | 403 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
404 |
@Override |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
405 |
public void onError(WebSocket webSocket, Throwable error) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
406 |
actual.completeExceptionally(error); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
407 |
} |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
408 |
}; |
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
409 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
410 |
webSocket = newHttpClient().newWebSocketBuilder() |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
411 |
.buildAsync(server.getURI(), listener) |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
412 |
.join(); |
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
413 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
414 |
List<String> a = actual.join(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
415 |
assertEquals(a, expected); |
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
416 |
} |
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
417 |
|
56263 | 418 |
/* |
419 |
* Exercises the scenario where requests for more messages are made prior to |
|
420 |
* completing the returned CompletionStage instances. |
|
421 |
*/ |
|
56129
fe88abe462c9
http-client-branch: (WebSocket) added test which aggregates text messages through StringBuilder
prappo
parents:
56089
diff
changeset
|
422 |
@Test |
56263 | 423 |
public void aggregatingTextMessages() throws IOException { |
56058 | 424 |
|
425 |
List<String> expected = List.of("alpha", "beta", "gamma", "delta"); |
|
426 |
||
427 |
int[] binary = new int[]{ |
|
428 |
0x81, 0x05, 0x61, 0x6c, 0x70, 0x68, 0x61, // "alpha" |
|
429 |
0x01, 0x02, 0x62, 0x65, // "be |
|
430 |
0x80, 0x02, 0x74, 0x61, // ta" |
|
431 |
0x01, 0x01, 0x67, // "g |
|
432 |
0x00, 0x01, 0x61, // a |
|
433 |
0x00, 0x00, // |
|
434 |
0x00, 0x00, // |
|
435 |
0x00, 0x01, 0x6d, // m |
|
436 |
0x00, 0x01, 0x6d, // m |
|
437 |
0x80, 0x01, 0x61, // a" |
|
438 |
0x8a, 0x00, // <PONG> |
|
439 |
0x01, 0x04, 0x64, 0x65, 0x6c, 0x74, // "delt |
|
440 |
0x00, 0x01, 0x61, // a |
|
441 |
0x80, 0x00, // " |
|
442 |
0x88, 0x00 // <CLOSE> |
|
443 |
}; |
|
444 |
CompletableFuture<List<String>> actual = new CompletableFuture<>(); |
|
445 |
||
446 |
||
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
447 |
server = Support.serverWithCannedData(binary); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
448 |
server.open(); |
56058 | 449 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
450 |
WebSocket.Listener listener = new WebSocket.Listener() { |
56058 | 451 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
452 |
List<CharSequence> parts = new ArrayList<>(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
453 |
/* |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
454 |
* A CompletableFuture which will complete once the current |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
455 |
* message has been fully assembled. Until then the listener |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
456 |
* returns this instance for every call. |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
457 |
*/ |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
458 |
CompletableFuture<?> currentCf = new CompletableFuture<>(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
459 |
List<String> collected = new ArrayList<>(); |
56058 | 460 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
461 |
@Override |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
462 |
public CompletionStage<?> onText(WebSocket webSocket, |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
463 |
CharSequence message, |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
464 |
boolean last) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
465 |
parts.add(message); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
466 |
if (!last) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
467 |
webSocket.request(1); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
468 |
} else { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
469 |
this.currentCf.thenRun(() -> webSocket.request(1)); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
470 |
CompletableFuture<?> refCf = this.currentCf; |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
471 |
processWholeMessage(new ArrayList<>(parts), refCf); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
472 |
currentCf = new CompletableFuture<>(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
473 |
parts.clear(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
474 |
return refCf; |
56058 | 475 |
} |
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
476 |
return currentCf; |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
477 |
} |
56263 | 478 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
479 |
@Override |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
480 |
public CompletionStage<?> onClose(WebSocket webSocket, |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
481 |
int statusCode, |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
482 |
String reason) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
483 |
actual.complete(collected); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
484 |
return null; |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
485 |
} |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
486 |
|
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
487 |
@Override |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
488 |
public void onError(WebSocket webSocket, Throwable error) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
489 |
actual.completeExceptionally(error); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
490 |
} |
56263 | 491 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
492 |
public void processWholeMessage(List<CharSequence> data, |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
493 |
CompletableFuture<?> cf) { |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
494 |
StringBuilder b = new StringBuilder(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
495 |
data.forEach(b::append); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
496 |
String s = b.toString(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
497 |
System.out.println(s); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
498 |
cf.complete(null); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
499 |
collected.add(s); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
500 |
} |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
501 |
}; |
56058 | 502 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
503 |
webSocket = newHttpClient().newWebSocketBuilder() |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
504 |
.buildAsync(server.getURI(), listener) |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
505 |
.join(); |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56294
diff
changeset
|
506 |
|
56324
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
507 |
List<String> a = actual.join(); |
3edf200fff01
http-client-branch: (WebSocket) full-duplex DummyWebSocketServer; tests stabilization & fixes
prappo
parents:
56323
diff
changeset
|
508 |
assertEquals(a, expected); |
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56317
diff
changeset
|
509 |
} |
56052 | 510 |
} |