Fixed hard-coded port 12345 in TestClient 51/38351/1
authorMichael Vorburger <vorburger@redhat.com>
Tue, 3 May 2016 23:36:52 +0000 (01:36 +0200)
committerMichael Vorburger <vorburger@redhat.com>
Tue, 3 May 2016 23:36:52 +0000 (01:36 +0200)
This avoids java.net.BindException: Address already in use

Change-Id: I15ca5fe4f5e381c40b4bf5eaddcc3ada4ca4d886
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
library/impl/src/test/java/org/opendaylight/ovsdb/lib/jsonrpc/NettyBootStrapper.java
library/impl/src/test/java/org/opendaylight/ovsdb/lib/jsonrpc/TestClient.java

index 7c4d7e8bac2a823c76bccd0099997fb22e0310d4..486613143f39c0a6f0d452c53eccd3d37dc7918b 100644 (file)
@@ -9,6 +9,10 @@
  */
 package org.opendaylight.ovsdb.lib.jsonrpc;
 
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.util.concurrent.TimeUnit;
+
 import io.netty.bootstrap.ServerBootstrap;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelHandler;
@@ -18,8 +22,7 @@ import io.netty.channel.EventLoopGroup;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.SocketChannel;
 import io.netty.channel.socket.nio.NioServerSocketChannel;
-
-import java.util.concurrent.TimeUnit;
+import org.apache.commons.lang3.NotImplementedException;
 
 public class NettyBootStrapper {
 
@@ -72,4 +75,14 @@ public class NettyBootStrapper {
         }
     }
 
+    public int getServerPort() {
+        SocketAddress socketAddress = f.channel().localAddress();
+        if (socketAddress instanceof InetSocketAddress) {
+            InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
+            return inetSocketAddress.getPort();
+        } else {
+            throw new NotImplementedException("Please implement how to obtain port from a " + socketAddress.toString());
+        }
+    }
+
 }
index 39cd1a1b247301e5de09e1d9d7630f6c87c97aff..22b1285f0b50f187b5f6d3eb2f1fa72d396cb660 100644 (file)
@@ -21,13 +21,11 @@ import java.net.Socket;
 public class TestClient extends TestCase {
 
         String serverurl = "127.0.0.1";
-        int serverport = 12345;
-
         NettyBootStrapper bootstrapper = new NettyBootStrapper();
         JsonRpcDecoder jsonRpcDecoder = new JsonRpcDecoder(100000);
 
         public void setupServer() throws Exception {
-            bootstrapper.startServer(serverport,
+            bootstrapper.startServer(0,
                     jsonRpcDecoder,
                     new LoggingHandler(LogLevel.DEBUG));
         }
@@ -38,15 +36,14 @@ public class TestClient extends TestCase {
 
         /**
          * Testing appropriate ChannelHandler integration for
-         * JsonRpcDecoder, so that Json strings written using an
+         * JsonRpcDecoder, so that JSON strings written using an
          * OutputStream connected to a ServerSocket of a Netty
-         * ServerBootstrap can be decoder properly.
+         * ServerBootstrap can be decoded properly.
          */
-
         @Test
         public void testBasicFlow() throws Exception {
             setupServer();
-            Socket socket = new Socket(serverurl, serverport);
+            Socket socket = new Socket(serverurl, bootstrapper.getServerPort());
             OutputStream outputStream = socket.getOutputStream();
 
             int records = 20;