X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fimpl%2FConcurrentClientsTest.java;h=8d3476f4b8dfa9b08507cb0e29eb2a6393a972e9;hb=58e0b181f70b2e8bf1c8097bb804f6d1f28b00b8;hp=54a3482e3443b4e06bddd32e3a3c39ffd04ea48a;hpb=a92d9d6a21a0f6ca8d2153795721f500eaf29ee9;p=controller.git diff --git a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/ConcurrentClientsTest.java b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/ConcurrentClientsTest.java index 54a3482e34..8d3476f4b8 100644 --- a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/ConcurrentClientsTest.java +++ b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/ConcurrentClientsTest.java @@ -8,12 +8,34 @@ package org.opendaylight.controller.netconf.impl; -import com.google.common.base.Optional; -import com.google.common.collect.Sets; +import static com.google.common.base.Preconditions.checkNotNull; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; import io.netty.channel.ChannelFuture; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; import io.netty.util.HashedWheelTimer; + +import java.io.DataOutputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.management.ManagementFactory; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import javax.management.ObjectName; + import org.apache.commons.io.IOUtils; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -26,37 +48,31 @@ import org.opendaylight.controller.netconf.api.NetconfDocumentedException; import org.opendaylight.controller.netconf.api.NetconfMessage; import org.opendaylight.controller.netconf.api.NetconfOperationRouter; import org.opendaylight.controller.netconf.client.NetconfClient; +import org.opendaylight.controller.netconf.client.NetconfClientDispatcher; import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl; -import org.opendaylight.controller.netconf.mapping.api.*; +import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService; +import org.opendaylight.controller.netconf.mapping.api.Capability; +import org.opendaylight.controller.netconf.mapping.api.HandlingPriority; +import org.opendaylight.controller.netconf.mapping.api.NetconfOperation; +import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilter; +import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService; +import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory; import org.opendaylight.controller.netconf.util.test.XmlFileLoader; import org.opendaylight.controller.netconf.util.xml.XmlUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; -import javax.management.ObjectName; -import javax.net.ssl.SSLContext; -import java.io.DataOutputStream; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.lang.management.ManagementFactory; -import java.net.InetSocketAddress; -import java.net.Socket; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -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.mockito.Matchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; +import com.google.common.base.Optional; +import com.google.common.collect.Sets; public class ConcurrentClientsTest { private static final int CONCURRENCY = 16; + private static EventLoopGroup nettyGroup = new NioEventLoopGroup(); + public static final NetconfClientDispatcher NETCONF_CLIENT_DISPATCHER = + new NetconfClientDispatcher( nettyGroup, nettyGroup); + @Mock private YangStoreService yangStoreService; @Mock @@ -69,6 +85,9 @@ public class ConcurrentClientsTest { private DefaultCommitNotificationProducer commitNot; private NetconfServerDispatcher dispatch; + @Mock + private SessionMonitoringService monitoring; + @Before public void setUp() throws Exception { { // init mocks @@ -93,14 +112,23 @@ public class ConcurrentClientsTest { commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer()); + doNothing().when(monitoring).onSessionUp(any(NetconfServerSession.class)); + doNothing().when(monitoring).onSessionDown(any(NetconfServerSession.class)); + NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory( - factoriesListener, commitNot, idProvider); - dispatch = new NetconfServerDispatcher(Optional. absent(), serverNegotiatorFactory, listenerFactory); + factoriesListener, commitNot, idProvider, monitoring); + NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(serverNegotiatorFactory, listenerFactory); + dispatch = new NetconfServerDispatcher(serverChannelInitializer, nettyGroup, nettyGroup); ChannelFuture s = dispatch.createServer(netconfAddress); s.await(); } + @AfterClass + public static void tearDownStatic() { + nettyGroup.shutdownGracefully(); + } + private NetconfOperationServiceFactory mockOpF() { return new NetconfOperationServiceFactory() { @Override @@ -147,7 +175,6 @@ public class ConcurrentClientsTest { @After public void cleanUp() throws Exception { commitNot.close(); - dispatch.close(); } @Test @@ -163,7 +190,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()); + } } } @@ -183,12 +214,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 thrownException; public BlockingThread(String name) { super("client-" + name); @@ -198,10 +233,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); } } @@ -241,7 +275,7 @@ public class ConcurrentClientsTest { private final String clientId; private final int attempts; - private Boolean success; + private Optional thrownException; TestingThread(String clientId, int attempts) { this.clientId = clientId; @@ -252,7 +286,7 @@ public class ConcurrentClientsTest { @Override public void run() { try { - final NetconfClient netconfClient = new NetconfClient(clientId, netconfAddress); + final NetconfClient netconfClient = new NetconfClient(clientId, netconfAddress, NETCONF_CLIENT_DISPATCHER); long sessionId = netconfClient.getSessionId(); logger.info("Client with sessionid {} hello exchanged", sessionId); @@ -262,10 +296,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); } } }