b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000);
+ b.option(ChannelOption.SO_REUSEADDR, true);
b.group(workergroup);
b.handler(new ChannelInitializer<NioSocketChannel>() {
package org.opendaylight.protocol.bmp.mock;
+import static org.opendaylight.protocol.bmp.mock.BmpMockTest.waitFutureSuccess;
+
import com.google.common.base.Optional;
import com.google.common.net.InetAddresses;
import io.netty.channel.Channel;
final ChannelFuture channelFuture = dispatcher.createClient(new InetSocketAddress(InetAddresses.forString("127.0.0.2"), 0),
new InetSocketAddress(InetAddresses.forString("127.0.0.3"), port));
+ waitFutureSuccess(channelFuture);
final Channel channel = channelFuture.sync().channel();
Assert.assertTrue(channel.isActive());
package org.opendaylight.protocol.bmp.mock;
import com.google.common.base.Optional;
+import com.google.common.util.concurrent.Uninterruptibles;
import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.util.concurrent.Future;
+import io.netty.util.concurrent.GenericFutureListener;
import java.net.InetSocketAddress;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
private BmpDispatcher bmpDispatcher;
@Before
- public void setUp() {
+ public void setUp() throws InterruptedException {
final BmpExtensionProviderContext ctx = new SimpleBmpExtensionProviderContext();
this.bmpActivator = new BmpActivator(
- ServiceLoaderBGPExtensionProviderContext.getSingletonInstance());
+ ServiceLoaderBGPExtensionProviderContext.getSingletonInstance());
this.bmpActivator.start(ctx);
this.bmpDispatcher = new BmpDispatcherImpl(new NioEventLoopGroup(), new NioEventLoopGroup(), ctx.getBmpMessageRegistry(),
- new DefaultBmpSessionFactory());
+ new DefaultBmpSessionFactory());
final BmpSessionListenerFactory bmpSessionListenerFactory = new BmpSessionListenerFactory() {
@Override
public BmpSessionListener getSessionListener() {
}
};
this.serverPort = BmpMockDispatcherTest.getRandomPort();
- this.serverChannel = this.bmpDispatcher.createServer(new InetSocketAddress("127.0.0.1", this.serverPort),
- bmpSessionListenerFactory, Optional.<KeyMapping>absent()).channel();
+ final ChannelFuture futureServer = this.bmpDispatcher.createServer(new InetSocketAddress("127.0.0.1", this.serverPort),
+ bmpSessionListenerFactory, Optional.<KeyMapping>absent());
+ waitFutureSuccess(futureServer);
+ this.serverChannel = futureServer.channel();
}
@After
@Test
public void testMain() throws Exception {
- BmpMock.main(new String[] {"--remote_address", "127.0.0.1:" + serverPort, "--peers_count", "3", "--pre_policy_routes", "3"});
+ BmpMock.main(new String[]{"--remote_address", "127.0.0.1:" + serverPort, "--peers_count", "3", "--pre_policy_routes", "3"});
Thread.sleep(1000);
Mockito.verify(this.sessionListener).onSessionUp(Mockito.any(BmpSession.class));
//1 * Initiate message + 3 * PeerUp Notification + 9 * Route Monitoring message
Mockito.verify(this.sessionListener, Mockito.times(13)).onMessage(Mockito.any(BmpSession.class), Mockito.any(Notification.class));
}
+ static void waitFutureSuccess(final ChannelFuture future) throws InterruptedException {
+ final CountDownLatch latch = new CountDownLatch(1);
+ future.addListener(new GenericFutureListener<Future<? super Void>>() {
+ @Override
+ public void operationComplete(final Future<? super Void> future) throws Exception {
+ latch.countDown();
+ }
+ });
+ Uninterruptibles.awaitUninterruptibly(latch, 10, TimeUnit.SECONDS);
+ }
}
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
+import com.google.common.util.concurrent.Uninterruptibles;
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.FutureListener;
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.concurrent.GlobalEventExecutor;
import java.net.InetSocketAddress;
import java.util.List;
+import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
private Channel channel;
@Before
- public void setUp() throws BGPDocumentedException {
+ public void setUp() throws BGPDocumentedException, InterruptedException {
final EventLoopGroup group = new NioEventLoopGroup();
this.registry = new StrictBGPPeerRegistry();
this.registry.addPeer(new IpAddress(new Ipv4Address(CLIENT_ADDRESS.getAddress().getHostAddress())),
CLIENT_ADDRESS);
final ChannelFuture future = this.dispatcher.createServer(this.registry, ADDRESS);
+ waitFutureSuccess(future);
future.addListener(new GenericFutureListener<Future<Void>>() {
@Override
public void operationComplete(final Future<Void> future) {
this.channel = future.channel();
}
+ private static <T extends Future> void waitFutureSuccess(final T future) throws InterruptedException {
+ final CountDownLatch latch = new CountDownLatch(1);
+ future.addListener(new FutureListener<T>() {
+ @Override
+ public void operationComplete(final Future<T> future) throws Exception {
+ latch.countDown();
+ }
+ });
+ Uninterruptibles.awaitUninterruptibly(latch, 10, TimeUnit.SECONDS);
+ }
+
@Test
public void testCreateClient() throws InterruptedException, ExecutionException {
- final BGPSessionImpl session = this.clientDispatcher.createClient(ADDRESS, this.registry,
- new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, TIMEOUT), Optional.<InetSocketAddress>absent()).get();
+ final Future<BGPSessionImpl> futureClient = this.clientDispatcher.createClient(ADDRESS, this.registry,
+ new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, TIMEOUT), Optional.<InetSocketAddress>absent());
+ waitFutureSuccess(futureClient);
+ final BGPSessionImpl session = futureClient.get();
Assert.assertEquals(BGPSessionImpl.State.UP, session.getState());
Assert.assertEquals(AS_NUMBER, session.getAsNumber());
Assert.assertEquals(Sets.newHashSet(this.ipv4tt), session.getAdvertisedTableTypes());
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,
new ReconnectStrategyFctImpl(), Optional.<InetSocketAddress>absent());
- final Channel channel2 = this.dispatcher.createServer(this.registry, CLIENT_ADDRESS2).channel();
- Thread.sleep(1000);
- Assert.assertTrue(listener.up);
+ final ChannelFuture future = this.dispatcher.createServer(this.registry, CLIENT_ADDRESS2);
+ waitFutureSuccess(future);
+ final Channel channel2 = future.channel();
+ Assert.assertTrue(listener.isUp());
Assert.assertTrue(channel2.isActive());
cf.cancel(true);
listener.releaseConnection();
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
+import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.Uninterruptibles;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
@Test
public void testHoldTimerExpire() throws InterruptedException {
this.bgpSession.sessionUp();
- Thread.sleep(3500);
- Assert.assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
+ checkIdleState(this.bgpSession);
Assert.assertEquals(3, this.receivedMsgs.size());
Assert.assertTrue(this.receivedMsgs.get(2) instanceof Notify);
final Notify error = (Notify) this.receivedMsgs.get(2);
Assert.assertEquals(BGPError.HOLD_TIMER_EXPIRED.getSubcode(), error.getErrorSubcode().shortValue());
Mockito.verify(this.speakerListener).close();
}
+
+ static void checkIdleState (final BGPSessionImpl session){
+ final Stopwatch sw = Stopwatch.createStarted();
+ while(sw.elapsed(TimeUnit.SECONDS) <= 10) {
+ if (BGPSessionImpl.State.IDLE != session.getState()){
+ Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
+ } else {
+ return;
+ }
+ }
+ Assert.fail();
+ }
}
assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
this.clientSession.handleMessage(new KeepaliveBuilder().build());
assertEquals(this.clientSession.getState(), BGPClientSessionNegotiator.State.FINISHED);
- Thread.sleep(1000);
- Thread.sleep(100);
}
@Test
*/
package org.opendaylight.protocol.bgp.rib.impl;
+import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.Uninterruptibles;
import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import org.junit.Assert;
import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
public List<Notification> getListMsg() {
return this.listMsg;
}
+ private final CountDownLatch sessionLatch = new CountDownLatch(1);
@Override
public void onMessage(final BGPSession session, final Notification message) {
LOG.debug("Session Up");
this.session = session;
this.up = true;
+ sessionLatch.countDown();
}
@Override
public void markUptodate(final TablesKey tablesKey) {
LOG.debug("Table marked as up-to-date {}", tablesKey);
}
+
+ public boolean isUp() {
+ Preconditions.checkNotNull(getSession());
+ return up;
+ }
+ public BGPSessionImpl getSession() {
+ Assert.assertEquals("Session up", true, Uninterruptibles.awaitUninterruptibly(sessionLatch, 10, TimeUnit.SECONDS));
+ return (BGPSessionImpl) this.session;
+ }
}
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.Uninterruptibles;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.util.concurrent.DefaultPromise;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.Future;
+import io.netty.util.concurrent.FutureListener;
import io.netty.util.concurrent.GlobalEventExecutor;
import io.netty.util.concurrent.Promise;
import java.net.InetSocketAddress;
import java.nio.channels.Channel;
import java.util.ArrayList;
import java.util.List;
+import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
return new SimpleSessionListener();
}
}, null);
- final PCEPSessionImpl session1 = (PCEPSessionImpl) this.pccMock.createClient(CLIENT1_ADDRESS,
- new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
- new PCEPSessionListenerFactory() {
- @Override
- public PCEPSessionListener getSessionListener() {
- return new SimpleSessionListener();
- }
- }).get();
+ waitFutureSuccess(futureChannel);
+ final Future<PCEPSession> client1 = this.pccMock.createClient(CLIENT1_ADDRESS,
+ new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
+ new PCEPSessionListenerFactory() {
+ @Override
+ public PCEPSessionListener getSessionListener() {
+ return new SimpleSessionListener();
+ }
+ });
+ waitFutureSuccess(client1);
+ final PCEPSessionImpl session1 = (PCEPSessionImpl) client1.get();
- final PCEPSessionImpl session2 = (PCEPSessionImpl) this.pccMock.createClient(CLIENT2_ADDRESS,
- new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
- new PCEPSessionListenerFactory() {
- @Override
- public PCEPSessionListener getSessionListener() {
- return new SimpleSessionListener();
- }
- }).get();
+ final Future<PCEPSession> client2 = this.pccMock.createClient(CLIENT2_ADDRESS,
+ new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
+ new PCEPSessionListenerFactory() {
+ @Override
+ public PCEPSessionListener getSessionListener() {
+ return new SimpleSessionListener();
+ }
+ });
+ waitFutureSuccess(client2);
+ final PCEPSessionImpl session2 = (PCEPSessionImpl) client2.get();
Assert.assertTrue(futureChannel.channel().isActive());
Assert.assertEquals(CLIENT1_ADDRESS.getAddress().getHostAddress(), session1.getPeerPref().getIpAddress());
Assert.assertTrue(futureChannel.channel().isActive());
}
+ static <T extends Future> void waitFutureSuccess(final T future) throws InterruptedException {
+ final CountDownLatch latch = new CountDownLatch(1);
+ future.addListener(new FutureListener() {
+ @Override
+ public void operationComplete(final Future future) throws Exception {
+ latch.countDown();
+ }
+ });
+ Uninterruptibles.awaitUninterruptibly(latch, 10, TimeUnit.SECONDS);
+ }
+
@Test
public void testCreateDuplicateClient() throws InterruptedException, ExecutionException {
- this.dispatcher.createServer(new InetSocketAddress("0.0.0.0", PORT),
- new PCEPSessionListenerFactory() {
- @Override
- public PCEPSessionListener getSessionListener() {
- return new SimpleSessionListener();
- }
- }, null);
- final PCEPSessionImpl session1 = (PCEPSessionImpl) this.pccMock.createClient(CLIENT1_ADDRESS,
+ final ChannelFuture futureServer = this.dispatcher.createServer(new InetSocketAddress("0.0.0.0", PORT),
+ new PCEPSessionListenerFactory() {
+ @Override
+ public PCEPSessionListener getSessionListener() {
+ return new SimpleSessionListener();
+ }
+ }, null);
+ waitFutureSuccess(futureServer);
+ final Future<PCEPSession> futureClient = this.pccMock.createClient(CLIENT1_ADDRESS,
new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
new PCEPSessionListenerFactory() {
@Override
public PCEPSessionListener getSessionListener() {
return new SimpleSessionListener();
}
- }).get();
+ });
+ waitFutureSuccess(futureClient);
+ final PCEPSessionImpl session1 = (PCEPSessionImpl) futureClient.get();
try {
this.pccMock.createClient(CLIENT1_ADDRESS,
@Test
public void testReconectClient() throws InterruptedException, ExecutionException {
- this.dispatcher.createServer(new InetSocketAddress("0.0.0.0", PORT),
- new PCEPSessionListenerFactory() {
- @Override
- public PCEPSessionListener getSessionListener() {
- return new SimpleSessionListener();
- }
- }, null);
- final PCEPSessionImpl session1 = (PCEPSessionImpl) this.pccMock.createClient(CLIENT1_ADDRESS,
- new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
- new PCEPSessionListenerFactory() {
- @Override
- public PCEPSessionListener getSessionListener() {
- return new SimpleSessionListener();
- }
- }).get();
+ final ChannelFuture futureServer = this.dispatcher.createServer(new InetSocketAddress("0.0.0.0", PORT),
+ new PCEPSessionListenerFactory() {
+ @Override
+ public PCEPSessionListener getSessionListener() {
+ return new SimpleSessionListener();
+ }
+ }, null);
+ waitFutureSuccess(futureServer);
+
+ final Future<PCEPSession> client = this.pccMock.createClient(CLIENT1_ADDRESS,
+ new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 500),
+ new PCEPSessionListenerFactory() {
+ @Override
+ public PCEPSessionListener getSessionListener() {
+ return new SimpleSessionListener();
+ }
+ });
+ waitFutureSuccess(client);
+ final PCEPSessionImpl session1 = (PCEPSessionImpl) client.get();
Assert.assertEquals(CLIENT1_ADDRESS.getAddress(), session1.getRemoteAddress());
Assert.assertEquals(DEAD_TIMER, session1.getDeadTimerValue().shortValue());
}
@Test
- public void testCustomizeBootstrap() {
+ public void testCustomizeBootstrap() throws InterruptedException {
final KeyMapping keys = new KeyMapping();
keys.put(CLIENT1_ADDRESS.getAddress(), new String("CLIENT1_ADDRESS").getBytes() );
keys.put(CLIENT2_ADDRESS.getAddress(), new String("CLIENT2_ADDRESS").getBytes() );
return new SimpleSessionListener();
}
}, null);
+ waitFutureSuccess(futureChannel);
Mockito.verify(this.disp2Spy).createServerBootstrap(Mockito.any(PCEPDispatcherImpl.ChannelPipelineInitializer.class));
}
setChannelFactory(b, keys);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
+ b.option(ChannelOption.SO_REUSEADDR, true);
final ReconnectStrategyFactory reconnectStrategy = reconnectTime == -1 ? getNeverReconnectStrategyFactory() : getTimedReconnectStrategyFactory(reconnectTime);
final PCCReconnectPromise promise = new PCCReconnectPromise(remoteAddress, reconnectStrategy, b);
final ChannelInitializer<SocketChannel> channelInitializer = new ChannelInitializer<SocketChannel>() {
package org.opendaylight.protocol.pcep.pcc.mock;
+import static org.opendaylight.protocol.pcep.pcc.mock.PCCMockCommon.checkSessionListenerNotNull;
+import static org.opendaylight.protocol.pcep.pcc.mock.WaitForFutureSucces.waitFutureSuccess;
+
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;
private static final List<PCEPCapability> CAPS = new ArrayList<>();
private static final PCEPSessionProposalFactory PROPOSAL = new BasePCEPSessionProposalFactory(30, 120, CAPS);
-
- private PCCDispatcherImpl dispatcher;
private final DefaultPCEPSessionNegotiatorFactory nf = new DefaultPCEPSessionNegotiatorFactory(PROPOSAL, 0);
+ private final Random random = new Random();
+ private PCCDispatcherImpl dispatcher;
private PCEPDispatcher pcepDispatcher;
private InetSocketAddress serverAddress;
private InetSocketAddress clientAddress;
- private final Random random = new Random();
private EventLoopGroup workerGroup;
private EventLoopGroup bossGroup;
this.bossGroup = new NioEventLoopGroup();
this.dispatcher = new PCCDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry());
this.pcepDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
- this.nf, this.bossGroup, this.workerGroup);
+ this.nf, this.bossGroup, this.workerGroup);
this.serverAddress = new InetSocketAddress("127.0.5.0", getRandomPort());
this.clientAddress = new InetSocketAddress("127.0.4.0", getRandomPort());
}
@Test
public void testClientReconnect() throws Exception {
final Future<PCEPSession> futureSession = this.dispatcher.createClient(this.serverAddress, 500, new TestingSessionListenerFactory(),
- this.nf, null, this.clientAddress);
+ this.nf, null, this.clientAddress);
final TestingSessionListenerFactory slf = new TestingSessionListenerFactory();
- final Channel channel = this.pcepDispatcher.createServer(this.serverAddress, slf, null).channel();
+ final ChannelFuture futureServer = this.pcepDispatcher.createServer(this.serverAddress, slf, null);
+ waitFutureSuccess(futureServer);
+ final Channel channel = futureServer.channel();
Assert.assertNotNull(futureSession.get());
+ checkSessionListenerNotNull(slf, "127.0.4.0");
final TestingSessionListener sl = slf.getSessionListenerByRemoteAddress(this.clientAddress.getAddress());
Assert.assertNotNull(sl);
+ Assert.assertNotNull(sl.getSession());
Assert.assertTrue(sl.isUp());
channel.close().get();
this.workerGroup = new NioEventLoopGroup();
this.bossGroup = new NioEventLoopGroup();
this.pcepDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
- this.nf, this.bossGroup, this.workerGroup);
+ this.nf, this.bossGroup, this.workerGroup);
final TestingSessionListenerFactory slf2 = new TestingSessionListenerFactory();
- this.pcepDispatcher.createServer(this.serverAddress, slf2, null).channel();
+ final ChannelFuture future2 = this.pcepDispatcher.createServer(this.serverAddress, slf2, null);
+ waitFutureSuccess(future2);
+ future2.channel();
Thread.sleep(500);
final TestingSessionListener sl2 = slf2.getSessionListenerByRemoteAddress(this.clientAddress.getAddress());
Assert.assertNotNull(sl2);
+ Assert.assertNotNull(sl2.getSession());
Assert.assertTrue(sl2.isUp());
}
public class PCCIncrementalSyncTest extends PCCMockCommon {
- private BigInteger lsp = BigInteger.valueOf(8);
+ private final BigInteger lsp = BigInteger.valueOf(8);
/**
* Test Incremental Synchronization
* Create 8 lsp, then it disconnects after 5 sec and then after 5 sec reconnects with Pcc DBVersion 10
checkSynchronizedSession(8, pceSessionListener, numberOflspAndDBv);
Thread.sleep(4000);
assertFalse(pceSessionListener.isUp());
- Thread.sleep(6000);
- Thread.sleep(1000);
final int expetedNumberOfLspAndEndOfSync = 3;
final BigInteger expectedFinalDBVersion = BigInteger.valueOf(10);
final TestingSessionListener sessionListenerAfterReconnect = getListener(factory);
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
+import static org.opendaylight.protocol.pcep.pcc.mock.WaitForFutureSucces.waitFutureSuccess;
import com.google.common.base.Optional;
+import com.google.common.base.Stopwatch;
import com.google.common.net.InetAddresses;
+import com.google.common.util.concurrent.Uninterruptibles;
import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.HashedWheelTimer;
import io.netty.util.concurrent.Future;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import org.junit.Assert;
import org.junit.Before;
import org.opendaylight.protocol.pcep.PCEPCapability;
import org.opendaylight.protocol.pcep.PCEPDispatcher;
nf, new NioEventLoopGroup(), new NioEventLoopGroup());
}
+ private static void checkNumberOfMessages(final int expectedNMessages, final TestingSessionListener listener) throws Exception {
+ Stopwatch sw = Stopwatch.createStarted();
+ while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
+ if (expectedNMessages != listener.messages().size()) {
+ Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
+ } else {
+ return;
+ }
+ }
+ Assert.assertEquals(expectedNMessages, listener.messages().size());
+ }
+
+ static TestingSessionListener checkSessionListenerNotNull(final TestingSessionListenerFactory factory, final String localAddress) {
+ Stopwatch sw = Stopwatch.createStarted();
+ TestingSessionListener listener = null;
+ while (sw.elapsed(TimeUnit.SECONDS) <= 20) {
+ listener = factory.getSessionListenerByRemoteAddress(InetAddresses.forString(localAddress));
+ if (listener == null) {
+ Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
+ } else {
+ return listener;
+ }
+ }
+ throw new NullPointerException();
+ }
+
protected static TestingSessionListener checkSessionListener(final int numMessages, final Channel channel, final TestingSessionListenerFactory factory, final String localAddress) throws
ExecutionException, InterruptedException {
final TestingSessionListener sessionListener = factory.getSessionListenerByRemoteAddress(InetAddresses.forString(localAddress));
assertNotNull(sessionListener);
+ assertNotNull(sessionListener.getSession());
assertTrue(sessionListener.isUp());
assertEquals(numMessages, sessionListener.messages().size());
channel.close().get();
return sessionListener;
}
- protected Channel createServer(final TestingSessionListenerFactory factory, final InetSocketAddress serverAddress2) {
+ protected Channel createServer(final TestingSessionListenerFactory factory, final InetSocketAddress serverAddress2) throws InterruptedException {
return createServer(factory, serverAddress2, null);
}
protected Channel createServer(final TestingSessionListenerFactory factory, final InetSocketAddress
- serverAddress2, final PCEPPeerProposal peerProposal) {
+ serverAddress2, final PCEPPeerProposal peerProposal) throws InterruptedException {
final PCEPExtensionProviderContext ctx = ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance();
final StatefulActivator activator07 = new StatefulActivator();
final SyncOptimizationsActivator optimizationsActivator = new SyncOptimizationsActivator();
activator07.start(ctx);
optimizationsActivator.start(ctx);
- return this.pceDispatcher.createServer(serverAddress2, factory, peerProposal).channel();
+ final ChannelFuture futureServer = this.pceDispatcher.createServer(serverAddress2, factory, peerProposal);
+ waitFutureSuccess(futureServer);
+ return futureServer.channel();
}
protected static void checkSynchronizedSession(final int numberOfLsp, final TestingSessionListener pceSessionListener, final BigInteger expectedeInitialDb) throws InterruptedException {
+ assertNotNull(pceSessionListener.getSession());
assertTrue(pceSessionListener.isUp());
Thread.sleep(1000);
//Send Open with LspDBV = 1
}
protected static void checkResyncSession(final Optional<Integer> startAtNumberLsp, final int expectedNumberOfLsp, final BigInteger startingDBVersion,
- final BigInteger expectedDBVersion, final TestingSessionListener pceSessionListener) {
+ final BigInteger expectedDBVersion, final TestingSessionListener pceSessionListener) throws InterruptedException {
+ assertNotNull(pceSessionListener.getSession());
assertTrue(pceSessionListener.isUp());
+ Thread.sleep(50);
List<Message> messages;
if(startAtNumberLsp.isPresent()) {
messages = pceSessionListener.messages().subList(startAtNumberLsp.get(), startAtNumberLsp.get() + expectedNumberOfLsp);
}
protected TestingSessionListener getListener(final TestingSessionListenerFactory factory) {
- return factory.getSessionListenerByRemoteAddress(InetAddresses.forString(PCCMockTest.LOCAL_ADDRESS));
+ return checkSessionListenerNotNull(factory, PCCMockTest.LOCAL_ADDRESS);
}
}
final TestingSessionListenerFactory factory = new TestingSessionListenerFactory();
final Channel channel = createServer(factory, socket, new PCCPeerProposal());
- Thread.sleep(200);
PCEPSession session = createPCCSession(BigInteger.TEN).get();
assertNotNull(session);
final TestingSessionListener pceSessionListener = getListener(factory);
assertNotNull(pceSessionListener);
- Thread.sleep(1000);
+ assertNotNull(pceSessionListener.getSession());
checkResyncSession(Optional.<Integer>absent(), 11, null, BigInteger.valueOf(10), pceSessionListener);
channel.close().get();
}
final int lspQuantity = 3;
final BigInteger numberOflspAndDBv = BigInteger.valueOf(lspQuantity);
this.channel = createServer(factory, socket, new PCCPeerProposal());
- Thread.sleep(300);
PCEPSession session = createPCCSession(numberOflspAndDBv).get();
assertNotNull(session);
final TestingSessionListener pceSessionListener = getListener(factory);
assertNotNull(pceSessionListener);
checkSynchronizedSession(lspQuantity, pceSessionListener, numberOflspAndDBv);
pccSessionListener.onMessage(session, createTriggerLspResync());
- Thread.sleep(300);
final TestingSessionListener sessionListenerAfterReconnect = getListener(factory);
checkResyncSession(Optional.of(lspQuantity), 4, null, numberOflspAndDBv, sessionListenerAfterReconnect);
channel.close().get();
final BigInteger numberOflspAndDBv = BigInteger.valueOf(lspQuantity);
this.channel = createServer(factory, socket, new PCCPeerProposal());
- Thread.sleep(200);
PCEPSession session = createPCCSession(numberOflspAndDBv).get();
assertNotNull(session);
final TestingSessionListener pceSessionListener = getListener(factory);
assertNotNull(pceSessionListener);
checkSynchronizedSession(lspQuantity, pceSessionListener, numberOflspAndDBv);
pccSessionListener.onMessage(session, createTriggerLspResync());
- Thread.sleep(300);
final TestingSessionListener sessionListenerAfterReconnect = getListener(factory);
checkResyncSession(Optional.of(lspQuantity), 2, null, numberOflspAndDBv, sessionListenerAfterReconnect);
channel.close().get();
public void testSessionTriggeredSync() throws Exception {
final TestingSessionListenerFactory factory = new TestingSessionListenerFactory();
this.channel = createServer(factory, socket, new PCCPeerProposal());
- Thread.sleep(200);
final BigInteger numberOflspAndDBv = BigInteger.valueOf(3);
PCEPSession session = createPCCSession(numberOflspAndDBv).get();
assertNotNull(session);
assertNotNull(pceSessionListener);
checkSynchronizedSession(0, pceSessionListener, BigInteger.ZERO);
pccSessionListener.onMessage(session, createTriggerMsg());
- Thread.sleep(300);
checkSynchronizedSession(3, pceSessionListener, numberOflspAndDBv);
this.channel.close().get();
}
package org.opendaylight.protocol.pcep.pcc.mock;
import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.Uninterruptibles;
import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import org.junit.Assert;
import org.opendaylight.protocol.pcep.PCEPSession;
import org.opendaylight.protocol.pcep.PCEPSessionListener;
import org.opendaylight.protocol.pcep.PCEPTerminationReason;
private static final Logger LOG = LoggerFactory.getLogger(TestingSessionListener.class);
private final List<Message> messages = Lists.newArrayList();
-
+ private final CountDownLatch sessionLatch = new CountDownLatch(1);
private boolean up = false;
private PCEPSession session = null;
- @Override
- public void onMessage(final PCEPSession session, final Message message) {
- LOG.debug("Received message: {}", message);
- this.messages.add(message);
- }
-
@Override
public void onSessionUp(final PCEPSession session) {
LOG.debug("Session up.");
this.up = true;
this.session = session;
+ sessionLatch.countDown();
}
@Override
LOG.debug("Session terminated. Cause : {}", cause);
}
+ @Override
+ public void onMessage(final PCEPSession session, final Message message) {
+ LOG.debug("Received message: {}", message);
+ this.messages.add(message);
+ }
+
public List<Message> messages() {
return this.messages;
}
- public boolean isUp () {
+ public boolean isUp() {
return this.up;
}
public PCEPSession getSession() {
+ Assert.assertEquals("Session up", true, Uninterruptibles.awaitUninterruptibly(sessionLatch, 10, TimeUnit.SECONDS));
return this.session;
}
}
--- /dev/null
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.protocol.pcep.pcc.mock;
+
+import com.google.common.util.concurrent.Uninterruptibles;
+import io.netty.util.concurrent.Future;
+import io.netty.util.concurrent.FutureListener;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+final class WaitForFutureSucces {
+ private WaitForFutureSucces() {
+ throw new UnsupportedOperationException();
+ }
+
+ static <T extends Future> void waitFutureSuccess(final T future) throws InterruptedException {
+ final CountDownLatch latch = new CountDownLatch(1);
+ future.addListener(new FutureListener() {
+ @Override
+ public void operationComplete(final Future future) throws Exception {
+ latch.countDown();
+ }
+ });
+ Uninterruptibles.awaitUninterruptibly(latch, 10, TimeUnit.SECONDS);
+ }
+}