author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 58289 | 3a79d4cccbcb |
permissions | -rw-r--r-- |
49765 | 1 |
/* |
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
49765 | 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 |
|
27 |
* @run testng/othervm |
|
28 |
* -Djdk.internal.httpclient.websocket.debug=true |
|
29 |
* Abort |
|
30 |
*/ |
|
31 |
||
32 |
import org.testng.annotations.Test; |
|
33 |
||
34 |
import java.io.IOException; |
|
35 |
import java.net.ProtocolException; |
|
36 |
import java.net.http.WebSocket; |
|
37 |
import java.nio.ByteBuffer; |
|
38 |
import java.util.Arrays; |
|
39 |
import java.util.List; |
|
40 |
import java.util.concurrent.CompletableFuture; |
|
41 |
import java.util.concurrent.CompletionStage; |
|
42 |
import java.util.concurrent.TimeUnit; |
|
43 |
import java.util.concurrent.TimeoutException; |
|
44 |
||
45 |
import static java.net.http.HttpClient.newHttpClient; |
|
46 |
import static java.net.http.WebSocket.NORMAL_CLOSURE; |
|
47 |
import static org.testng.Assert.assertEquals; |
|
48 |
import static org.testng.Assert.assertThrows; |
|
49 |
import static org.testng.Assert.assertTrue; |
|
50 |
import static org.testng.Assert.fail; |
|
51 |
||
52 |
public class Abort { |
|
53 |
||
54 |
private static final Class<NullPointerException> NPE = NullPointerException.class; |
|
55 |
private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class; |
|
56 |
private static final Class<IOException> IOE = IOException.class; |
|
57 |
||
58 |
||
59 |
@Test |
|
60 |
public void onOpenThenAbort() throws Exception { |
|
61 |
int[] bytes = new int[]{ |
|
62 |
0x88, 0x00, // opcode=close |
|
63 |
}; |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
64 |
try (var server = Support.serverWithCannedData(bytes)) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
65 |
server.open(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
66 |
// messages are available |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
67 |
MockListener listener = new MockListener() { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
68 |
@Override |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
69 |
protected void onOpen0(WebSocket webSocket) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
70 |
// unbounded request |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
71 |
webSocket.request(Long.MAX_VALUE); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
72 |
webSocket.abort(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
73 |
} |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
74 |
}; |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
75 |
var webSocket = newHttpClient().newWebSocketBuilder() |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
76 |
.buildAsync(server.getURI(), listener) |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
77 |
.join(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
78 |
try { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
79 |
TimeUnit.SECONDS.sleep(5); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
80 |
List<MockListener.Invocation> inv = listener.invocationsSoFar(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
81 |
// no more invocations after onOpen as WebSocket was aborted |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
82 |
assertEquals(inv, List.of(MockListener.Invocation.onOpen(webSocket))); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
83 |
} finally { |
49765 | 84 |
webSocket.abort(); |
85 |
} |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
86 |
} |
49765 | 87 |
} |
88 |
||
89 |
@Test |
|
90 |
public void onOpenThenOnTextThenAbort() throws Exception { |
|
91 |
int[] bytes = new int[]{ |
|
92 |
0x81, 0x00, // opcode=text, fin=true |
|
93 |
0x88, 0x00, // opcode=close |
|
94 |
}; |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
95 |
try (var server = Support.serverWithCannedData(bytes)) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
96 |
server.open(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
97 |
MockListener listener = new MockListener() { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
98 |
@Override |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
99 |
protected void onOpen0(WebSocket webSocket) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
100 |
// unbounded request |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
101 |
webSocket.request(Long.MAX_VALUE); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
102 |
} |
49765 | 103 |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
104 |
@Override |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
105 |
protected CompletionStage<?> onText0(WebSocket webSocket, |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
106 |
CharSequence message, |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
107 |
boolean last) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
108 |
webSocket.abort(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
109 |
return super.onText0(webSocket, message, last); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
110 |
} |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
111 |
}; |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
112 |
var webSocket = newHttpClient().newWebSocketBuilder() |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
113 |
.buildAsync(server.getURI(), listener) |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
114 |
.join(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
115 |
try { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
116 |
TimeUnit.SECONDS.sleep(5); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
117 |
List<MockListener.Invocation> inv = listener.invocationsSoFar(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
118 |
// no more invocations after onOpen, onBinary as WebSocket was aborted |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
119 |
List<MockListener.Invocation> expected = List.of( |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
120 |
MockListener.Invocation.onOpen(webSocket), |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
121 |
MockListener.Invocation.onText(webSocket, "", true)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
122 |
assertEquals(inv, expected); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
123 |
} finally { |
49765 | 124 |
webSocket.abort(); |
125 |
} |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
126 |
} |
49765 | 127 |
} |
128 |
||
129 |
@Test |
|
130 |
public void onOpenThenOnBinaryThenAbort() throws Exception { |
|
131 |
int[] bytes = new int[]{ |
|
132 |
0x82, 0x00, // opcode=binary, fin=true |
|
133 |
0x88, 0x00, // opcode=close |
|
134 |
}; |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
135 |
try (var server = Support.serverWithCannedData(bytes)) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
136 |
server.open(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
137 |
MockListener listener = new MockListener() { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
138 |
@Override |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
139 |
protected void onOpen0(WebSocket webSocket) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
140 |
// unbounded request |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
141 |
webSocket.request(Long.MAX_VALUE); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
142 |
} |
49765 | 143 |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
144 |
@Override |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
145 |
protected CompletionStage<?> onBinary0(WebSocket webSocket, |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
146 |
ByteBuffer message, |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
147 |
boolean last) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
148 |
webSocket.abort(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
149 |
return super.onBinary0(webSocket, message, last); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
150 |
} |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
151 |
}; |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
152 |
var webSocket = newHttpClient().newWebSocketBuilder() |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
153 |
.buildAsync(server.getURI(), listener) |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
154 |
.join(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
155 |
try { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
156 |
TimeUnit.SECONDS.sleep(5); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
157 |
List<MockListener.Invocation> inv = listener.invocationsSoFar(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
158 |
// no more invocations after onOpen, onBinary as WebSocket was aborted |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
159 |
List<MockListener.Invocation> expected = List.of( |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
160 |
MockListener.Invocation.onOpen(webSocket), |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
161 |
MockListener.Invocation.onBinary(webSocket, ByteBuffer.allocate(0), true)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
162 |
assertEquals(inv, expected); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
163 |
} finally { |
49765 | 164 |
webSocket.abort(); |
165 |
} |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
166 |
} |
49765 | 167 |
} |
168 |
||
169 |
@Test |
|
170 |
public void onOpenThenOnPingThenAbort() throws Exception { |
|
171 |
int[] bytes = { |
|
172 |
0x89, 0x00, // opcode=ping |
|
173 |
0x88, 0x00, // opcode=close |
|
174 |
}; |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
175 |
try (var server = Support.serverWithCannedData(bytes)) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
176 |
server.open(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
177 |
MockListener listener = new MockListener() { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
178 |
@Override |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
179 |
protected void onOpen0(WebSocket webSocket) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
180 |
// unbounded request |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
181 |
webSocket.request(Long.MAX_VALUE); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
182 |
} |
49765 | 183 |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
184 |
@Override |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
185 |
protected CompletionStage<?> onPing0(WebSocket webSocket, |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
186 |
ByteBuffer message) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
187 |
webSocket.abort(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
188 |
return super.onPing0(webSocket, message); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
189 |
} |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
190 |
}; |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
191 |
var webSocket = newHttpClient().newWebSocketBuilder() |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
192 |
.buildAsync(server.getURI(), listener) |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
193 |
.join(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
194 |
try { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
195 |
TimeUnit.SECONDS.sleep(5); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
196 |
List<MockListener.Invocation> inv = listener.invocationsSoFar(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
197 |
// no more invocations after onOpen, onPing as WebSocket was aborted |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
198 |
List<MockListener.Invocation> expected = List.of( |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
199 |
MockListener.Invocation.onOpen(webSocket), |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
200 |
MockListener.Invocation.onPing(webSocket, ByteBuffer.allocate(0))); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
201 |
assertEquals(inv, expected); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
202 |
} finally { |
49765 | 203 |
webSocket.abort(); |
204 |
} |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
205 |
} |
49765 | 206 |
} |
207 |
||
208 |
@Test |
|
209 |
public void onOpenThenOnPongThenAbort() throws Exception { |
|
210 |
int[] bytes = { |
|
211 |
0x8a, 0x00, // opcode=pong |
|
212 |
0x88, 0x00, // opcode=close |
|
213 |
}; |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
214 |
try (var server = Support.serverWithCannedData(bytes)) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
215 |
server.open(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
216 |
MockListener listener = new MockListener() { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
217 |
@Override |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
218 |
protected void onOpen0(WebSocket webSocket) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
219 |
// unbounded request |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
220 |
webSocket.request(Long.MAX_VALUE); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
221 |
} |
49765 | 222 |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
223 |
@Override |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
224 |
protected CompletionStage<?> onPong0(WebSocket webSocket, |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
225 |
ByteBuffer message) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
226 |
webSocket.abort(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
227 |
return super.onPong0(webSocket, message); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
228 |
} |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
229 |
}; |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
230 |
var webSocket = newHttpClient().newWebSocketBuilder() |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
231 |
.buildAsync(server.getURI(), listener) |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
232 |
.join(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
233 |
try { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
234 |
TimeUnit.SECONDS.sleep(5); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
235 |
List<MockListener.Invocation> inv = listener.invocationsSoFar(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
236 |
// no more invocations after onOpen, onPong as WebSocket was aborted |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
237 |
List<MockListener.Invocation> expected = List.of( |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
238 |
MockListener.Invocation.onOpen(webSocket), |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
239 |
MockListener.Invocation.onPong(webSocket, ByteBuffer.allocate(0))); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
240 |
assertEquals(inv, expected); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
241 |
} finally { |
49765 | 242 |
webSocket.abort(); |
243 |
} |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
244 |
} |
49765 | 245 |
} |
246 |
||
247 |
@Test |
|
248 |
public void onOpenThenOnCloseThenAbort() throws Exception { |
|
249 |
int[] bytes = { |
|
250 |
0x88, 0x00, // opcode=close |
|
251 |
0x8a, 0x00, // opcode=pong |
|
252 |
}; |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
253 |
try (var server = Support.serverWithCannedData(bytes)) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
254 |
server.open(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
255 |
MockListener listener = new MockListener() { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
256 |
@Override |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
257 |
protected void onOpen0(WebSocket webSocket) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
258 |
// unbounded request |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
259 |
webSocket.request(Long.MAX_VALUE); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
260 |
} |
49765 | 261 |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
262 |
@Override |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
263 |
protected CompletionStage<?> onClose0(WebSocket webSocket, |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
264 |
int statusCode, |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
265 |
String reason) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
266 |
webSocket.abort(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
267 |
return super.onClose0(webSocket, statusCode, reason); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
268 |
} |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
269 |
}; |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
270 |
var webSocket = newHttpClient().newWebSocketBuilder() |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
271 |
.buildAsync(server.getURI(), listener) |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
272 |
.join(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
273 |
try { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
274 |
TimeUnit.SECONDS.sleep(5); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
275 |
List<MockListener.Invocation> inv = listener.invocationsSoFar(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
276 |
// no more invocations after onOpen, onClose |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
277 |
List<MockListener.Invocation> expected = List.of( |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
278 |
MockListener.Invocation.onOpen(webSocket), |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
279 |
MockListener.Invocation.onClose(webSocket, 1005, "")); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
280 |
assertEquals(inv, expected); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
281 |
} finally { |
49765 | 282 |
webSocket.abort(); |
283 |
} |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
284 |
} |
49765 | 285 |
} |
286 |
||
287 |
@Test |
|
288 |
public void onOpenThenOnErrorThenAbort() throws Exception { |
|
289 |
// A header of 128 bytes long Ping (which is a protocol error) |
|
290 |
int[] badPingHeader = new int[]{0x89, 0x7e, 0x00, 0x80}; |
|
291 |
int[] closeMessage = new int[]{0x88, 0x00}; |
|
292 |
int[] bytes = new int[badPingHeader.length + 128 + closeMessage.length]; |
|
293 |
System.arraycopy(badPingHeader, 0, bytes, 0, badPingHeader.length); |
|
294 |
System.arraycopy(closeMessage, 0, bytes, badPingHeader.length + 128, closeMessage.length); |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
295 |
try (var server = Support.serverWithCannedData(bytes)) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
296 |
server.open(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
297 |
MockListener listener = new MockListener() { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
298 |
@Override |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
299 |
protected void onOpen0(WebSocket webSocket) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
300 |
// unbounded request |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
301 |
webSocket.request(Long.MAX_VALUE); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
302 |
} |
49765 | 303 |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
304 |
@Override |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
305 |
protected void onError0(WebSocket webSocket, Throwable error) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
306 |
webSocket.abort(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
307 |
super.onError0(webSocket, error); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
308 |
} |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
309 |
}; |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
310 |
var webSocket = newHttpClient().newWebSocketBuilder() |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
311 |
.buildAsync(server.getURI(), listener) |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
312 |
.join(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
313 |
try { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
314 |
TimeUnit.SECONDS.sleep(5); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
315 |
List<MockListener.Invocation> inv = listener.invocationsSoFar(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
316 |
// no more invocations after onOpen, onError |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
317 |
List<MockListener.Invocation> expected = List.of( |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
318 |
MockListener.Invocation.onOpen(webSocket), |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
319 |
MockListener.Invocation.onError(webSocket, ProtocolException.class)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
320 |
System.out.println("actual invocations:" + Arrays.toString(inv.toArray())); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
321 |
assertEquals(inv, expected); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
322 |
} finally { |
49765 | 323 |
webSocket.abort(); |
324 |
} |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
325 |
} |
49765 | 326 |
} |
327 |
||
328 |
@Test |
|
329 |
public void immediateAbort() throws Exception { |
|
330 |
CompletableFuture<Void> messageReceived = new CompletableFuture<>(); |
|
331 |
WebSocket.Listener listener = new WebSocket.Listener() { |
|
332 |
||
333 |
@Override |
|
334 |
public void onOpen(WebSocket webSocket) { |
|
335 |
/* no initial request */ |
|
336 |
} |
|
337 |
||
338 |
@Override |
|
339 |
public CompletionStage<?> onText(WebSocket webSocket, |
|
340 |
CharSequence message, |
|
341 |
boolean last) { |
|
342 |
messageReceived.complete(null); |
|
343 |
return null; |
|
344 |
} |
|
345 |
||
346 |
@Override |
|
347 |
public CompletionStage<?> onBinary(WebSocket webSocket, |
|
348 |
ByteBuffer message, |
|
349 |
boolean last) { |
|
350 |
messageReceived.complete(null); |
|
351 |
return null; |
|
352 |
} |
|
353 |
||
354 |
@Override |
|
355 |
public CompletionStage<?> onPing(WebSocket webSocket, |
|
356 |
ByteBuffer message) { |
|
357 |
messageReceived.complete(null); |
|
358 |
return null; |
|
359 |
} |
|
360 |
||
361 |
@Override |
|
362 |
public CompletionStage<?> onPong(WebSocket webSocket, |
|
363 |
ByteBuffer message) { |
|
364 |
messageReceived.complete(null); |
|
365 |
return null; |
|
366 |
} |
|
367 |
||
368 |
@Override |
|
369 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
370 |
int statusCode, |
|
371 |
String reason) { |
|
372 |
messageReceived.complete(null); |
|
373 |
return null; |
|
374 |
} |
|
375 |
}; |
|
376 |
||
377 |
int[] bytes = new int[]{ |
|
378 |
0x82, 0x00, // opcode=binary, fin=true |
|
379 |
0x88, 0x00, // opcode=close |
|
380 |
}; |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
381 |
try (var server = Support.serverWithCannedData(bytes)) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
382 |
server.open(); |
49765 | 383 |
|
58289
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
384 |
WebSocket ws = newHttpClient() |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
385 |
.newWebSocketBuilder() |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
386 |
.buildAsync(server.getURI(), listener) |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
387 |
.join(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
388 |
try { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
389 |
for (int i = 0; i < 3; i++) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
390 |
System.out.printf("iteration #%s%n", i); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
391 |
// after the first abort() each consecutive one must be a no-op, |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
392 |
// moreover, query methods should continue to return consistent |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
393 |
// values |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
394 |
for (int j = 0; j < 3; j++) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
395 |
System.out.printf("abort #%s%n", j); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
396 |
ws.abort(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
397 |
assertTrue(ws.isInputClosed()); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
398 |
assertTrue(ws.isOutputClosed()); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
399 |
assertEquals(ws.getSubprotocol(), ""); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
400 |
} |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
401 |
// at this point valid requests MUST be a no-op: |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
402 |
for (int j = 0; j < 3; j++) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
403 |
System.out.printf("request #%s%n", j); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
404 |
ws.request(1); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
405 |
ws.request(2); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
406 |
ws.request(8); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
407 |
ws.request(Integer.MAX_VALUE); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
408 |
ws.request(Long.MAX_VALUE); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
409 |
// invalid requests MUST throw IAE: |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
410 |
assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
411 |
assertThrows(IAE, () -> ws.request(Long.MIN_VALUE)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
412 |
assertThrows(IAE, () -> ws.request(-1)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
413 |
assertThrows(IAE, () -> ws.request(0)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
414 |
} |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
415 |
} |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
416 |
// even though there is a bunch of messages readily available on the |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
417 |
// wire we shouldn't have received any of them as we aborted before |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
418 |
// the first request |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
419 |
try { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
420 |
messageReceived.get(5, TimeUnit.SECONDS); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
421 |
fail(); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
422 |
} catch (TimeoutException expected) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
423 |
System.out.println("Finished waiting"); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
424 |
} |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
425 |
for (int i = 0; i < 3; i++) { |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
426 |
System.out.printf("send #%s%n", i); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
427 |
Support.assertFails(IOE, ws.sendText("text!", false)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
428 |
Support.assertFails(IOE, ws.sendText("text!", true)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
429 |
Support.assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), false)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
430 |
Support.assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), true)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
431 |
Support.assertFails(IOE, ws.sendPing(ByteBuffer.allocate(16))); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
432 |
Support.assertFails(IOE, ws.sendPong(ByteBuffer.allocate(16))); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
433 |
Support.assertFails(IOE, ws.sendClose(NORMAL_CLOSURE, "a reason")); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
434 |
assertThrows(NPE, () -> ws.sendText(null, false)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
435 |
assertThrows(NPE, () -> ws.sendText(null, true)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
436 |
assertThrows(NPE, () -> ws.sendBinary(null, false)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
437 |
assertThrows(NPE, () -> ws.sendBinary(null, true)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
438 |
assertThrows(NPE, () -> ws.sendPing(null)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
439 |
assertThrows(NPE, () -> ws.sendPong(null)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
440 |
assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null)); |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
441 |
} |
3a79d4cccbcb
8217825: Verify @AfterTest is used correctly in WebSocket tests
pconcannon
parents:
49765
diff
changeset
|
442 |
} finally { |
49765 | 443 |
ws.abort(); |
444 |
} |
|
445 |
} |
|
446 |
} |
|
447 |
} |