http-client-branch: (WebSocket) tests http-client-branch
authorprappo
Wed, 31 Jan 2018 14:29:56 +0000
branchhttp-client-branch
changeset 56051 c4867f1e7e5a
parent 56050 e4877b1aa02f
child 56052 da2aa3c773c9
http-client-branch: (WebSocket) tests
test/jdk/java/net/httpclient/websocket/Exceptionally.java
--- a/test/jdk/java/net/httpclient/websocket/Exceptionally.java	Wed Jan 31 13:08:53 2018 +0000
+++ b/test/jdk/java/net/httpclient/websocket/Exceptionally.java	Wed Jan 31 14:29:56 2018 +0000
@@ -43,7 +43,10 @@
 
 import static jdk.incubator.http.HttpClient.newHttpClient;
 import static jdk.incubator.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 Exceptionally {
 
@@ -74,6 +77,68 @@
     }
 
     @Test
+    public void testSendClose1() 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());
+            assertFalse(ws.isInputClosed());
+            assertEquals(ws.getSubprotocol(), "");
+            ws.request(1); // No exceptions must be thrown
+        }
+    }
+
+    @Test
+    public void testSendClose2() throws Exception {
+        try (DummyWebSocketServer server = notReadingServer()) {
+            server.open();
+            WebSocket ws = newHttpClient()
+                    .newWebSocketBuilder()
+                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
+                    .join();
+            ByteBuffer data = ByteBuffer.allocate(65536);
+            for (int i = 0; ; i++) {
+                System.out.println("cycle #" + i);
+                try {
+                    ws.sendBinary(data, true).get(10, TimeUnit.SECONDS);
+                    data.clear();
+                } catch (TimeoutException e) {
+                    break;
+                }
+            }
+            CompletableFuture<WebSocket> cf = ws.sendClose(NORMAL_CLOSURE, "");
+            assertTrue(ws.isOutputClosed());
+            assertFalse(ws.isInputClosed());
+            assertEquals(ws.getSubprotocol(), "");
+            // The output closes regardless of whether or not the Close message
+            // has been sent
+            assertFalse(cf.isDone());
+        }
+    }
+
+    /*
+     * 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 testIllegalArgument() throws IOException {
         try (DummyWebSocketServer server = new DummyWebSocketServer()) {
             server.open();
@@ -220,19 +285,6 @@
         }
     }
 
-    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 testIllegalStateOutstanding2() throws Exception {
         try (DummyWebSocketServer server = notReadingServer()) {