Bug 3230 - Attempt to use Epoll native transport if available
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / TcpHandlerTest.java
index 6d103355904315729bafec4ac775f3f2b85f4c95..6faad908f01cf60d798a67d6e263376561a0d583 100644 (file)
@@ -18,6 +18,7 @@ import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.util.concurrent.ExecutionException;
 
+import io.netty.channel.unix.Errors;
 import org.junit.Assert;
 import org.junit.Test;
 import org.mockito.Mock;
@@ -62,7 +63,25 @@ public class TcpHandlerTest {
         tcpHandler = new TcpHandler(null, 0);
         tcpHandler.setChannelInitializer(mockChannelInitializer);
 
-        assertEquals("failed to start server", true, startupServer()) ;
+        assertEquals("failed to start server", true, startupServer(false)) ;
+        assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort())) ;
+        shutdownServer();
+    }
+
+    /**
+     * Test run with null address set on Epoll native transport
+     * @throws IOException
+     * @throws InterruptedException
+     * @throws ExecutionException
+     */
+    @Test
+    public void testRunWithNullAddressOnEpoll() throws IOException, InterruptedException, ExecutionException  {
+
+        tcpHandler = new TcpHandler(null, 0);
+        tcpHandler.setChannelInitializer(mockChannelInitializer);
+
+        //Use Epoll native transport
+        assertEquals("failed to start server", true, startupServer(true)) ;
         assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort())) ;
         shutdownServer();
     }
@@ -79,11 +98,29 @@ public class TcpHandlerTest {
         tcpHandler = new TcpHandler(serverAddress, 0);
         tcpHandler.setChannelInitializer(mockChannelInitializer);
 
-        assertEquals("failed to start server", true, startupServer()) ;
+        assertEquals("failed to start server", true, startupServer(false)) ;
         assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort())) ;
         shutdownServer();
     }
 
+    /**
+     * Test run with address set on Epoll native transport
+     * @throws IOException
+     * @throws InterruptedException
+     * @throws ExecutionException
+     */
+    @Test
+    public void testRunWithAddressOnEpoll() throws IOException, InterruptedException, ExecutionException  {
+
+        tcpHandler = new TcpHandler(serverAddress, 0);
+        tcpHandler.setChannelInitializer(mockChannelInitializer);
+
+        //Use Epoll native transport
+        assertEquals("failed to start server", true, startupServer(true));
+        assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort()));
+        shutdownServer();
+    }
+
     /**
      * Test run with encryption
      * @throws InterruptedException
@@ -91,17 +128,40 @@ public class TcpHandlerTest {
      * @throws ExecutionException
      */
     @Test
-    public void testRunWithEncryption () throws InterruptedException, IOException, ExecutionException {
+    public void testRunWithEncryption() throws InterruptedException, IOException, ExecutionException {
         int serverPort = 28001;
         tcpHandler = new TcpHandler(serverAddress, serverPort);
         tcpHandler.setChannelInitializer(mockChannelInitializer);
 
-        assertEquals( "failed to start server", true, startupServer()) ;
-        assertEquals( "wrong connection count", 0, tcpHandler.getNumberOfConnections() );
-        assertEquals( "wrong port", serverPort, tcpHandler.getPort() );
-        assertEquals( "wrong address", serverAddress.getHostAddress(), tcpHandler.getAddress()) ;
+        assertEquals( "failed to start server", true, startupServer(false));
+        assertEquals( "wrong connection count", 0, tcpHandler.getNumberOfConnections());
+        assertEquals( "wrong port", serverPort, tcpHandler.getPort());
+        assertEquals( "wrong address", serverAddress.getHostAddress(), tcpHandler.getAddress());
 
-        assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort())) ;
+        assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort()));
+
+        shutdownServer();
+    }
+
+    /**
+     * Test run with encryption on Epoll native transport
+     * @throws InterruptedException
+     * @throws IOException
+     * @throws ExecutionException
+     */
+    @Test
+    public void testRunWithEncryptionOnEpoll() throws InterruptedException, IOException, ExecutionException {
+        int serverPort = 28001;
+        tcpHandler = new TcpHandler(serverAddress, serverPort);
+        tcpHandler.setChannelInitializer(mockChannelInitializer);
+
+        //Use Epoll native transport
+        assertEquals( "failed to start server", true, startupServer(true));
+        assertEquals( "wrong connection count", 0, tcpHandler.getNumberOfConnections());
+        assertEquals( "wrong port", serverPort, tcpHandler.getPort());
+        assertEquals( "wrong address", serverAddress.getHostAddress(), tcpHandler.getAddress());
+
+        assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort()));
 
         shutdownServer();
     }
@@ -123,7 +183,7 @@ public class TcpHandlerTest {
         try {
             tcpHandler = new TcpHandler(serverAddress, serverPort);
             tcpHandler.setChannelInitializer(mockChannelInitializer);
-            tcpHandler.initiateEventLoopGroups(null);
+            tcpHandler.initiateEventLoopGroups(null, false);
             tcpHandler.run();
         } catch (Exception e) {
             if (e instanceof BindException) {
@@ -134,6 +194,35 @@ public class TcpHandlerTest {
         Assert.assertTrue("Expected BindException has not been thrown", exceptionThrown == true);
     }
 
+    /**
+     * Test run on already used port
+     * @throws IOException
+     */
+    @Test
+    public void testSocketAlreadyInUseOnEpoll() throws IOException {
+        int serverPort = 28001;
+        Socket firstBinder = new Socket();
+        boolean exceptionThrown = false;
+        try {
+            firstBinder.bind(new InetSocketAddress(serverAddress, serverPort));
+        } catch (Exception e) {
+            Assert.fail("Test precondition failed - not able to bind socket to port " + serverPort);
+        }
+        try {
+            tcpHandler = new TcpHandler(serverAddress, serverPort);
+            tcpHandler.setChannelInitializer(mockChannelInitializer);
+            //Use Epoll native transport
+            tcpHandler.initiateEventLoopGroups(null, true);
+            tcpHandler.run();
+        } catch (Exception e) {
+            if (e instanceof BindException || e instanceof Errors.NativeIoException) {
+                exceptionThrown = true;
+            }
+        }
+        firstBinder.close();
+        Assert.assertTrue("Expected BindException has not been thrown", exceptionThrown == true);
+    }
+
     /**
      * Trigger the server shutdown and wait 2 seconds for completion
      */
@@ -149,9 +238,13 @@ public class TcpHandlerTest {
      * @throws IOException
      * @throws ExecutionException
      */
-    private Boolean startupServer() throws InterruptedException, IOException, ExecutionException {
+    private Boolean startupServer(boolean isEpollEnabled) throws InterruptedException, IOException, ExecutionException {
         ListenableFuture<Boolean> online = tcpHandler.getIsOnlineFuture();
-        tcpHandler.initiateEventLoopGroups(null);
+        /**
+         * Test EPoll based native transport if isEpollEnabled is true.
+         * Else use Nio based transport.
+         */
+        tcpHandler.initiateEventLoopGroups(null, isEpollEnabled);
             (new Thread(tcpHandler)).start();
             int retry = 0;
             while (online.isDone() != true && retry++ < 20) {