http-client-branch: (WebSocket) tests http-client-branch
authorprappo
Thu, 30 Nov 2017 22:27:18 +0300
branchhttp-client-branch
changeset 55943 952aca3f605a
parent 55937 c94a558a70ed
child 55944 b7e186aa1915
http-client-branch: (WebSocket) tests
test/jdk/java/net/httpclient/websocket/jdk.incubator.httpclient/jdk/incubator/http/internal/websocket/MockListener.java
test/jdk/java/net/httpclient/websocket/jdk.incubator.httpclient/jdk/incubator/http/internal/websocket/ReceivingTest.java
--- a/test/jdk/java/net/httpclient/websocket/jdk.incubator.httpclient/jdk/incubator/http/internal/websocket/MockListener.java	Thu Nov 30 17:49:29 2017 +0000
+++ b/test/jdk/java/net/httpclient/websocket/jdk.incubator.httpclient/jdk/incubator/http/internal/websocket/MockListener.java	Thu Nov 30 22:27:18 2017 +0300
@@ -242,6 +242,11 @@
         public int hashCode() {
             return Objects.hash(text, part, webSocket);
         }
+
+        @Override
+        public String toString() {
+            return String.format("onText(%s, %s, %s)", webSocket, text, part);
+        }
     }
 
     public static final class OnBinary extends ListenerInvocation {
@@ -269,6 +274,11 @@
         public int hashCode() {
             return Objects.hash(data, part, webSocket);
         }
+
+        @Override
+        public String toString() {
+            return String.format("onBinary(%s, %s, %s)", webSocket, data, part);
+        }
     }
 
     public static final class OnPing extends ListenerInvocation {
@@ -293,6 +303,11 @@
         public int hashCode() {
             return Objects.hash(data, webSocket);
         }
+
+        @Override
+        public String toString() {
+            return String.format("onPing(%s, %s)", webSocket, data);
+        }
     }
 
     public static final class OnPong extends ListenerInvocation {
@@ -317,6 +332,11 @@
         public int hashCode() {
             return Objects.hash(data, webSocket);
         }
+
+        @Override
+        public String toString() {
+            return String.format("onPong(%s, %s)", webSocket, data);
+        }
     }
 
     public static final class OnClose extends ListenerInvocation {
@@ -344,6 +364,11 @@
         public int hashCode() {
             return Objects.hash(statusCode, reason, webSocket);
         }
+
+        @Override
+        public String toString() {
+            return String.format("onClose(%s, %s, %s)", webSocket, statusCode, reason);
+        }
     }
 
     public static final class OnError extends ListenerInvocation {
@@ -368,5 +393,10 @@
         public int hashCode() {
             return Objects.hash(clazz, webSocket);
         }
+
+        @Override
+        public String toString() {
+            return String.format("onError(%s, %s)", webSocket, clazz);
+        }
     }
 }
--- a/test/jdk/java/net/httpclient/websocket/jdk.incubator.httpclient/jdk/incubator/http/internal/websocket/ReceivingTest.java	Thu Nov 30 17:49:29 2017 +0000
+++ b/test/jdk/java/net/httpclient/websocket/jdk.incubator.httpclient/jdk/incubator/http/internal/websocket/ReceivingTest.java	Thu Nov 30 22:27:18 2017 +0300
@@ -27,6 +27,7 @@
 import org.testng.annotations.Test;
 
 import java.net.URI;
+import java.nio.ByteBuffer;
 import java.util.List;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionStage;
@@ -42,6 +43,8 @@
 import static jdk.incubator.http.internal.websocket.MockListener.ListenerInvocation.onClose;
 import static jdk.incubator.http.internal.websocket.MockListener.ListenerInvocation.onError;
 import static jdk.incubator.http.internal.websocket.MockListener.ListenerInvocation.onOpen;
+import static jdk.incubator.http.internal.websocket.MockListener.ListenerInvocation.onPing;
+import static jdk.incubator.http.internal.websocket.MockListener.ListenerInvocation.onPong;
 import static jdk.incubator.http.internal.websocket.MockListener.ListenerInvocation.onText;
 import static org.testng.Assert.assertEquals;
 
@@ -80,8 +83,7 @@
                 return new MockReceiver(consumer, channel,
                                         pair(now(), m -> m.onText("1", FIRST)),
                                         pair(now(), m -> m.onText("2", PART)),
-                                        pair(now(), m -> m.onText("3", PART)),
-                                        pair(now(), m -> m.onText("4", LAST)),
+                                        pair(now(), m -> m.onText("3", LAST)),
                                         pair(now(), m -> m.onClose(NORMAL_CLOSURE, "no reason")));
             }
         };
@@ -91,8 +93,99 @@
         assertEquals(invocations, List.of(onOpen(ws),
                                           onText(ws, "1", FIRST),
                                           onText(ws, "2", PART),
-                                          onText(ws, "3", PART),
-                                          onText(ws, "4", LAST),
+                                          onText(ws, "3", LAST),
+                                          onClose(ws, NORMAL_CLOSURE, "no reason")));
+    }
+
+    @Test
+    public void testText2() throws Exception {
+        MockListener listener = new MockListener(Long.MAX_VALUE);
+        MockTransport transport = new MockTransport() {
+            @Override
+            protected Receiver newReceiver(MessageStreamConsumer consumer) {
+                return new MockReceiver(consumer, channel,
+                                        pair(now(),      m -> m.onText("1", FIRST)),
+                                        pair(seconds(1), m -> m.onText("2", PART)),
+                                        pair(now(),      m -> m.onText("3", LAST)),
+                                        pair(seconds(1), m -> m.onClose(NORMAL_CLOSURE, "no reason")));
+            }
+        };
+        WebSocket ws = newInstance(listener, transport);
+        listener.onCloseOrOnErrorCalled().get(10, TimeUnit.SECONDS);
+        List<MockListener.ListenerInvocation> invocations = listener.invocations();
+        assertEquals(invocations, List.of(onOpen(ws),
+                                          onText(ws, "1", FIRST),
+                                          onText(ws, "2", PART),
+                                          onText(ws, "3", LAST),
+                                          onClose(ws, NORMAL_CLOSURE, "no reason")));
+    }
+
+    @Test
+    public void testTextIntermixedWithPongs() throws Exception {
+        MockListener listener = new MockListener(Long.MAX_VALUE);
+        MockTransport transport = new MockTransport() {
+            @Override
+            protected Receiver newReceiver(MessageStreamConsumer consumer) {
+                return new MockReceiver(consumer, channel,
+                                        pair(now(),      m -> m.onText("1", FIRST)),
+                                        pair(now(),      m -> m.onText("2", PART)),
+                                        pair(now(),      m -> m.onPong(ByteBuffer.allocate(16))),
+                                        pair(seconds(1), m -> m.onPong(ByteBuffer.allocate(32))),
+                                        pair(now(),      m -> m.onText("3", LAST)),
+                                        pair(now(),      m -> m.onPong(ByteBuffer.allocate(64))),
+                                        pair(now(),      m -> m.onClose(NORMAL_CLOSURE, "no reason")));
+            }
+        };
+        WebSocket ws = newInstance(listener, transport);
+        listener.onCloseOrOnErrorCalled().get(10, TimeUnit.SECONDS);
+        List<MockListener.ListenerInvocation> invocations = listener.invocations();
+        assertEquals(invocations, List.of(onOpen(ws),
+                                          onText(ws, "1", FIRST),
+                                          onText(ws, "2", PART),
+                                          onPong(ws, ByteBuffer.allocate(16)),
+                                          onPong(ws, ByteBuffer.allocate(32)),
+                                          onText(ws, "3", LAST),
+                                          onPong(ws, ByteBuffer.allocate(64)),
+                                          onClose(ws, NORMAL_CLOSURE, "no reason")));
+    }
+
+    @Test
+    public void testTextIntermixedWithPings() throws Exception {
+        MockListener listener = new MockListener(Long.MAX_VALUE);
+        MockTransport transport = new MockTransport() {
+            @Override
+            protected Receiver newReceiver(MessageStreamConsumer consumer) {
+                return new MockReceiver(consumer, channel,
+                                        pair(now(),      m -> m.onText("1", FIRST)),
+                                        pair(now(),      m -> m.onText("2", PART)),
+                                        pair(now(),      m -> m.onPing(ByteBuffer.allocate(16))),
+                                        pair(seconds(1), m -> m.onPing(ByteBuffer.allocate(32))),
+                                        pair(now(),      m -> m.onText("3", LAST)),
+                                        pair(now(),      m -> m.onPing(ByteBuffer.allocate(64))),
+                                        pair(now(),      m -> m.onClose(NORMAL_CLOSURE, "no reason")));
+            }
+
+            @Override
+            protected Transmitter newTransmitter() {
+                return new MockTransmitter() {
+                    @Override
+                    protected CompletionStage<?> whenSent() {
+                        return now();
+                    }
+                };
+            }
+        };
+        WebSocket ws = newInstance(listener, transport);
+        listener.onCloseOrOnErrorCalled().get(10, TimeUnit.SECONDS);
+        List<MockListener.ListenerInvocation> invocations = listener.invocations();
+        System.out.println(invocations);
+        assertEquals(invocations, List.of(onOpen(ws),
+                                          onText(ws, "1", FIRST),
+                                          onText(ws, "2", PART),
+                                          onPing(ws, ByteBuffer.allocate(16)),
+                                          onPing(ws, ByteBuffer.allocate(32)),
+                                          onText(ws, "3", LAST),
+                                          onPing(ws, ByteBuffer.allocate(64)),
                                           onClose(ws, NORMAL_CLOSURE, "no reason")));
     }