BUG-6038: Fix race condition when Open message...
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / BGPDispatcherImplTest.java
index b24fdd7b7454bb45cb59b70b2d2cbfdff131df8a..7dfc6cd823a20ebf33375682007840fbcba8a12d 100755 (executable)
@@ -9,11 +9,11 @@
 package org.opendaylight.protocol.bgp.rib.impl;
 
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelFuture;
-import io.netty.channel.EventLoopGroup;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.util.concurrent.Future;
 import io.netty.util.concurrent.GenericFutureListener;
@@ -27,6 +27,7 @@ import org.junit.Test;
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
+import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
@@ -48,75 +49,82 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.type
 
 public class BGPDispatcherImplTest {
 
-    private static final InetSocketAddress ADDRESS = new InetSocketAddress("127.0.10.0", 1790);
-    private static final InetSocketAddress CLIENT_ADDRESS = new InetSocketAddress("127.0.11.0", 1791);
-    private static final InetSocketAddress CLIENT_ADDRESS2 = new InetSocketAddress("127.0.12.0", 1792);
     private static final AsNumber AS_NUMBER = new AsNumber(30L);
-    private static final int TIMEOUT = 5000;
     private static final int RETRY_TIMER = 10;
-
-    private final BgpTableType ipv4tt = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
-
-    private BGPDispatcherImpl dispatcher;
+    private static final BgpTableType IPV_4_TT = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
+    private BGPDispatcherImpl serverDispatcher;
     private TestClientDispatcher clientDispatcher;
-
     private BGPPeerRegistry registry;
-
-    private Channel channel;
+    private SimpleSessionListener clientListener;
+    private SimpleSessionListener serverListener;
 
     @Before
     public void setUp() throws BGPDocumentedException {
-        final EventLoopGroup group = new NioEventLoopGroup();
         this.registry = new StrictBGPPeerRegistry();
-        this.registry.addPeer(new IpAddress(new Ipv4Address(CLIENT_ADDRESS.getAddress().getHostAddress())),
-                new SimpleSessionListener(), createPreferences(CLIENT_ADDRESS));
-        this.registry.addPeer(new IpAddress(new Ipv4Address(ADDRESS.getAddress().getHostAddress())),
-                new SimpleSessionListener(), createPreferences(ADDRESS));
-        this.dispatcher = new BGPDispatcherImpl(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry(), group, group);
-        this.clientDispatcher = new TestClientDispatcher(group, group, ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry(),
-                CLIENT_ADDRESS);
+        this.clientListener = new SimpleSessionListener();
+        final BGPExtensionProviderContext ctx = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance();
+        this.serverDispatcher = new BGPDispatcherImpl(ctx.getMessageRegistry(), new NioEventLoopGroup(), new NioEventLoopGroup());
+        configureClient(ctx);
+    }
 
-        final ChannelFuture future = this.dispatcher.createServer(this.registry, ADDRESS);
+    private void configureClient(final BGPExtensionProviderContext ctx) {
+        final InetSocketAddress clientAddress = new InetSocketAddress("127.0.11.0", 1791);
+        final IpAddress clientPeerIp = new IpAddress(new Ipv4Address(clientAddress.getAddress().getHostAddress()));
+        this.registry.addPeer(clientPeerIp, this.clientListener, createPreferences(clientAddress));
+        this.clientDispatcher = new TestClientDispatcher(new NioEventLoopGroup(), new NioEventLoopGroup(), ctx.getMessageRegistry(), clientAddress);
+    }
+
+    private Channel createServer(final InetSocketAddress serverAddress) throws InterruptedException {
+        this.serverListener = new SimpleSessionListener();
+        this.registry.addPeer(new IpAddress(new Ipv4Address(serverAddress.getAddress().getHostAddress())), this.serverListener, createPreferences(serverAddress));
+        final ChannelFuture future = this.serverDispatcher.createServer(this.registry, serverAddress);
         future.addListener(new GenericFutureListener<Future<Void>>() {
             @Override
             public void operationComplete(final Future<Void> future) {
-                if(!future.isSuccess()) {
-                    Assert.fail("Failed to create server.");
-                }
+                Preconditions.checkArgument(future.isSuccess(), "Unable to start bgp server on %s", future.cause());
             }
         });
-        this.channel = future.channel();
+        return future.channel();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        this.serverDispatcher.close();
+        this.registry.close();
     }
 
     @Test
     public void testCreateClient() throws InterruptedException, ExecutionException {
-        final BGPSessionImpl session = this.clientDispatcher.createClient(ADDRESS, this.registry,
-                0, Optional.<InetSocketAddress>absent()).get();
-        Assert.assertEquals(BGPSessionImpl.State.UP, session.getState());
+        final InetSocketAddress serverAddress = new InetSocketAddress("127.0.10.0", 1790);
+        final Channel serverChannel = createServer(serverAddress);
+        Thread.sleep(1000);
+        final BGPSessionImpl session = this.clientDispatcher.createClient(serverAddress, this.registry, 0, Optional.absent()).get();
+        Thread.sleep(3000);
+        Assert.assertEquals(BGPSessionImpl.State.UP, this.clientListener.getState());
+        Assert.assertEquals(BGPSessionImpl.State.UP, this.serverListener.getState());
         Assert.assertEquals(AS_NUMBER, session.getAsNumber());
-        Assert.assertEquals(Sets.newHashSet(this.ipv4tt), session.getAdvertisedTableTypes());
+        Assert.assertEquals(Sets.newHashSet(IPV_4_TT), session.getAdvertisedTableTypes());
+        Assert.assertTrue(serverChannel.isWritable());
         session.close();
-    }
 
-    @After
-    public void tearDown() throws Exception {
-        this.channel.close().get();
-        this.dispatcher.close();
-        this.registry.close();
+        Thread.sleep(3000);
+        Assert.assertEquals(BGPSessionImpl.State.IDLE, this.clientListener.getState());
+        Assert.assertEquals(BGPSessionImpl.State.IDLE, this.serverListener.getState());
     }
 
     @Test
-    public void testCreateReconnectingClient() throws InterruptedException, ExecutionException {
-        final SimpleSessionListener listener = new SimpleSessionListener();
-        this.registry.addPeer(new IpAddress(new Ipv4Address(CLIENT_ADDRESS2.getAddress().getHostAddress())), listener, createPreferences(CLIENT_ADDRESS2));
-        final Future<Void> cf = this.clientDispatcher.createReconnectingClient(CLIENT_ADDRESS2, this.registry,
-                RETRY_TIMER, Optional.<InetSocketAddress>absent());
-        final Channel channel2 = this.dispatcher.createServer(this.registry, CLIENT_ADDRESS2).channel();
+    public void testCreateReconnectingClient() throws Exception {
+        final InetSocketAddress serverAddress = new InetSocketAddress("127.0.20.0", 1792);
+        final Channel serverChannel = createServer(serverAddress);
         Thread.sleep(1000);
-        Assert.assertTrue(listener.up);
-        Assert.assertTrue(channel2.isActive());
-        cf.cancel(true);
-        listener.releaseConnection();
+        final Future<Void> future = this.clientDispatcher.createReconnectingClient(serverAddress, this.registry, RETRY_TIMER, Optional.absent());
+        Thread.sleep(3000);
+        Assert.assertEquals(BGPSessionImpl.State.UP, this.serverListener.getState());
+        Assert.assertTrue(serverChannel.isWritable());
+        future.cancel(true);
+        this.serverListener.releaseConnection();
+        Thread.sleep(3000);
+        Assert.assertEquals(BGPSessionImpl.State.IDLE, this.serverListener.getState());
     }
 
     private BGPSessionPreferences createPreferences(final InetSocketAddress socketAddress) {
@@ -124,7 +132,7 @@ public class BGPDispatcherImplTest {
         final List<OptionalCapabilities> capas = Lists.newArrayList();
         capas.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(
             CParameters1.class, new CParameters1Builder().setMultiprotocolCapability(new MultiprotocolCapabilityBuilder()
-                .setAfi(this.ipv4tt.getAfi()).setSafi(this.ipv4tt.getSafi()).build()).build())
+                .setAfi(IPV_4_TT.getAfi()).setSafi(IPV_4_TT.getSafi()).build()).build())
             .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(new AsNumber(30L)).build())
             .build()).build());
         capas.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());