--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/net/httpclient/websocket/ImmediateAbort.java Sat Mar 17 18:01:01 2018 +0000
@@ -0,0 +1,193 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @build DummyWebSocketServer
+ * @run testng/othervm
+ * -Djdk.internal.httpclient.websocket.debug=true
+ * ImmediateAbort
+ */
+
+import java.io.IOException;
+import java.net.http.WebSocket;
+import java.nio.ByteBuffer;
+import java.nio.channels.SocketChannel;
+import java.util.Arrays;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.testng.annotations.Test;
+import static java.net.http.HttpClient.newHttpClient;
+import static java.net.http.WebSocket.NORMAL_CLOSURE;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertThrows;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
+public class ImmediateAbort {
+
+ private static final Class<NullPointerException> NPE = NullPointerException.class;
+ private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
+ private static final Class<IOException> IOE = IOException.class;
+
+ /*
+ * Examines WebSocket behaviour after a call to abort()
+ */
+ @Test
+ public void immediateAbort() throws Exception {
+ try (DummyWebSocketServer server = serverWithCannedData(0x81, 0x00, 0x88, 0x00)) {
+ server.open();
+ CompletableFuture<Void> messageReceived = new CompletableFuture<>();
+ WebSocket.Listener listener = new WebSocket.Listener() {
+
+ @Override
+ public void onOpen(WebSocket webSocket) {
+ /* no initial request */
+ }
+
+ @Override
+ public CompletionStage<?> onText(WebSocket webSocket,
+ CharSequence message,
+ WebSocket.MessagePart part) {
+ messageReceived.complete(null);
+ return null;
+ }
+
+ @Override
+ public CompletionStage<?> onBinary(WebSocket webSocket,
+ ByteBuffer message,
+ WebSocket.MessagePart part) {
+ messageReceived.complete(null);
+ return null;
+ }
+
+ @Override
+ public CompletionStage<?> onPing(WebSocket webSocket,
+ ByteBuffer message) {
+ messageReceived.complete(null);
+ return null;
+ }
+
+ @Override
+ public CompletionStage<?> onPong(WebSocket webSocket,
+ ByteBuffer message) {
+ messageReceived.complete(null);
+ return null;
+ }
+
+ @Override
+ public CompletionStage<?> onClose(WebSocket webSocket,
+ int statusCode,
+ String reason) {
+ messageReceived.complete(null);
+ return null;
+ }
+ };
+
+ WebSocket ws = newHttpClient()
+ .newWebSocketBuilder()
+ .buildAsync(server.getURI(), listener)
+ .join();
+ for (int i = 0; i < 3; i++) {
+ System.out.printf("iteration #%s%n", i);
+ // after the first abort() each consecutive one must be a no-op,
+ // moreover, query methods should continue to return consistent,
+ // permanent values
+ for (int j = 0; j < 3; j++) {
+ System.out.printf("abort #%s%n", j);
+ ws.abort();
+ assertTrue(ws.isInputClosed());
+ assertTrue(ws.isOutputClosed());
+ assertEquals(ws.getSubprotocol(), "");
+ }
+ // at this point valid requests MUST be a no-op:
+ for (int j = 0; j < 3; j++) {
+ System.out.printf("request #%s%n", j);
+ ws.request(1);
+ ws.request(2);
+ ws.request(8);
+ ws.request(Integer.MAX_VALUE);
+ ws.request(Long.MAX_VALUE);
+ // invalid requests MUST throw IAE:
+ assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE));
+ assertThrows(IAE, () -> ws.request(Long.MIN_VALUE));
+ assertThrows(IAE, () -> ws.request(-1));
+ assertThrows(IAE, () -> ws.request(0));
+ }
+ }
+ // even though there is a bunch of messages readily available on the
+ // wire we shouldn't have received any of them as we aborted before
+ // the first request
+ try {
+ messageReceived.get(10, TimeUnit.SECONDS);
+ fail();
+ } catch (TimeoutException expected) {
+ System.out.println("Finished waiting");
+ }
+ for (int i = 0; i < 3; i++) {
+ System.out.printf("send #%s%n", i);
+ assertFails(IOE, ws.sendText("text!", false));
+ assertFails(IOE, ws.sendText("text!", true));
+ assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), false));
+ assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), true));
+ assertFails(IOE, ws.sendPing(ByteBuffer.allocate(16)));
+ assertFails(IOE, ws.sendPong(ByteBuffer.allocate(16)));
+ assertFails(IOE, ws.sendClose(NORMAL_CLOSURE, "a reason"));
+ assertThrows(NPE, () -> ws.sendText(null, false));
+ assertThrows(NPE, () -> ws.sendText(null, true));
+ assertThrows(NPE, () -> ws.sendBinary(null, false));
+ assertThrows(NPE, () -> ws.sendBinary(null, true));
+ assertThrows(NPE, () -> ws.sendPing(null));
+ assertThrows(NPE, () -> ws.sendPong(null));
+ assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null));
+ }
+ }
+ }
+
+ private static void assertFails(Class<? extends Throwable> clazz,
+ CompletionStage<?> stage) {
+ Support.assertCompletesExceptionally(clazz, stage);
+ }
+
+ private static DummyWebSocketServer serverWithCannedData(int... data) {
+ byte[] copy = new byte[data.length];
+ for (int i = 0; i < data.length; i++) {
+ copy[i] = (byte) data[i];
+ }
+ return serverWithCannedData(copy);
+ }
+
+ private static DummyWebSocketServer serverWithCannedData(byte... data) {
+ byte[] copy = Arrays.copyOf(data, data.length);
+ return new DummyWebSocketServer() {
+ @Override
+ protected void serve(SocketChannel channel) throws IOException {
+ ByteBuffer closeMessage = ByteBuffer.wrap(copy);
+ channel.write(closeMessage);
+ super.serve(channel);
+ }
+ };
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/net/httpclient/websocket/SendTest.java Sat Mar 17 18:01:01 2018 +0000
@@ -0,0 +1,288 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @build DummyWebSocketServer
+ * @run testng/othervm
+ * -Djdk.internal.httpclient.websocket.debug=true
+ * SendTest
+ */
+
+import java.io.IOException;
+import java.net.http.WebSocket;
+import java.nio.ByteBuffer;
+import java.nio.channels.SocketChannel;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.testng.annotations.Test;
+import static java.net.http.HttpClient.newHttpClient;
+import static java.net.http.WebSocket.NORMAL_CLOSURE;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertThrows;
+import static org.testng.Assert.assertTrue;
+
+public class SendTest {
+
+ private static final Class<NullPointerException> NPE = NullPointerException.class;
+
+ /* shortcut */
+ private static void assertFails(Class<? extends Throwable> clazz,
+ CompletionStage<?> stage) {
+ Support.assertCompletesExceptionally(clazz, stage);
+ }
+
+ private static DummyWebSocketServer serverWithCannedData(byte... data) {
+ byte[] copy = Arrays.copyOf(data, data.length);
+ return new DummyWebSocketServer() {
+ @Override
+ protected void serve(SocketChannel channel) throws IOException {
+ ByteBuffer closeMessage = ByteBuffer.wrap(copy);
+ channel.write(closeMessage);
+ super.serve(channel);
+ }
+ };
+ }
+
+ @Test
+ public void sendMethodsThrowNPE() throws IOException {
+ try (DummyWebSocketServer server = new DummyWebSocketServer()) {
+ server.open();
+ WebSocket ws = newHttpClient()
+ .newWebSocketBuilder()
+ .buildAsync(server.getURI(), new WebSocket.Listener() { })
+ .join();
+
+ assertThrows(NPE, () -> ws.sendText(null, false));
+ assertThrows(NPE, () -> ws.sendText(null, true));
+ assertThrows(NPE, () -> ws.sendBinary(null, false));
+ assertThrows(NPE, () -> ws.sendBinary(null, true));
+ assertThrows(NPE, () -> ws.sendPing(null));
+ assertThrows(NPE, () -> ws.sendPong(null));
+ assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null));
+
+ ws.abort();
+
+ assertThrows(NPE, () -> ws.sendText(null, false));
+ assertThrows(NPE, () -> ws.sendText(null, true));
+ assertThrows(NPE, () -> ws.sendBinary(null, false));
+ assertThrows(NPE, () -> ws.sendBinary(null, true));
+ assertThrows(NPE, () -> ws.sendPing(null));
+ assertThrows(NPE, () -> ws.sendPong(null));
+ assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null));
+ }
+ }
+
+ // TODO: request in onClose/onError
+ // TODO: throw exception in onClose/onError
+ // TODO: exception is thrown from request()
+
+ @Test
+ public void sendCloseCompleted() throws IOException {
+ try (DummyWebSocketServer server = new DummyWebSocketServer()) {
+ server.open();
+ WebSocket ws = newHttpClient()
+ .newWebSocketBuilder()
+ .buildAsync(server.getURI(), new WebSocket.Listener() { })
+ .join();
+ ws.sendClose(NORMAL_CLOSURE, "").join();
+ assertTrue(ws.isOutputClosed());
+ assertEquals(ws.getSubprotocol(), "");
+ ws.request(1); // No exceptions must be thrown
+ }
+ }
+
+ @Test
+ public void sendClosePending() throws Exception {
+ try (DummyWebSocketServer server = notReadingServer()) {
+ server.open();
+ WebSocket ws = newHttpClient()
+ .newWebSocketBuilder()
+ .buildAsync(server.getURI(), new WebSocket.Listener() { })
+ .join();
+ try {
+ ByteBuffer data = ByteBuffer.allocate(65536);
+ for (int i = 0; ; i++) { // fill up the send buffer
+ System.out.printf("begin cycle #%s at %s%n",
+ i, System.currentTimeMillis());
+ try {
+ ws.sendBinary(data, true).get(10, TimeUnit.SECONDS);
+ data.clear();
+ } catch (TimeoutException e) {
+ break;
+ } finally {
+ System.out.printf("end cycle #%s at %s%n",
+ i, System.currentTimeMillis());
+ }
+ }
+ CompletableFuture<WebSocket> cf = ws.sendClose(NORMAL_CLOSURE, "");
+ // The output closes even if the Close message has not been sent
+ assertFalse(cf.isDone());
+ assertTrue(ws.isOutputClosed());
+ assertEquals(ws.getSubprotocol(), "");
+ } finally {
+ ws.abort();
+ }
+ }
+ }
+
+ /*
+ * This server does not read from the wire, allowing its client to fill up
+ * their send buffer. Used to test scenarios with outstanding send
+ * operations.
+ */
+ private static DummyWebSocketServer notReadingServer() {
+ return new DummyWebSocketServer() {
+ @Override
+ protected void serve(SocketChannel channel) throws IOException {
+ try {
+ Thread.sleep(Long.MAX_VALUE);
+ } catch (InterruptedException e) {
+ throw new IOException(e);
+ }
+ }
+ };
+ }
+
+ @Test
+ public void abortPendingSendBinary() throws Exception {
+ try (DummyWebSocketServer server = notReadingServer()) {
+ server.open();
+ WebSocket ws = newHttpClient()
+ .newWebSocketBuilder()
+ .buildAsync(server.getURI(), new WebSocket.Listener() { })
+ .join();
+ ByteBuffer data = ByteBuffer.allocate(65536);
+ CompletableFuture<WebSocket> cf = null;
+ for (int i = 0; ; i++) { // fill up the send buffer
+ System.out.printf("begin cycle #%s at %s%n",
+ i, System.currentTimeMillis());
+ try {
+ cf = ws.sendBinary(data, true);
+ cf.get(10, TimeUnit.SECONDS);
+ data.clear();
+ } catch (TimeoutException e) {
+ break;
+ } finally {
+ System.out.printf("end cycle #%s at %s%n",
+ i, System.currentTimeMillis());
+ }
+ }
+ ws.abort();
+ assertTrue(ws.isOutputClosed());
+ assertTrue(ws.isInputClosed());
+ assertFails(IOException.class, cf);
+ }
+ }
+
+ @Test
+ public void abortPendingSendText() throws Exception {
+ try (DummyWebSocketServer server = notReadingServer()) {
+ server.open();
+ WebSocket ws = newHttpClient()
+ .newWebSocketBuilder()
+ .buildAsync(server.getURI(), new WebSocket.Listener() { })
+ .join();
+ String data = stringWith2NBytes(32768);
+ CompletableFuture<WebSocket> cf = null;
+ for (int i = 0; ; i++) { // fill up the send buffer
+ System.out.printf("begin cycle #%s at %s%n",
+ i, System.currentTimeMillis());
+ try {
+ cf = ws.sendText(data, true);
+ cf.get(10, TimeUnit.SECONDS);
+ } catch (TimeoutException e) {
+ break;
+ } finally {
+ System.out.printf("end cycle #%s at %s%n",
+ i, System.currentTimeMillis());
+ }
+ }
+ ws.abort();
+ assertTrue(ws.isOutputClosed());
+ assertTrue(ws.isInputClosed());
+ assertFails(IOException.class, cf);
+ }
+ }
+
+ private static String stringWith2NBytes(int n) {
+ // -- Russian Alphabet (33 characters, 2 bytes per char) --
+ char[] abc = {
+ 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0401, 0x0416,
+ 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E,
+ 0x041F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426,
+ 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E,
+ 0x042F,
+ };
+ // repeat cyclically
+ StringBuilder sb = new StringBuilder(n);
+ for (int i = 0, j = 0; i < n; i++, j = (j + 1) % abc.length) {
+ sb.append(abc[j]);
+ }
+ String s = sb.toString();
+ assert s.length() == n && s.getBytes(StandardCharsets.UTF_8).length == 2 * n;
+ return s;
+ }
+
+ @Test
+ public void sendCloseTimeout() throws Exception {
+ try (DummyWebSocketServer server = notReadingServer()) {
+ server.open();
+ WebSocket ws = newHttpClient()
+ .newWebSocketBuilder()
+ .buildAsync(server.getURI(), new WebSocket.Listener() { })
+ .join();
+ String data = stringWith2NBytes(32768);
+ CompletableFuture<WebSocket> cf = null;
+ for (int i = 0; ; i++) { // fill up the send buffer
+ System.out.printf("begin cycle #%s at %s%n",
+ i, System.currentTimeMillis());
+ try {
+ cf = ws.sendText(data, true);
+ cf.get(10, TimeUnit.SECONDS);
+ } catch (TimeoutException e) {
+ break;
+ } finally {
+ System.out.printf("end cycle #%s at %s%n",
+ i, System.currentTimeMillis());
+ }
+ }
+ long before = System.currentTimeMillis();
+ assertFails(IOException.class,
+ ws.sendClose(WebSocket.NORMAL_CLOSURE, "ok"));
+ long after = System.currentTimeMillis();
+ // default timeout should be 30 seconds
+ long elapsed = after - before;
+ System.out.printf("Elapsed %s ms%n", elapsed);
+ assertTrue(elapsed >= 29_000, String.valueOf(elapsed));
+ assertTrue(ws.isOutputClosed());
+ assertTrue(ws.isInputClosed());
+ assertFails(IOException.class, cf);
+ }
+ }
+}
--- a/test/jdk/java/net/httpclient/websocket/WebSocketTest.java Fri Mar 16 12:57:42 2018 +0000
+++ b/test/jdk/java/net/httpclient/websocket/WebSocketTest.java Sat Mar 17 18:01:01 2018 +0000
@@ -24,12 +24,10 @@
/*
* @test
* @build DummyWebSocketServer
- * @run testng/othervm/timeout=600
+ * @run testng/othervm
* -Djdk.internal.httpclient.websocket.debug=true
* WebSocketTest
*/
-import org.testng.annotations.DataProvider;
-import org.testng.annotations.Test;
import java.io.IOException;
import java.net.http.WebSocket;
@@ -45,7 +43,8 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
-
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
import static java.net.http.HttpClient.newHttpClient;
import static java.net.http.WebSocket.NORMAL_CLOSURE;
import static org.testng.Assert.assertEquals;
@@ -56,125 +55,10 @@
public class WebSocketTest {
- private static final Class<NullPointerException> NPE = NullPointerException.class;
private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
private static final Class<IllegalStateException> ISE = IllegalStateException.class;
private static final Class<IOException> IOE = IOException.class;
- /*
- * Examines WebSocket behaviour after a call to abort()
- */
- @Test
- public void immediateAbort() throws Exception {
- try (DummyWebSocketServer server = serverWithCannedData(0x81, 0x00, 0x88, 0x00)) {
- server.open();
- CompletableFuture<Void> messageReceived = new CompletableFuture<>();
- WebSocket.Listener listener = new WebSocket.Listener() {
-
- @Override
- public void onOpen(WebSocket webSocket) {
- /* no initial request */
- }
-
- @Override
- public CompletionStage<?> onText(WebSocket webSocket,
- CharSequence message,
- WebSocket.MessagePart part) {
- messageReceived.complete(null);
- return null;
- }
-
- @Override
- public CompletionStage<?> onBinary(WebSocket webSocket,
- ByteBuffer message,
- WebSocket.MessagePart part) {
- messageReceived.complete(null);
- return null;
- }
-
- @Override
- public CompletionStage<?> onPing(WebSocket webSocket,
- ByteBuffer message) {
- messageReceived.complete(null);
- return null;
- }
-
- @Override
- public CompletionStage<?> onPong(WebSocket webSocket,
- ByteBuffer message) {
- messageReceived.complete(null);
- return null;
- }
-
- @Override
- public CompletionStage<?> onClose(WebSocket webSocket,
- int statusCode,
- String reason) {
- messageReceived.complete(null);
- return null;
- }
- };
-
- WebSocket ws = newHttpClient()
- .newWebSocketBuilder()
- .buildAsync(server.getURI(), listener)
- .join();
- for (int i = 0; i < 3; i++) {
- System.out.printf("iteration #%s%n", i);
- // after the first abort() each consecutive one must be a no-op,
- // moreover, query methods should continue to return consistent,
- // permanent values
- for (int j = 0; j < 3; j++) {
- System.out.printf("abort #%s%n", j);
- ws.abort();
- assertTrue(ws.isInputClosed());
- assertTrue(ws.isOutputClosed());
- assertEquals(ws.getSubprotocol(), "");
- }
- // at this point valid requests MUST be a no-op:
- for (int j = 0; j < 3; j++) {
- System.out.printf("request #%s%n", j);
- ws.request(1);
- ws.request(2);
- ws.request(8);
- ws.request(Integer.MAX_VALUE);
- ws.request(Long.MAX_VALUE);
- // invalid requests MUST throw IAE:
- assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE));
- assertThrows(IAE, () -> ws.request(Long.MIN_VALUE));
- assertThrows(IAE, () -> ws.request(-1));
- assertThrows(IAE, () -> ws.request(0));
- }
- }
- // even though there is a bunch of messages readily available on the
- // wire we shouldn't have received any of them as we aborted before
- // the first request
- try {
- messageReceived.get(10, TimeUnit.SECONDS);
- fail();
- } catch (TimeoutException expected) {
- System.out.println("Finished waiting");
- }
- for (int i = 0; i < 3; i++) {
- System.out.printf("send #%s%n", i);
- assertFails(IOE, ws.sendText("text!", false));
- assertFails(IOE, ws.sendText("text!", true));
- assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), false));
- assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), true));
- assertFails(IOE, ws.sendPing(ByteBuffer.allocate(16)));
- assertFails(IOE, ws.sendPong(ByteBuffer.allocate(16)));
- assertFails(IOE, ws.sendClose(NORMAL_CLOSURE, "a reason"));
- assertThrows(NPE, () -> ws.sendText(null, false));
- assertThrows(NPE, () -> ws.sendText(null, true));
- assertThrows(NPE, () -> ws.sendBinary(null, false));
- assertThrows(NPE, () -> ws.sendBinary(null, true));
- assertThrows(NPE, () -> ws.sendPing(null));
- assertThrows(NPE, () -> ws.sendPong(null));
- assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null));
- }
- }
- }
-
/* shortcut */
private static void assertFails(Class<? extends Throwable> clazz,
CompletionStage<?> stage) {
@@ -201,88 +85,6 @@
};
}
- @Test
- public void sendMethodsThrowNPE() throws IOException {
- try (DummyWebSocketServer server = new DummyWebSocketServer()) {
- server.open();
- WebSocket ws = newHttpClient()
- .newWebSocketBuilder()
- .buildAsync(server.getURI(), new WebSocket.Listener() { })
- .join();
-
- assertThrows(NPE, () -> ws.sendText(null, false));
- assertThrows(NPE, () -> ws.sendText(null, true));
- assertThrows(NPE, () -> ws.sendBinary(null, false));
- assertThrows(NPE, () -> ws.sendBinary(null, true));
- assertThrows(NPE, () -> ws.sendPing(null));
- assertThrows(NPE, () -> ws.sendPong(null));
- assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null));
-
- ws.abort();
-
- assertThrows(NPE, () -> ws.sendText(null, false));
- assertThrows(NPE, () -> ws.sendText(null, true));
- assertThrows(NPE, () -> ws.sendBinary(null, false));
- assertThrows(NPE, () -> ws.sendBinary(null, true));
- assertThrows(NPE, () -> ws.sendPing(null));
- assertThrows(NPE, () -> ws.sendPong(null));
- assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null));
- }
- }
-
- // TODO: request in onClose/onError
- // TODO: throw exception in onClose/onError
- // TODO: exception is thrown from request()
-
- @Test
- public void sendCloseCompleted() throws IOException {
- try (DummyWebSocketServer server = new DummyWebSocketServer()) {
- server.open();
- WebSocket ws = newHttpClient()
- .newWebSocketBuilder()
- .buildAsync(server.getURI(), new WebSocket.Listener() { })
- .join();
- ws.sendClose(NORMAL_CLOSURE, "").join();
- assertTrue(ws.isOutputClosed());
- assertEquals(ws.getSubprotocol(), "");
- ws.request(1); // No exceptions must be thrown
- }
- }
-
- @Test
- public void sendClosePending() throws Exception {
- try (DummyWebSocketServer server = notReadingServer()) {
- server.open();
- WebSocket ws = newHttpClient()
- .newWebSocketBuilder()
- .buildAsync(server.getURI(), new WebSocket.Listener() { })
- .join();
- try {
- ByteBuffer data = ByteBuffer.allocate(65536);
- for (int i = 0; ; i++) { // fill up the send buffer
- System.out.printf("begin cycle #%s at %s%n",
- i, System.currentTimeMillis());
- try {
- ws.sendBinary(data, true).get(10, TimeUnit.SECONDS);
- data.clear();
- } catch (TimeoutException e) {
- break;
- } finally {
- System.out.printf("end cycle #%s at %s%n",
- i, System.currentTimeMillis());
- }
- }
- CompletableFuture<WebSocket> cf = ws.sendClose(NORMAL_CLOSURE, "");
- // The output closes even if the Close message has not been sent
- assertFalse(cf.isDone());
- assertTrue(ws.isOutputClosed());
- assertEquals(ws.getSubprotocol(), "");
- } finally {
- ws.abort();
- }
- }
- }
-
/*
* This server does not read from the wire, allowing its client to fill up
* their send buffer. Used to test scenarios with outstanding send
@@ -301,67 +103,6 @@
};
}
- @Test
- public void abortPendingSendBinary() throws Exception {
- try (DummyWebSocketServer server = notReadingServer()) {
- server.open();
- WebSocket ws = newHttpClient()
- .newWebSocketBuilder()
- .buildAsync(server.getURI(), new WebSocket.Listener() { })
- .join();
- ByteBuffer data = ByteBuffer.allocate(65536);
- CompletableFuture<WebSocket> cf = null;
- for (int i = 0; ; i++) { // fill up the send buffer
- System.out.printf("begin cycle #%s at %s%n",
- i, System.currentTimeMillis());
- try {
- cf = ws.sendBinary(data, true);
- cf.get(10, TimeUnit.SECONDS);
- data.clear();
- } catch (TimeoutException e) {
- break;
- } finally {
- System.out.printf("end cycle #%s at %s%n",
- i, System.currentTimeMillis());
- }
- }
- ws.abort();
- assertTrue(ws.isOutputClosed());
- assertTrue(ws.isInputClosed());
- assertFails(IOException.class, cf);
- }
- }
-
- @Test
- public void abortPendingSendText() throws Exception {
- try (DummyWebSocketServer server = notReadingServer()) {
- server.open();
- WebSocket ws = newHttpClient()
- .newWebSocketBuilder()
- .buildAsync(server.getURI(), new WebSocket.Listener() { })
- .join();
- String data = stringWith2NBytes(32768);
- CompletableFuture<WebSocket> cf = null;
- for (int i = 0; ; i++) { // fill up the send buffer
- System.out.printf("begin cycle #%s at %s%n",
- i, System.currentTimeMillis());
- try {
- cf = ws.sendText(data, true);
- cf.get(10, TimeUnit.SECONDS);
- } catch (TimeoutException e) {
- break;
- } finally {
- System.out.printf("end cycle #%s at %s%n",
- i, System.currentTimeMillis());
- }
- }
- ws.abort();
- assertTrue(ws.isOutputClosed());
- assertTrue(ws.isInputClosed());
- assertFails(IOException.class, cf);
- }
- }
-
private static String stringWith2NBytes(int n) {
// -- Russian Alphabet (33 characters, 2 bytes per char) --
char[] abc = {
@@ -382,43 +123,6 @@
}
@Test
- public void sendCloseTimeout() throws Exception {
- try (DummyWebSocketServer server = notReadingServer()) {
- server.open();
- WebSocket ws = newHttpClient()
- .newWebSocketBuilder()
- .buildAsync(server.getURI(), new WebSocket.Listener() { })
- .join();
- String data = stringWith2NBytes(32768);
- CompletableFuture<WebSocket> cf = null;
- for (int i = 0; ; i++) { // fill up the send buffer
- System.out.printf("begin cycle #%s at %s%n",
- i, System.currentTimeMillis());
- try {
- cf = ws.sendText(data, true);
- cf.get(10, TimeUnit.SECONDS);
- } catch (TimeoutException e) {
- break;
- } finally {
- System.out.printf("end cycle #%s at %s%n",
- i, System.currentTimeMillis());
- }
- }
- long before = System.currentTimeMillis();
- assertFails(IOException.class,
- ws.sendClose(WebSocket.NORMAL_CLOSURE, "ok"));
- long after = System.currentTimeMillis();
- // default timeout should be 30 seconds
- long elapsed = after - before;
- System.out.printf("Elapsed %s ms%n", elapsed);
- assertTrue(elapsed >= 29_000, String.valueOf(elapsed));
- assertTrue(ws.isOutputClosed());
- assertTrue(ws.isInputClosed());
- assertFails(IOException.class, cf);
- }
- }
-
- @Test
public void testIllegalArgument() throws IOException {
try (DummyWebSocketServer server = new DummyWebSocketServer()) {
server.open();