test/jdk/java/net/httpclient/websocket/SendTest.java
branchhttp-client-branch
changeset 56318 2a96e88888b2
parent 56317 52a9f6b74e43
child 56320 f82729ca8660
--- a/test/jdk/java/net/httpclient/websocket/SendTest.java	Sat Mar 17 18:01:01 2018 +0000
+++ b/test/jdk/java/net/httpclient/websocket/SendTest.java	Sun Mar 18 14:31:36 2018 +0000
@@ -29,17 +29,15 @@
  *       SendTest
  */
 
+import org.testng.annotations.Test;
+
 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;
@@ -51,24 +49,6 @@
 
     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()) {
@@ -119,7 +99,7 @@
 
     @Test
     public void sendClosePending() throws Exception {
-        try (DummyWebSocketServer server = notReadingServer()) {
+        try (DummyWebSocketServer server = Support.notReadingServer()) {
             server.open();
             WebSocket ws = newHttpClient()
                     .newWebSocketBuilder()
@@ -151,27 +131,9 @@
         }
     }
 
-    /*
-     * 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()) {
+        try (DummyWebSocketServer server = Support.notReadingServer()) {
             server.open();
             WebSocket ws = newHttpClient()
                     .newWebSocketBuilder()
@@ -196,19 +158,19 @@
             ws.abort();
             assertTrue(ws.isOutputClosed());
             assertTrue(ws.isInputClosed());
-            assertFails(IOException.class, cf);
+            Support.assertFails(IOException.class, cf);
         }
     }
 
     @Test
     public void abortPendingSendText() throws Exception {
-        try (DummyWebSocketServer server = notReadingServer()) {
+        try (DummyWebSocketServer server = Support.notReadingServer()) {
             server.open();
             WebSocket ws = newHttpClient()
                     .newWebSocketBuilder()
                     .buildAsync(server.getURI(), new WebSocket.Listener() { })
                     .join();
-            String data = stringWith2NBytes(32768);
+            String data = Support.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",
@@ -226,38 +188,19 @@
             ws.abort();
             assertTrue(ws.isOutputClosed());
             assertTrue(ws.isInputClosed());
-            assertFails(IOException.class, cf);
+            Support.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()) {
+        try (DummyWebSocketServer server = Support.notReadingServer()) {
             server.open();
             WebSocket ws = newHttpClient()
                     .newWebSocketBuilder()
                     .buildAsync(server.getURI(), new WebSocket.Listener() { })
                     .join();
-            String data = stringWith2NBytes(32768);
+            String data = Support.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",
@@ -273,7 +216,7 @@
                 }
             }
             long before = System.currentTimeMillis();
-            assertFails(IOException.class,
+            Support.assertFails(IOException.class,
                     ws.sendClose(WebSocket.NORMAL_CLOSURE, "ok"));
             long after = System.currentTimeMillis();
             // default timeout should be 30 seconds
@@ -282,7 +225,7 @@
             assertTrue(elapsed >= 29_000, String.valueOf(elapsed));
             assertTrue(ws.isOutputClosed());
             assertTrue(ws.isInputClosed());
-            assertFails(IOException.class, cf);
+            Support.assertFails(IOException.class, cf);
         }
     }
 }