From 257ee3c3c96e5f15b39c7ca0fdbf4f436517a766 Mon Sep 17 00:00:00 2001 From: "Claudio D. Gasparini" Date: Thu, 10 Nov 2016 13:59:51 +0100 Subject: [PATCH] BUG-7003: Remove Thread.sleep() calls in tests Partial improvement under Additional Paths tests. The time spent has been reduced. Change-Id: I8ebfd19af9147a10f1d5856b5478501f2ab890ab Signed-off-by: Claudio D. Gasparini --- .../bgp/rib/impl/AbstractAddPathTest.java | 67 +++++++------------ .../bgp/rib/impl/AddPathAllPathsTest.java | 55 +++++++-------- .../bgp/rib/impl/AddPathBasePathsTest.java | 55 +++++++-------- .../bgp/rib/impl/AddPathNPathsTest.java | 54 +++++++-------- .../bgp/rib/impl/BGPDispatcherImplTest.java | 1 - .../protocol/bgp/rib/impl/CheckUtil.java | 2 +- 6 files changed, 101 insertions(+), 133 deletions(-) diff --git a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AbstractAddPathTest.java b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AbstractAddPathTest.java index 8045a3577d..20edea6e66 100644 --- a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AbstractAddPathTest.java +++ b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AbstractAddPathTest.java @@ -9,11 +9,10 @@ package org.opendaylight.protocol.bgp.rib.impl; import static org.mockito.Matchers.any; import static org.mockito.Mockito.doReturn; +import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.readData; import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.waitFutureSuccess; -import static org.opendaylight.protocol.bgp.rib.spi.RouterIds.createPeerId; import com.google.common.base.Optional; -import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.google.common.net.InetAddresses; import io.netty.channel.epoll.Epoll; @@ -31,15 +30,12 @@ import org.junit.Before; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec; import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest; -import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService; import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider; import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration; import org.opendaylight.protocol.bgp.inet.RIBActivator; -import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl; import org.opendaylight.protocol.bgp.parser.impl.BGPActivator; import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext; import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext; @@ -77,7 +73,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mess import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.WithdrawnRoutesBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1Builder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.SendReceive; @@ -90,10 +85,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mult import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutes; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.BgpRib; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier; @@ -111,8 +104,7 @@ import org.opendaylight.yangtools.yang.binding.util.BindingReflections; import org.opendaylight.yangtools.yang.model.api.SchemaContext; class AbstractAddPathTest extends AbstractDataBrokerTest { - protected static final List TABLES = ImmutableList.of(new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class)); - protected static final TablesKey TABLE_KEY = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class); + static final int RETRY_TIMER = 10; static final String RIB_ID = "127.0.0.1"; static final Ipv4Address PEER1 = new Ipv4Address("127.0.0.2"); static final Ipv4Address PEER2 = new Ipv4Address("127.0.0.3"); @@ -124,11 +116,6 @@ class AbstractAddPathTest extends AbstractDataBrokerTest { static final int PORT = InetSocketAddressUtil.getRandomPort(); static final Ipv4Prefix PREFIX1 = new Ipv4Prefix("1.1.1.1/32"); private static final ClusterIdentifier CLUSTER_ID = new ClusterIdentifier(RIB_ID); - private static final PeerId PEER1_ID = createPeerId(PEER1); - private static final PeerId PEER2_ID = createPeerId(PEER2); - private static final PeerId PEER3_ID = createPeerId(PEER3); - private static final PeerId PEER4_ID = createPeerId(PEER4); - private static final PeerId PEER5_ID = createPeerId(PEER5); private static final int HOLDTIMER = 2180; private static final Ipv4Address NH1 = new Ipv4Address("2.2.2.2"); static final Update UPD_100 = createSimpleUpdate(PREFIX1, new PathId(1L), CLUSTER_ID, 100); @@ -140,6 +127,7 @@ class AbstractAddPathTest extends AbstractDataBrokerTest { static final Update UPD_NA_200 = createSimpleUpdate(PREFIX1, null, CLUSTER_ID, 200); static final Update UPD_NA_200_EBGP = createSimpleUpdateEbgp(PREFIX1, null); protected BGPExtensionProviderContext context; + private static final InstanceIdentifier BGP_IID = InstanceIdentifier.create(BgpRib.class); protected SchemaContext schemaContext; @Mock protected ClusterSingletonServiceProvider clusterSingletonServiceProvider; @@ -201,43 +189,38 @@ class AbstractAddPathTest extends AbstractDataBrokerTest { } void sendRouteAndCheckIsOnLocRib(final BGPSessionImpl session, final Ipv4Prefix prefix, final long localPreference, final int expectedRoutesOnDS) - throws InterruptedException, ExecutionException { - session.writeAndFlush(createSimpleUpdate(prefix, null, null, localPreference)); - Thread.sleep(2000); + throws Exception { + waitFutureSuccess(session.writeAndFlush(createSimpleUpdate(prefix, null, null, localPreference))); checkLocRib(expectedRoutesOnDS); - } void sendWithdrawalRouteAndCheckIsOnLocRib(final BGPSessionImpl session, final Ipv4Prefix prefix, final long localPreference, final int expectedRoutesOnDS) - throws InterruptedException, ExecutionException { - session.writeAndFlush(createSimpleWithdrawalUpdate(prefix, localPreference)); - Thread.sleep(2000); + throws Exception { + waitFutureSuccess(session.writeAndFlush(createSimpleWithdrawalUpdate(prefix, localPreference))); checkLocRib(expectedRoutesOnDS); } - private void checkLocRib(final int expectedRoutesOnDS) throws ExecutionException, InterruptedException { - final ReadOnlyTransaction rTx = getDataBroker().newReadOnlyTransaction(); - final BgpRib bgpRib = rTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(BgpRib.class)).get().get(); - rTx.close(); - final Ipv4RoutesCase routes = ((Ipv4RoutesCase) bgpRib.getRib().get(0).getLocRib().getTables().get(0).getRoutes()); - final List routeList = routes.getIpv4Routes().getIpv4Route(); - Assert.assertEquals(expectedRoutesOnDS, routeList.size()); + private void checkLocRib(final int expectedRoutesOnDS) throws Exception { + Thread.sleep(100); + readData(getDataBroker(), BGP_IID, bgpRib -> { + final Ipv4RoutesCase routes = ((Ipv4RoutesCase) bgpRib.getRib().get(0).getLocRib().getTables().get(0).getRoutes()); + final List routeList = routes.getIpv4Routes().getIpv4Route(); + Assert.assertEquals(expectedRoutesOnDS, routeList.size()); + return bgpRib; + }); } - void checkPeersPresentOnDataStore(final int numberOfPeers) throws ExecutionException, InterruptedException { - Thread.sleep(1000); - final ReadOnlyTransaction rTx = getDataBroker().newReadOnlyTransaction(); - final BgpRib bgpRib = rTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(BgpRib.class)).get().get(); - rTx.close(); - - //check 5 peers present in the DS - Assert.assertEquals(numberOfPeers, bgpRib.getRib().get(0).getPeer().size()); + void checkPeersPresentOnDataStore(final int numberOfPeers) throws Exception { + readData(getDataBroker(), BGP_IID, bgpRib -> { + Assert.assertEquals(numberOfPeers, bgpRib.getRib().get(0).getPeer().size()); + return bgpRib; + }); } BGPSessionImpl createPeerSession(final Ipv4Address peer, final PeerRole peerRole, final BgpParameters nonAddPathParams, final RIBImpl ribImpl, - final BGPHandlerFactory hf, final SimpleSessionListener sessionListener) throws InterruptedException, ExecutionException { + final SimpleSessionListener sessionListener) throws InterruptedException, ExecutionException { configurePeer(peer, ribImpl, nonAddPathParams, peerRole); - return connectPeer(peer, nonAddPathParams, this.dispatcher, hf, sessionListener); + return connectPeer(peer, nonAddPathParams, this.dispatcher, sessionListener); } private static int getPeerRibOutSize(final Peer peer) { @@ -255,15 +238,15 @@ class AbstractAddPathTest extends AbstractDataBrokerTest { } private static BGPSessionImpl connectPeer(final Ipv4Address localAddress, final BgpParameters bgpParameters, - final BGPDispatcherImpl dispatcherImpl, final BGPHandlerFactory hf, final BGPSessionListener sessionListener) throws InterruptedException { + final BGPDispatcherImpl dispatcherImpl, final BGPSessionListener sessionListener) throws InterruptedException { final BGPPeerRegistry peerRegistry = new StrictBGPPeerRegistry(); peerRegistry.addPeer(new IpAddress(new Ipv4Address(RIB_ID)), sessionListener, new BGPSessionPreferences(AS_NUMBER, HOLDTIMER, new BgpId(localAddress), AS_NUMBER, Lists.newArrayList(bgpParameters), Optional.absent())); final Future future = dispatcherImpl.createClient(new InetSocketAddress(localAddress.getValue(), PORT), - new InetSocketAddress(RIB_ID, PORT), peerRegistry, 2, true); - Thread.sleep(1000); + new InetSocketAddress(RIB_ID, PORT), peerRegistry, RETRY_TIMER, true); + Thread.sleep(200); waitFutureSuccess(future); final BGPSessionImpl client = future.getNow(); return client; diff --git a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AddPathAllPathsTest.java b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AddPathAllPathsTest.java index 6fb2496f64..2e02469b2a 100644 --- a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AddPathAllPathsTest.java +++ b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AddPathAllPathsTest.java @@ -8,6 +8,8 @@ package org.opendaylight.protocol.bgp.rib.impl; import static org.junit.Assert.assertEquals; +import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.checkReceivedMessages; +import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.waitFutureSuccess; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -53,79 +55,72 @@ public class AddPathAllPathsTest extends AbstractAddPathTest { ribImpl.instantiateServiceInstance(); ribImpl.onGlobalContextUpdated(this.schemaContext); - this.dispatcher.createServer(StrictBGPPeerRegistry.GLOBAL, new InetSocketAddress(RIB_ID, PORT)).sync(); - Thread.sleep(1000); - - - final BGPHandlerFactory hf = new BGPHandlerFactory(this.context.getMessageRegistry()); + waitFutureSuccess(this.dispatcher.createServer(StrictBGPPeerRegistry.GLOBAL, new InetSocketAddress(RIB_ID, PORT))); final BgpParameters nonAddPathParams = createParameter(false); final BgpParameters addPathParams = createParameter(true); - final BGPSessionImpl session1 = createPeerSession(PEER1, PeerRole.Ibgp, nonAddPathParams, ribImpl, hf, new SimpleSessionListener()); - final BGPSessionImpl session2 = createPeerSession(PEER2, PeerRole.Ibgp, nonAddPathParams, ribImpl, hf, new SimpleSessionListener()); - final BGPSessionImpl session3 = createPeerSession(PEER3, PeerRole.Ibgp, nonAddPathParams, ribImpl, hf, new SimpleSessionListener()); + final BGPSessionImpl session1 = createPeerSession(PEER1, PeerRole.Ibgp, nonAddPathParams, ribImpl, new SimpleSessionListener()); + final BGPSessionImpl session2 = createPeerSession(PEER2, PeerRole.Ibgp, nonAddPathParams, ribImpl, new SimpleSessionListener()); + final BGPSessionImpl session3 = createPeerSession(PEER3, PeerRole.Ibgp, nonAddPathParams, ribImpl, new SimpleSessionListener()); final SimpleSessionListener listener4 = new SimpleSessionListener(); - final BGPSessionImpl session4 = createPeerSession(PEER4, PeerRole.RrClient, nonAddPathParams, ribImpl, hf, listener4); + final BGPSessionImpl session4 = createPeerSession(PEER4, PeerRole.RrClient, nonAddPathParams, ribImpl, listener4); final SimpleSessionListener listener5 = new SimpleSessionListener(); - final BGPSessionImpl session5 = createPeerSession(PEER5, PeerRole.RrClient, addPathParams, ribImpl, hf, listener5); - Thread.sleep(1000); + final BGPSessionImpl session5 = createPeerSession(PEER5, PeerRole.RrClient, addPathParams, ribImpl, listener5); checkPeersPresentOnDataStore(5); //the best route sendRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 1); - assertEquals(1, listener4.getListMsg().size()); - assertEquals(1, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 1); + checkReceivedMessages(listener5, 1); assertEquals(UPD_100, listener5.getListMsg().get(0)); final SimpleSessionListener listener6 = new SimpleSessionListener(); - final BGPSessionImpl session6 = createPeerSession(PEER6, PeerRole.RrClient, nonAddPathParams, ribImpl, hf, listener6); - Thread.sleep(1000); + final BGPSessionImpl session6 = createPeerSession(PEER6, PeerRole.RrClient, nonAddPathParams, ribImpl, listener6); checkPeersPresentOnDataStore(6); - assertEquals(1, listener6.getListMsg().size()); + checkReceivedMessages(listener6, 1); assertEquals(UPD_NA_100, listener6.getListMsg().get(0)); session6.close(); - Thread.sleep(1000); + checkPeersPresentOnDataStore(5); //the second best route sendRouteAndCheckIsOnLocRib(session2, PREFIX1, 50, 2); - assertEquals(1, listener4.getListMsg().size()); - assertEquals(2, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 1); + checkReceivedMessages(listener5, 2); assertEquals(UPD_50, listener5.getListMsg().get(1)); //new best route sendRouteAndCheckIsOnLocRib(session3, PREFIX1, 200, 3); - assertEquals(2, listener4.getListMsg().size()); - assertEquals(3, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 2); + checkReceivedMessages(listener5, 3); assertEquals(UPD_200, listener5.getListMsg().get(2)); //the worst route sendRouteAndCheckIsOnLocRib(session1, PREFIX1, 20, 3); - assertEquals(2, listener4.getListMsg().size()); - assertEquals(4, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 2); + checkReceivedMessages(listener5, 4); assertEquals(UPD_200.getAttributes().getLocalPref(), ((Update) listener4.getListMsg().get(1)).getAttributes().getLocalPref()); assertEquals(UPD_20, listener5.getListMsg().get(3)); //withdraw second best route, 1 advertisement(1 withdrawal) for add-path supported, none for non add path sendWithdrawalRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 2); - assertEquals(2, listener4.getListMsg().size()); - assertEquals(5, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 2); + checkReceivedMessages(listener5, 5); //we advertise again to try new test sendRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 3); - assertEquals(2, listener4.getListMsg().size()); - assertEquals(6, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 2); + checkReceivedMessages(listener5, 6); assertEquals(UPD_200, listener5.getListMsg().get(2)); //withdraw second best route, 1 advertisement(1 withdrawal) for add-path supported, 1 for non add path (withdrawal) sendWithdrawalRouteAndCheckIsOnLocRib(session3, PREFIX1, 200, 2); - assertEquals(3, listener4.getListMsg().size()); - assertEquals(7, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 3); + checkReceivedMessages(listener5, 7); session1.close(); session2.close(); session3.close(); session4.close(); session5.close(); - Thread.sleep(1000); } } diff --git a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AddPathBasePathsTest.java b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AddPathBasePathsTest.java index 365f3f351a..689ec9f0bf 100644 --- a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AddPathBasePathsTest.java +++ b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AddPathBasePathsTest.java @@ -9,6 +9,8 @@ package org.opendaylight.protocol.bgp.rib.impl; import static org.junit.Assert.assertEquals; +import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.checkReceivedMessages; +import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.waitFutureSuccess; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -52,78 +54,71 @@ public class AddPathBasePathsTest extends AbstractAddPathTest { ribImpl.instantiateServiceInstance(); ribImpl.onGlobalContextUpdated(this.schemaContext); - this.dispatcher.createServer(StrictBGPPeerRegistry.GLOBAL, new InetSocketAddress(RIB_ID, PORT)).sync(); - Thread.sleep(1000); - - final BGPHandlerFactory hf = new BGPHandlerFactory(this.context.getMessageRegistry()); + waitFutureSuccess(this.dispatcher.createServer(StrictBGPPeerRegistry.GLOBAL, new InetSocketAddress(RIB_ID, PORT))); final BgpParameters nonAddPathParams = createParameter(false); - final BGPSessionImpl session1 = createPeerSession(PEER1, PeerRole.Ibgp, nonAddPathParams, ribImpl, hf, new SimpleSessionListener()); - final BGPSessionImpl session2 = createPeerSession(PEER2, PeerRole.Ibgp, nonAddPathParams, ribImpl, hf, new SimpleSessionListener()); - final BGPSessionImpl session3 = createPeerSession(PEER3, PeerRole.Ibgp, nonAddPathParams, ribImpl, hf, new SimpleSessionListener()); + final BGPSessionImpl session1 = createPeerSession(PEER1, PeerRole.Ibgp, nonAddPathParams, ribImpl, new SimpleSessionListener()); + final BGPSessionImpl session2 = createPeerSession(PEER2, PeerRole.Ibgp, nonAddPathParams, ribImpl, new SimpleSessionListener()); + final BGPSessionImpl session3 = createPeerSession(PEER3, PeerRole.Ibgp, nonAddPathParams, ribImpl, new SimpleSessionListener()); final SimpleSessionListener listener4 = new SimpleSessionListener(); - final BGPSessionImpl session4 = createPeerSession(PEER4, PeerRole.RrClient, nonAddPathParams, ribImpl, hf, listener4); + final BGPSessionImpl session4 = createPeerSession(PEER4, PeerRole.RrClient, nonAddPathParams, ribImpl, listener4); final SimpleSessionListener listener5 = new SimpleSessionListener(); - final BGPSessionImpl session5 = createPeerSession(PEER5, PeerRole.Ebgp, nonAddPathParams, ribImpl, hf, listener5); - Thread.sleep(1000); + final BGPSessionImpl session5 = createPeerSession(PEER5, PeerRole.Ebgp, nonAddPathParams, ribImpl, listener5); checkPeersPresentOnDataStore(5); //new best route so far sendRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 1); - assertEquals(1, listener4.getListMsg().size()); - assertEquals(1, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 1); + checkReceivedMessages(listener5, 1); assertEquals(UPD_NA_100, listener4.getListMsg().get(0)); assertEquals(UPD_NA_100_EBGP, listener5.getListMsg().get(0)); //the second best route sendRouteAndCheckIsOnLocRib(session2, PREFIX1, 50, 1); - assertEquals(1, listener4.getListMsg().size()); - assertEquals(1, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 1); + checkReceivedMessages(listener5, 1); //new best route sendRouteAndCheckIsOnLocRib(session3, PREFIX1, 200, 1); - assertEquals(2, listener4.getListMsg().size()); - assertEquals(2, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 2); + checkReceivedMessages(listener5, 2); assertEquals(UPD_NA_200, listener4.getListMsg().get(1)); assertEquals(UPD_NA_200_EBGP, listener5.getListMsg().get(1)); final SimpleSessionListener listener6 = new SimpleSessionListener(); - final BGPSessionImpl session6 = createPeerSession(PEER6, PeerRole.RrClient, nonAddPathParams, ribImpl, hf, listener6); - Thread.sleep(1000); + final BGPSessionImpl session6 = createPeerSession(PEER6, PeerRole.RrClient, nonAddPathParams, ribImpl, listener6); checkPeersPresentOnDataStore(6); - assertEquals(1, listener6.getListMsg().size()); + checkReceivedMessages(listener6, 1); assertEquals(UPD_NA_200, listener6.getListMsg().get(0)); - Thread.sleep(1000); session6.close(); - Thread.sleep(1000); + checkPeersPresentOnDataStore(5); //best route updated to be the worse one sendRouteAndCheckIsOnLocRib(session3, PREFIX1, 20, 1); - assertEquals(3, listener4.getListMsg().size()); - assertEquals(3, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 3); + checkReceivedMessages(listener5, 3); assertEquals(UPD_NA_100, listener4.getListMsg().get(2)); assertEquals(UPD_NA_100_EBGP, listener5.getListMsg().get(2)); //Remove second best, no advertisement should be done sendWithdrawalRouteAndCheckIsOnLocRib(session2, PREFIX1, 50, 1); - assertEquals(3, listener4.getListMsg().size()); - assertEquals(3, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 3); + checkReceivedMessages(listener5, 3); //Remove best, 1 advertisement sendWithdrawalRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 1); - assertEquals(4, listener4.getListMsg().size()); - assertEquals(4, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 4); + checkReceivedMessages(listener5, 4); //Remove best, 1 withdrawal sendWithdrawalRouteAndCheckIsOnLocRib(session3, PREFIX1, 20, 0); - assertEquals(5, listener4.getListMsg().size()); - assertEquals(5, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 5); + checkReceivedMessages(listener5, 5); session1.close(); session2.close(); session3.close(); session4.close(); session5.close(); - Thread.sleep(1000); } } diff --git a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AddPathNPathsTest.java b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AddPathNPathsTest.java index ebc4404551..8333c727b8 100644 --- a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AddPathNPathsTest.java +++ b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/AddPathNPathsTest.java @@ -9,6 +9,8 @@ package org.opendaylight.protocol.bgp.rib.impl; import static org.junit.Assert.assertEquals; +import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.checkReceivedMessages; +import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.waitFutureSuccess; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -53,75 +55,69 @@ public class AddPathNPathsTest extends AbstractAddPathTest { ribImpl.instantiateServiceInstance(); ribImpl.onGlobalContextUpdated(this.schemaContext); - this.dispatcher.createServer(StrictBGPPeerRegistry.GLOBAL, new InetSocketAddress(RIB_ID, PORT)).sync(); - Thread.sleep(1000); - - final BGPHandlerFactory hf = new BGPHandlerFactory(this.context.getMessageRegistry()); + waitFutureSuccess(this.dispatcher.createServer(StrictBGPPeerRegistry.GLOBAL, new InetSocketAddress(RIB_ID, PORT))); final BgpParameters nonAddPathParams = createParameter(false); final BgpParameters addPathParams = createParameter(true); - final BGPSessionImpl session1 = createPeerSession(PEER1, PeerRole.Ibgp, nonAddPathParams, ribImpl, hf, new SimpleSessionListener()); - final BGPSessionImpl session2 = createPeerSession(PEER2, PeerRole.Ibgp, nonAddPathParams, ribImpl, hf, new SimpleSessionListener()); - final BGPSessionImpl session3 = createPeerSession(PEER3, PeerRole.Ibgp, nonAddPathParams, ribImpl, hf, new SimpleSessionListener()); + final BGPSessionImpl session1 = createPeerSession(PEER1, PeerRole.Ibgp, nonAddPathParams, ribImpl, new SimpleSessionListener()); + final BGPSessionImpl session2 = createPeerSession(PEER2, PeerRole.Ibgp, nonAddPathParams, ribImpl, new SimpleSessionListener()); + final BGPSessionImpl session3 = createPeerSession(PEER3, PeerRole.Ibgp, nonAddPathParams, ribImpl, new SimpleSessionListener()); final SimpleSessionListener listener4 = new SimpleSessionListener(); - final BGPSessionImpl session4 = createPeerSession(PEER4, PeerRole.RrClient, nonAddPathParams, ribImpl, hf, listener4); + final BGPSessionImpl session4 = createPeerSession(PEER4, PeerRole.RrClient, nonAddPathParams, ribImpl, listener4); final SimpleSessionListener listener5 = new SimpleSessionListener(); - final BGPSessionImpl session5 = createPeerSession(PEER5, PeerRole.RrClient, addPathParams, ribImpl, hf, listener5); - Thread.sleep(1000); + final BGPSessionImpl session5 = createPeerSession(PEER5, PeerRole.RrClient, addPathParams, ribImpl, listener5); checkPeersPresentOnDataStore(5); //new best route so far sendRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 1); - assertEquals(1, listener4.getListMsg().size()); - assertEquals(1, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 1); + checkReceivedMessages(listener5, 1); assertEquals(UPD_100, listener5.getListMsg().get(0)); final SimpleSessionListener listener6 = new SimpleSessionListener(); - final BGPSessionImpl session6 = createPeerSession(PEER6, PeerRole.RrClient, nonAddPathParams, ribImpl, hf, listener6); - Thread.sleep(1000); + final BGPSessionImpl session6 = createPeerSession(PEER6, PeerRole.RrClient, nonAddPathParams, ribImpl, listener6); checkPeersPresentOnDataStore(6); - assertEquals(1, listener6.getListMsg().size()); + checkReceivedMessages(listener6, 1); assertEquals(UPD_NA_100, listener6.getListMsg().get(0)); session6.close(); - Thread.sleep(1000); + checkPeersPresentOnDataStore(5); //the second best route sendRouteAndCheckIsOnLocRib(session2, PREFIX1, 50, 2); - assertEquals(1, listener4.getListMsg().size()); - assertEquals(2, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 1); + checkReceivedMessages(listener5, 2); assertEquals(UPD_50, listener5.getListMsg().get(1)); //new best route sendRouteAndCheckIsOnLocRib(session3, PREFIX1, 200, 2); - assertEquals(2, listener4.getListMsg().size()); - assertEquals(3, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 2); + checkReceivedMessages(listener5, 3); assertEquals(UPD_200, listener5.getListMsg().get(2)); //the worst prefix, no changes sendRouteAndCheckIsOnLocRib(session2, PREFIX1, 20, 2); - assertEquals(2, listener4.getListMsg().size()); - assertEquals(3, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 2); + checkReceivedMessages(listener5, 3); //withdraw second best route, 2 advertisement (1 withdrawal) for add-path supported, none for non add path sendWithdrawalRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 2); - assertEquals(2, listener4.getListMsg().size()); - assertEquals(5, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 2); + checkReceivedMessages(listener5, 5); //we advertise again to try new test sendRouteAndCheckIsOnLocRib(session1, PREFIX1, 100, 2); - assertEquals(2, listener4.getListMsg().size()); - assertEquals(6, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 2); + checkReceivedMessages(listener5, 6); //withdraw second best route, 2 advertisement (1 withdrawal) for add-path supported, 1 withdrawal for non add path sendWithdrawalRouteAndCheckIsOnLocRib(session3, PREFIX1, 200, 2); - assertEquals(3, listener4.getListMsg().size()); - assertEquals(8, listener5.getListMsg().size()); + checkReceivedMessages(listener4, 3); + checkReceivedMessages(listener5, 8); session1.close(); session2.close(); session3.close(); session4.close(); session5.close(); - Thread.sleep(1000); } } diff --git a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/BGPDispatcherImplTest.java b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/BGPDispatcherImplTest.java index f31c3dbb2f..612489c4c0 100755 --- a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/BGPDispatcherImplTest.java +++ b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/BGPDispatcherImplTest.java @@ -24,7 +24,6 @@ public class BGPDispatcherImplTest extends AbstractBGPDispatcherTest { public void testCreateClient() throws InterruptedException, ExecutionException { final InetSocketAddress serverAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(); final Channel serverChannel = createServer(serverAddress); - Thread.sleep(1000); final Future futureClient = this.clientDispatcher.createClient(this.clientAddress, serverAddress, this.registry, 2, true); waitFutureSuccess(futureClient); final BGPSessionImpl session = futureClient.get(); diff --git a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/CheckUtil.java b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/CheckUtil.java index 8d16e48403..7a3d5bf4ed 100644 --- a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/CheckUtil.java +++ b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/CheckUtil.java @@ -61,7 +61,7 @@ public final class CheckUtil { throw lastError; } - + @SuppressWarnings("unchecked") public static void waitFutureSuccess(final T future) { final CountDownLatch latch = new CountDownLatch(1); future.addListener(future1 -> latch.countDown()); -- 2.36.6