X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=applications%2Fforwardingrules-sync%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fopenflowplugin%2Fapplications%2Ffrsync%2Fimpl%2FSyncReactorFutureZipDecoratorTest.java;h=875aaf9284432959b37c750a3205bead3af9ddcc;hb=b4f4b4b702e2ccd8a7c62fd2a5c184c5b1cbe665;hp=f9fb4410cee18cb0946881a70f29ab14943f441e;hpb=5225848e57f10d051355f649df8d1ae4ec0c068c;p=openflowplugin.git diff --git a/applications/forwardingrules-sync/src/test/java/org/opendaylight/openflowplugin/applications/frsync/impl/SyncReactorFutureZipDecoratorTest.java b/applications/forwardingrules-sync/src/test/java/org/opendaylight/openflowplugin/applications/frsync/impl/SyncReactorFutureZipDecoratorTest.java index f9fb4410ce..875aaf9284 100644 --- a/applications/forwardingrules-sync/src/test/java/org/opendaylight/openflowplugin/applications/frsync/impl/SyncReactorFutureZipDecoratorTest.java +++ b/applications/forwardingrules-sync/src/test/java/org/opendaylight/openflowplugin/applications/frsync/impl/SyncReactorFutureZipDecoratorTest.java @@ -11,23 +11,26 @@ package org.opendaylight.openflowplugin.applications.frsync.impl; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.ThreadFactoryBuilder; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; import org.mockito.InOrder; -import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; import org.mockito.runners.MockitoJUnitRunner; -import org.mockito.stubbing.Answer; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.openflowplugin.applications.frsync.SyncReactor; +import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes; @@ -48,18 +51,22 @@ public class SyncReactorFutureZipDecoratorTest { private SyncReactorFutureZipDecorator reactor; private InstanceIdentifier fcNodePath; private ListeningExecutorService syncThreadPool; + private final LogicalDatastoreType configDS = LogicalDatastoreType.CONFIGURATION; + private final LogicalDatastoreType operationalDS = LogicalDatastoreType.OPERATIONAL; @Mock - private SyncReactorGuardDecorator delegate; + private SyncReactor delegate; + @Mock + private SyncupEntry syncupEntry; @Before public void setUp() { - syncThreadPool = FrmExecutors.instance() - .newFixedThreadPool(1, new ThreadFactoryBuilder() - .setNameFormat(SyncReactorFutureDecorator.FRM_RPC_CLIENT_PREFIX + "%d") - .setDaemon(false) - .build()); - + final ExecutorService executorService = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder() + .setDaemon(false) + .setNameFormat("frsync-test-%d") + .setUncaughtExceptionHandler((thread, ex) -> LOG.error("Uncaught exception {}", thread, ex)) + .build()); + syncThreadPool = MoreExecutors.listeningDecorator(executorService); reactor = new SyncReactorFutureZipDecorator(delegate, syncThreadPool); fcNodePath = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(NODE_ID)) .augmentation(FlowCapableNode.class); @@ -72,27 +79,32 @@ public class SyncReactorFutureZipDecoratorTest { final FlowCapableNode dataAfter2 = Mockito.mock(FlowCapableNode.class); final CountDownLatch latchForFirst = new CountDownLatch(1); final CountDownLatch latchForNext = new CountDownLatch(1); - final LogicalDatastoreType dsType = LogicalDatastoreType.CONFIGURATION; - - Mockito.when(delegate.syncup(Matchers.>any(), Matchers.any(), - Matchers.any(), Matchers.any())).thenAnswer(new Answer>() { - @Override - public ListenableFuture answer(final InvocationOnMock invocationOnMock) throws Throwable { - LOG.info("unlocking next configs"); - latchForNext.countDown(); - latchForFirst.await(); - LOG.info("unlocking first delegate"); - return Futures.immediateFuture(Boolean.TRUE); - } - }).thenReturn(Futures.immediateFuture(Boolean.TRUE)); + final SyncupEntry first = new SyncupEntry(dataBefore, configDS, null, operationalDS); + final SyncupEntry second = new SyncupEntry(dataAfter, configDS, dataBefore, configDS); + final SyncupEntry third = new SyncupEntry(null, configDS, dataAfter, configDS); + final SyncupEntry fourth = new SyncupEntry(dataAfter2, configDS, null, configDS); + final SyncupEntry zipped = new SyncupEntry(dataAfter2, configDS, dataBefore, configDS); final List> allResults = new ArrayList<>(); - allResults.add(reactor.syncup(fcNodePath, dataBefore, null, dsType)); + + Mockito.when(delegate.syncup(ArgumentMatchers.>any(), Mockito.eq(first))) + .thenAnswer(invocationOnMock -> { + LOG.info("unlocking next configs"); + latchForNext.countDown(); + latchForFirst.await(); + LOG.info("unlocking first delegate"); + return Futures.immediateFuture(Boolean.TRUE); + }); + + allResults.add(reactor.syncup(fcNodePath, first)); latchForNext.await(); - allResults.add(reactor.syncup(fcNodePath, dataAfter, dataBefore, dsType)); - allResults.add(reactor.syncup(fcNodePath, null, dataAfter, dsType)); - allResults.add(reactor.syncup(fcNodePath, dataAfter2, null, dsType)); + mockSyncupWithEntry(second); + allResults.add(reactor.syncup(fcNodePath, second)); + mockSyncupWithEntry(third); + allResults.add(reactor.syncup(fcNodePath, third)); + mockSyncupWithEntry(fourth); + allResults.add(reactor.syncup(fcNodePath, fourth)); latchForFirst.countDown(); Futures.allAsList(allResults).get(1, TimeUnit.SECONDS); @@ -105,8 +117,8 @@ public class SyncReactorFutureZipDecoratorTest { syncThreadPool.shutdownNow(); } final InOrder inOrder = Mockito.inOrder(delegate); - inOrder.verify(delegate).syncup(fcNodePath, dataBefore, null, dsType); - inOrder.verify(delegate).syncup(fcNodePath, dataAfter2, dataBefore, dsType); + inOrder.verify(delegate).syncup(fcNodePath, first); + inOrder.verify(delegate).syncup(fcNodePath, zipped); inOrder.verifyNoMoreInteractions(); } @@ -115,21 +127,21 @@ public class SyncReactorFutureZipDecoratorTest { final FlowCapableNode dataBefore = Mockito.mock(FlowCapableNode.class); final FlowCapableNode dataAfter = Mockito.mock(FlowCapableNode.class); final CountDownLatch latchForNext = new CountDownLatch(1); - final LogicalDatastoreType dsType = LogicalDatastoreType.CONFIGURATION; - - Mockito.when(delegate.syncup(Matchers.>any(), Matchers.any(), - Matchers.any(), Matchers.any())).thenAnswer(new Answer>() { - @Override - public ListenableFuture answer(final InvocationOnMock invocationOnMock) throws Throwable { - LOG.info("unlocking next config"); - latchForNext.countDown(); - return Futures.immediateFuture(Boolean.TRUE); - } - }).thenReturn(Futures.immediateFuture(Boolean.TRUE)); - - reactor.syncup(fcNodePath, dataBefore, null, dsType); + + final SyncupEntry first = new SyncupEntry(dataBefore, configDS, null, configDS); + final SyncupEntry second = new SyncupEntry(dataAfter, configDS, dataBefore, configDS); + + Mockito.when(delegate.syncup(ArgumentMatchers.>any(), Mockito.eq(first))) + .thenAnswer(invocationOnMock -> { + LOG.info("unlocking next config"); + latchForNext.countDown(); + return Futures.immediateFuture(Boolean.TRUE); + }); + + reactor.syncup(fcNodePath, first); latchForNext.await(); - reactor.syncup(fcNodePath, dataAfter, dataBefore, dsType); + mockSyncupWithEntry(second); + reactor.syncup(fcNodePath, second); boolean terminated = syncThreadPool.awaitTermination(1, TimeUnit.SECONDS); if (!terminated) { @@ -137,10 +149,9 @@ public class SyncReactorFutureZipDecoratorTest { syncThreadPool.shutdownNow(); } final InOrder inOrder = Mockito.inOrder(delegate); - inOrder.verify(delegate).syncup(fcNodePath, dataBefore, null, dsType); - inOrder.verify(delegate).syncup(fcNodePath, dataAfter, dataBefore, dsType); + inOrder.verify(delegate).syncup(fcNodePath, first); + inOrder.verify(delegate).syncup(fcNodePath, second); inOrder.verifyNoMoreInteractions(); - } @Test @@ -152,22 +163,23 @@ public class SyncReactorFutureZipDecoratorTest { final CountDownLatch latchForFirst = new CountDownLatch(1); final CountDownLatch latchForNext = new CountDownLatch(1); - Mockito.when(delegate.syncup(Matchers.>any(), Matchers.any(), - Matchers.any(), Matchers.any())).thenAnswer(new Answer>() { - @Override - public ListenableFuture answer(final InvocationOnMock invocationOnMock) throws Throwable { - LOG.info("unlocking for fresh operational"); - latchForNext.countDown(); - latchForFirst.await(); - LOG.info("unlocking first delegate"); - return Futures.immediateFuture(Boolean.TRUE); - } - }).thenReturn(Futures.immediateFuture(Boolean.TRUE)); - - reactor.syncup(fcNodePath, configAfter, configBefore, LogicalDatastoreType.CONFIGURATION); + final SyncupEntry first = new SyncupEntry(configAfter, configDS, configBefore, configDS); + final SyncupEntry second = new SyncupEntry(configActual, configDS, freshOperational, operationalDS); + + Mockito.when(delegate.syncup(ArgumentMatchers.>any(), Mockito.eq(first))) + .thenAnswer(invocationOnMock -> { + LOG.info("unlocking for fresh operational"); + latchForNext.countDown(); + latchForFirst.await(); + LOG.info("unlocking first delegate"); + return Futures.immediateFuture(Boolean.TRUE); + }); + + reactor.syncup(fcNodePath, first); latchForNext.await(); - reactor.syncup(fcNodePath, configActual, freshOperational, LogicalDatastoreType.OPERATIONAL); + mockSyncupWithEntry(second); + reactor.syncup(fcNodePath, second); latchForFirst.countDown(); syncThreadPool.shutdown(); @@ -176,11 +188,16 @@ public class SyncReactorFutureZipDecoratorTest { LOG.info("thread pool not terminated."); syncThreadPool.shutdownNow(); } - Mockito.verify(delegate, Mockito.times(1)).syncup(fcNodePath, configActual, freshOperational, LogicalDatastoreType.OPERATIONAL); + Mockito.verify(delegate, Mockito.times(1)).syncup(fcNodePath, second); + } + + private void mockSyncupWithEntry(final SyncupEntry entry) { + Mockito.when(delegate.syncup(ArgumentMatchers.any(), Mockito.eq(entry))) + .thenReturn(Futures.immediateFuture(Boolean.TRUE)); } @After public void tearDown() { syncThreadPool.shutdownNow(); } -} \ No newline at end of file +}