You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2013/02/11 15:03:54 UTC

svn commit: r1444769 - in /tomcat/trunk/test/org/apache/tomcat/websocket: TestWsRemoteEndpoint.java TesterSingleMessageClient.java

Author: markt
Date: Mon Feb 11 14:03:53 2013
New Revision: 1444769

URL: http://svn.apache.org/r1444769
Log:
Add Writer test

Added:
    tomcat/trunk/test/org/apache/tomcat/websocket/TestWsRemoteEndpoint.java   (with props)
Modified:
    tomcat/trunk/test/org/apache/tomcat/websocket/TesterSingleMessageClient.java

Added: tomcat/trunk/test/org/apache/tomcat/websocket/TestWsRemoteEndpoint.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/websocket/TestWsRemoteEndpoint.java?rev=1444769&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/tomcat/websocket/TestWsRemoteEndpoint.java (added)
+++ tomcat/trunk/test/org/apache/tomcat/websocket/TestWsRemoteEndpoint.java Mon Feb 11 14:03:53 2013
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomcat.websocket;
+
+import java.io.Writer;
+import java.net.URI;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import javax.websocket.ContainerProvider;
+import javax.websocket.DefaultClientConfiguration;
+import javax.websocket.Session;
+import javax.websocket.WebSocketContainer;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.catalina.Context;
+import org.apache.catalina.startup.Tomcat;
+import org.apache.catalina.startup.TomcatBaseTest;
+import org.apache.tomcat.websocket.TesterSingleMessageClient.AsyncHandler;
+import org.apache.tomcat.websocket.TesterSingleMessageClient.AsyncText;
+import org.apache.tomcat.websocket.TesterSingleMessageClient.TesterEndpoint;
+
+public class TestWsRemoteEndpoint extends TomcatBaseTest {
+
+    private static final String SEQUENCE = "ABCDE";
+    private static final int S_LEN = SEQUENCE.length();
+    private static final String TEST_MESSAGE_5K;
+
+    static {
+        StringBuilder sb = new StringBuilder(S_LEN * 1024);
+        for (int i = 0; i < 1024; i++) {
+            sb.append(SEQUENCE);
+        }
+        TEST_MESSAGE_5K = sb.toString();
+    }
+
+    @Test
+    public void testWriter() throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+        // Must have a real docBase - just use temp
+        Context ctx =
+            tomcat.addContext("", System.getProperty("java.io.tmpdir"));
+        ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
+
+        WebSocketContainer wsContainer =
+                ContainerProvider.createClientContainer();
+
+        tomcat.start();
+
+        Session wsSession = wsContainer.connectToServer(TesterEndpoint.class,
+                new DefaultClientConfiguration(), new URI("http://localhost:" +
+                        getPort() + TesterEchoServer.Config.PATH_ASYNC));
+
+        CountDownLatch latch = new CountDownLatch(1);
+        wsSession.getUserProperties().put("latch", latch);
+        AsyncHandler<?> handler = new AsyncText(latch);
+
+        wsSession.addMessageHandler(handler);
+
+        Writer w = wsSession.getRemote().getSendWriter();
+
+        for (int i = 0; i < 8; i++) {
+            w.write(TEST_MESSAGE_5K);
+        }
+
+        w.close();
+
+        boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
+
+        Assert.assertTrue(latchResult);
+
+        List<String> messages = (List<String>) handler.getMessages();
+
+        int offset = 0;
+        int i = 0;
+        for (String message : messages) {
+            // First may be a fragment
+            Assert.assertEquals(SEQUENCE.substring(offset, S_LEN),
+                    message.substring(0, S_LEN - offset));
+            i = S_LEN - offset;
+            while (i + S_LEN < message.length()) {
+                if (!SEQUENCE.equals(message.substring(i, i + S_LEN))) {
+                    Assert.fail();
+                }
+                i += S_LEN;
+            }
+            offset = message.length() - i;
+            if (!SEQUENCE.substring(0, offset).equals(message.substring(i))) {
+                Assert.fail();
+            }
+        }
+    }
+}

Propchange: tomcat/trunk/test/org/apache/tomcat/websocket/TestWsRemoteEndpoint.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tomcat/trunk/test/org/apache/tomcat/websocket/TesterSingleMessageClient.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/websocket/TesterSingleMessageClient.java?rev=1444769&r1=1444768&r2=1444769&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/tomcat/websocket/TesterSingleMessageClient.java (original)
+++ tomcat/trunk/test/org/apache/tomcat/websocket/TesterSingleMessageClient.java Mon Feb 11 14:03:53 2013
@@ -108,4 +108,55 @@ public class TesterSingleMessageClient {
             }
         }
     }
+
+    public abstract static class AsyncHandler<T>
+            implements MessageHandler.Async<T> {
+
+        private final CountDownLatch latch;
+
+        private final List<T> messages = new CopyOnWriteArrayList<>();
+
+        public AsyncHandler(CountDownLatch latch) {
+            this.latch = latch;
+        }
+
+        public CountDownLatch getLatch() {
+            return latch;
+        }
+
+        public List<T> getMessages() {
+            return messages;
+        }
+    }
+
+    public static class AsyncBinary extends AsyncHandler<ByteBuffer> {
+
+        public AsyncBinary(CountDownLatch latch) {
+            super(latch);
+        }
+
+        @Override
+        public void onMessage(ByteBuffer message, boolean last) {
+            getMessages().add(message);
+            if (last && getLatch() != null) {
+                getLatch().countDown();
+            }
+        }
+    }
+
+    public static class AsyncText extends AsyncHandler<String> {
+
+
+        public AsyncText(CountDownLatch latch) {
+            super(latch);
+        }
+
+        @Override
+        public void onMessage(String message, boolean last) {
+            getMessages().add(message);
+            if (last && getLatch() != null) {
+                getLatch().countDown();
+            }
+        }
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org