Merge "Enabling notification from netconf client"
[controller.git] / opendaylight / netconf / netconf-impl / src / test / java / org / opendaylight / controller / netconf / impl / ConcurrentClientsTest.java
index a74a347d3bbf5d29811ddf28dd2c9c825cb460cc..b363976aaed6b6afd8f3aa4c9844ffb5357a661c 100644 (file)
@@ -11,6 +11,8 @@ package org.opendaylight.controller.netconf.impl;
 import com.google.common.base.Optional;
 import com.google.common.collect.Sets;
 import io.netty.channel.ChannelFuture;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.util.HashedWheelTimer;
 import org.apache.commons.io.IOUtils;
 import org.junit.After;
@@ -56,7 +58,7 @@ import java.util.Set;
 import java.util.concurrent.TimeUnit;
 
 import static com.google.common.base.Preconditions.checkNotNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
@@ -64,7 +66,10 @@ import static org.mockito.Mockito.mock;
 public class ConcurrentClientsTest {
 
     private static final int CONCURRENCY = 16;
-    public static final NetconfClientDispatcher NETCONF_CLIENT_DISPATCHER = new NetconfClientDispatcher(Optional.<SSLContext>absent());
+    private static EventLoopGroup nettyGroup = new NioEventLoopGroup();
+    public static final NetconfClientDispatcher NETCONF_CLIENT_DISPATCHER = new NetconfClientDispatcher(
+            Optional.<SSLContext> absent(), nettyGroup, nettyGroup);
+
     @Mock
     private YangStoreService yangStoreService;
     @Mock
@@ -77,6 +82,7 @@ public class ConcurrentClientsTest {
     private DefaultCommitNotificationProducer commitNot;
     private NetconfServerDispatcher dispatch;
 
+
     @Before
     public void setUp() throws Exception {
         { // init mocks
@@ -103,7 +109,9 @@ public class ConcurrentClientsTest {
 
         NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
                 factoriesListener, commitNot, idProvider);
-        dispatch = new NetconfServerDispatcher(Optional.<SSLContext> absent(), serverNegotiatorFactory, listenerFactory);
+        NetconfServerDispatcher.ServerSslChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerSslChannelInitializer(
+                Optional.<SSLContext> absent(), serverNegotiatorFactory, listenerFactory);
+        dispatch = new NetconfServerDispatcher(serverChannelInitializer, nettyGroup, nettyGroup);
 
         ChannelFuture s = dispatch.createServer(netconfAddress);
         s.await();
@@ -111,7 +119,7 @@ public class ConcurrentClientsTest {
 
     @AfterClass
     public static void tearDownStatic() {
-        NETCONF_CLIENT_DISPATCHER.close();
+        nettyGroup.shutdownGracefully();
     }
 
     private NetconfOperationServiceFactory mockOpF() {
@@ -160,7 +168,6 @@ public class ConcurrentClientsTest {
     @After
     public void cleanUp() throws Exception {
         commitNot.close();
-        dispatch.close();
     }
 
     @Test
@@ -176,7 +183,11 @@ public class ConcurrentClientsTest {
 
         for (TestingThread thread : threads) {
             thread.join();
-            assertTrue(thread.success);
+            if(thread.thrownException.isPresent()) {
+                Exception exception = thread.thrownException.get();
+                logger.error("Thread for testing client failed", exception);
+                fail("Client thread " + thread + " failed: " + exception.getMessage());
+            }
         }
     }
 
@@ -196,12 +207,16 @@ public class ConcurrentClientsTest {
 
         for (BlockingThread thread : threads) {
             thread.join();
-            assertTrue(thread.success);
+            if(thread.thrownException.isPresent()) {
+                Exception exception = thread.thrownException.get();
+                logger.error("Thread for testing client failed", exception);
+                fail("Client thread " + thread + " failed: " + exception.getMessage());
+            }
         }
     }
 
     class BlockingThread extends Thread {
-        Boolean success;
+        private Optional<Exception> thrownException;
 
         public BlockingThread(String name) {
             super("client-" + name);
@@ -211,10 +226,9 @@ public class ConcurrentClientsTest {
         public void run() {
             try {
                 run2();
-                success = true;
+                thrownException = Optional.absent();
             } catch (Exception e) {
-                success = false;
-                throw new RuntimeException(e);
+                thrownException = Optional.of(e);
             }
         }
 
@@ -254,7 +268,7 @@ public class ConcurrentClientsTest {
 
         private final String clientId;
         private final int attempts;
-        private Boolean success;
+        private Optional<Exception> thrownException;
 
         TestingThread(String clientId, int attempts) {
             this.clientId = clientId;
@@ -275,10 +289,9 @@ public class ConcurrentClientsTest {
                 logger.info("Client with sessionid {} got result {}", sessionId, result);
                 netconfClient.close();
                 logger.info("Client with session id {} ended", sessionId);
-                success = true;
+                thrownException = Optional.absent();
             } catch (final Exception e) {
-                success = false;
-                throw new RuntimeException(e);
+                thrownException = Optional.of(e);
             }
         }
     }