You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by el...@apache.org on 2012/11/21 12:08:00 UTC

svn commit: r1412057 - in /mina/mina/trunk/core/src/test/java/org/apache/mina/transport/tcp: NioTcpServerFilterEventTest.java NioTcpServerHandlerTest.java

Author: elecharny
Date: Wed Nov 21 11:07:59 2012
New Revision: 1412057

URL: http://svn.apache.org/viewvc?rev=1412057&view=rev
Log:
Added some tests to check that we can use one single selector to handle OP_ACCEPT, OP_READ and OP_WRITE events

Modified:
    mina/mina/trunk/core/src/test/java/org/apache/mina/transport/tcp/NioTcpServerFilterEventTest.java
    mina/mina/trunk/core/src/test/java/org/apache/mina/transport/tcp/NioTcpServerHandlerTest.java

Modified: mina/mina/trunk/core/src/test/java/org/apache/mina/transport/tcp/NioTcpServerFilterEventTest.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/test/java/org/apache/mina/transport/tcp/NioTcpServerFilterEventTest.java?rev=1412057&r1=1412056&r2=1412057&view=diff
==============================================================================
--- mina/mina/trunk/core/src/test/java/org/apache/mina/transport/tcp/NioTcpServerFilterEventTest.java (original)
+++ mina/mina/trunk/core/src/test/java/org/apache/mina/transport/tcp/NioTcpServerFilterEventTest.java Wed Nov 21 11:07:59 2012
@@ -33,7 +33,9 @@ import org.apache.mina.api.AbstractIoFil
 import org.apache.mina.api.IoSession;
 import org.apache.mina.filterchain.ReadFilterChainController;
 import org.apache.mina.filterchain.WriteFilterChainController;
+import org.apache.mina.transport.nio.FixedSelectorLoopPool;
 import org.apache.mina.transport.nio.NioTcpServer;
+import org.apache.mina.transport.nio.SelectorLoopPool;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -47,7 +49,9 @@ public class NioTcpServerFilterEventTest
 
     private static final Logger LOG = LoggerFactory.getLogger(NioTcpServerFilterEventTest.class);
 
-    private static final int CLIENT_COUNT = 50;
+    private static final int CLIENT_COUNT = 100;
+
+    private static final int WAIT_TIME = 200;
 
     private final CountDownLatch msgSentLatch = new CountDownLatch(CLIENT_COUNT);
 
@@ -65,17 +69,90 @@ public class NioTcpServerFilterEventTest
         // warm up
         Thread.sleep(100);
 
+        long t0 = System.currentTimeMillis();
+        final int port = server.getServerSocketChannel().socket().getLocalPort();
+
+        final Socket[] clients = new Socket[CLIENT_COUNT];
+
+        // connect some clients
+        for (int i = 0; i < CLIENT_COUNT; i++) {
+            //System.out.println("Creation client " + i);
+            try {
+                clients[i] = new Socket("127.0.0.1", port);
+            } catch (Exception e) {
+                e.printStackTrace();
+                System.out.println("Creation client " + i + " failed");
+            }
+        }
+
+        // does the session open message was fired ?
+        assertTrue(openLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
+
+        // write some messages
+        for (int i = 0; i < CLIENT_COUNT; i++) {
+            clients[i].getOutputStream().write(("test:" + i).getBytes());
+            clients[i].getOutputStream().flush();
+        }
+
+        // test is message was received by the server
+        assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
+
+        // does response was wrote and sent ?
+        assertTrue(msgSentLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
+
+        // read the echos
+        final byte[] buffer = new byte[1024];
+
+        for (int i = 0; i < CLIENT_COUNT; i++) {
+            final int bytes = clients[i].getInputStream().read(buffer);
+            final String text = new String(buffer, 0, bytes);
+            assertEquals("test:" + i, text);
+        }
+
+        // close the session
+        assertEquals(CLIENT_COUNT, closedLatch.getCount());
+        for (int i = 0; i < CLIENT_COUNT; i++) {
+            clients[i].close();
+        }
+
+        // does the session close event was fired ?
+        assertTrue(closedLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
+
+        long t1 = System.currentTimeMillis();
+
+        System.out.println("Delta = " + (t1 - t0));
+
+        server.unbind();
+    }
+
+    /**
+     * A test that creates 50 clients, each one of them writing one message. We will
+     * check that for each client we correctly process the sessionOpened, messageReceived,
+     * messageSent and sessionClosed events.
+     * We use only one selector to process all the OP events.
+     */
+    @Test
+    public void generateAllKindOfServerEventOneSelector() throws IOException, InterruptedException {
+        SelectorLoopPool selectorLoopPool = new FixedSelectorLoopPool(1);
+        final NioTcpServer server = new NioTcpServer(selectorLoopPool.getSelectorLoop(), selectorLoopPool);
+        server.setFilters(new MyCodec(), new Handler());
+        server.bind(0);
+        // warm up
+        Thread.sleep(100);
+
+        long t0 = System.currentTimeMillis();
         final int port = server.getServerSocketChannel().socket().getLocalPort();
 
         final Socket[] clients = new Socket[CLIENT_COUNT];
 
         // connect some clients
         for (int i = 0; i < CLIENT_COUNT; i++) {
+            //System.out.println("Creation client 2 " + i);
             clients[i] = new Socket("127.0.0.1", port);
         }
 
         // does the session open message was fired ?
-        assertTrue(openLatch.await(200, TimeUnit.MILLISECONDS));
+        assertTrue(openLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
 
         // write some messages
         for (int i = 0; i < CLIENT_COUNT; i++) {
@@ -84,10 +161,10 @@ public class NioTcpServerFilterEventTest
         }
 
         // test is message was received by the server
-        assertTrue(msgReadLatch.await(200, TimeUnit.MILLISECONDS));
+        assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
 
         // does response was wrote and sent ?
-        assertTrue(msgSentLatch.await(200, TimeUnit.MILLISECONDS));
+        assertTrue(msgSentLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
 
         // read the echos
         final byte[] buffer = new byte[1024];
@@ -105,7 +182,11 @@ public class NioTcpServerFilterEventTest
         }
 
         // does the session close event was fired ?
-        assertTrue(closedLatch.await(200, TimeUnit.MILLISECONDS));
+        assertTrue(closedLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
+
+        long t1 = System.currentTimeMillis();
+
+        System.out.println("Delta = " + (t1 - t0));
 
         server.unbind();
     }

Modified: mina/mina/trunk/core/src/test/java/org/apache/mina/transport/tcp/NioTcpServerHandlerTest.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/test/java/org/apache/mina/transport/tcp/NioTcpServerHandlerTest.java?rev=1412057&r1=1412056&r2=1412057&view=diff
==============================================================================
--- mina/mina/trunk/core/src/test/java/org/apache/mina/transport/tcp/NioTcpServerHandlerTest.java (original)
+++ mina/mina/trunk/core/src/test/java/org/apache/mina/transport/tcp/NioTcpServerHandlerTest.java Wed Nov 21 11:07:59 2012
@@ -33,7 +33,9 @@ import junit.framework.Assert;
 import org.apache.mina.api.AbstractIoHandler;
 import org.apache.mina.api.IoHandler;
 import org.apache.mina.api.IoSession;
+import org.apache.mina.transport.nio.FixedSelectorLoopPool;
 import org.apache.mina.transport.nio.NioTcpServer;
+import org.apache.mina.transport.nio.SelectorLoopPool;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -48,7 +50,9 @@ public class NioTcpServerHandlerTest {
 
     private static final Logger LOG = LoggerFactory.getLogger(NioTcpServerHandlerTest.class);
 
-    private static final int CLIENT_COUNT = 50;
+    private static final int CLIENT_COUNT = 100;
+
+    private static final int WAIT_TIME = 200;
 
     private final CountDownLatch msgSentLatch = new CountDownLatch(CLIENT_COUNT);
 
@@ -80,7 +84,63 @@ public class NioTcpServerHandlerTest {
         }
 
         // does the session open message was fired ?
-        assertTrue(openLatch.await(200, TimeUnit.MILLISECONDS));
+        assertTrue(openLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
+
+        // write some messages
+        for (int i = 0; i < CLIENT_COUNT; i++) {
+            clients[i].getOutputStream().write(("test:" + i).getBytes());
+            clients[i].getOutputStream().flush();
+        }
+
+        // test is message was received by the server
+        assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
+
+        // does response was wrote and sent ?
+        assertTrue(msgSentLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
+
+        // read the echos
+        final byte[] buffer = new byte[1024];
+
+        for (int i = 0; i < CLIENT_COUNT; i++) {
+            final int bytes = clients[i].getInputStream().read(buffer);
+            final String text = new String(buffer, 0, bytes);
+            assertEquals("test:" + i, text);
+        }
+
+        // close the session
+        assertEquals(CLIENT_COUNT, closedLatch.getCount());
+        for (int i = 0; i < CLIENT_COUNT; i++) {
+            clients[i].close();
+        }
+
+        // does the session close event was fired ?
+        assertTrue(closedLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
+
+        server.unbind();
+    }
+
+    @Test
+    public void generateAllKindOfServerEventOneSelector() throws IOException, InterruptedException {
+        SelectorLoopPool selectorLoopPool = new FixedSelectorLoopPool(1);
+        final NioTcpServer server = new NioTcpServer(selectorLoopPool.getSelectorLoop(), selectorLoopPool);
+        server.setFilters();
+        server.setIoHandler(new Handler());
+        server.bind(0);
+
+        // warm up
+        Thread.sleep(100);
+
+        final int port = server.getServerSocketChannel().socket().getLocalPort();
+
+        final Socket[] clients = new Socket[CLIENT_COUNT];
+
+        // connect some clients
+        for (int i = 0; i < CLIENT_COUNT; i++) {
+            clients[i] = new Socket("127.0.0.1", port);
+        }
+
+        // does the session open message was fired ?
+        assertTrue(openLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
 
         // write some messages
         for (int i = 0; i < CLIENT_COUNT; i++) {
@@ -89,10 +149,10 @@ public class NioTcpServerHandlerTest {
         }
 
         // test is message was received by the server
-        assertTrue(msgReadLatch.await(200, TimeUnit.MILLISECONDS));
+        assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
 
         // does response was wrote and sent ?
-        assertTrue(msgSentLatch.await(200, TimeUnit.MILLISECONDS));
+        assertTrue(msgSentLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
 
         // read the echos
         final byte[] buffer = new byte[1024];
@@ -110,7 +170,7 @@ public class NioTcpServerHandlerTest {
         }
 
         // does the session close event was fired ?
-        assertTrue(closedLatch.await(200, TimeUnit.MILLISECONDS));
+        assertTrue(closedLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
 
         server.unbind();
     }