X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FDataTreeChangeListenerProxyTest.java;h=3f5f222febc8b096f4f90b410fd51cf6fca54537;hp=0dc0706a24f588f1c753a1d1b0ae7d55d22cb667;hb=2fd1fa721510a30f58b3bc277deb05fce58badd6;hpb=9a6190b662c5f8de44df872b0ba7e8dd17f8da13 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DataTreeChangeListenerProxyTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DataTreeChangeListenerProxyTest.java index 0dc0706a24..3f5f222feb 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DataTreeChangeListenerProxyTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DataTreeChangeListenerProxyTest.java @@ -7,234 +7,239 @@ */ package org.opendaylight.controller.cluster.datastore; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; + import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.Props; import akka.actor.Terminated; import akka.dispatch.ExecutionContexts; import akka.dispatch.Futures; -import akka.testkit.JavaTestKit; +import akka.testkit.javadsl.TestKit; import akka.util.Timeout; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.Uninterruptibles; +import java.time.Duration; import java.util.concurrent.TimeUnit; -import org.junit.Assert; import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import org.opendaylight.controller.cluster.common.actor.Dispatchers; +import org.opendaylight.controller.cluster.datastore.config.Configuration; import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException; -import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeChangeListenerRegistration; +import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistration; import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard; import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound; import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound; import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener; -import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply; +import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply; import org.opendaylight.controller.cluster.datastore.utils.ActorContext; -import org.opendaylight.controller.cluster.datastore.utils.Dispatchers; -import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor; +import org.opendaylight.controller.cluster.raft.utils.DoNothingActor; import org.opendaylight.controller.md.cluster.datastore.model.TestModel; -import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener; +import org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener; +import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import scala.concurrent.ExecutionContextExecutor; import scala.concurrent.Future; -import scala.concurrent.duration.FiniteDuration; public class DataTreeChangeListenerProxyTest extends AbstractActorTest { - @SuppressWarnings("unchecked") private final DOMDataTreeChangeListener mockListener = mock(DOMDataTreeChangeListener.class); - @Test(timeout=10000) + @Test(timeout = 10000) public void testSuccessfulRegistration() { - new JavaTestKit(getSystem()) {{ - ActorContext actorContext = new ActorContext(getSystem(), getRef(), - mock(ClusterWrapper.class), mock(Configuration.class)); + final TestKit kit = new TestKit(getSystem()); + ActorContext actorContext = new ActorContext(getSystem(), kit.getRef(), mock(ClusterWrapper.class), + mock(Configuration.class)); - final DataTreeChangeListenerProxy proxy = - new DataTreeChangeListenerProxy<>(actorContext, mockListener); + final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME); + final DataTreeChangeListenerProxy proxy = new DataTreeChangeListenerProxy<>( + actorContext, mockListener, path); - final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME); - new Thread() { - @Override - public void run() { - proxy.init("shard-1", path); - } + new Thread(() -> proxy.init("shard-1")).start(); - }.start(); + Duration timeout = Duration.ofSeconds(5); + FindLocalShard findLocalShard = kit.expectMsgClass(timeout, FindLocalShard.class); + assertEquals("getShardName", "shard-1", findLocalShard.getShardName()); - FiniteDuration timeout = duration("5 seconds"); - FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class); - Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName()); + kit.reply(new LocalShardFound(kit.getRef())); - reply(new LocalShardFound(getRef())); + RegisterDataTreeChangeListener registerMsg = kit.expectMsgClass(timeout, + RegisterDataTreeChangeListener.class); + assertEquals("getPath", path, registerMsg.getPath()); + assertFalse("isRegisterOnAllInstances", registerMsg.isRegisterOnAllInstances()); - RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout, RegisterDataTreeChangeListener.class); - Assert.assertEquals("getPath", path, registerMsg.getPath()); + kit.reply(new RegisterDataTreeNotificationListenerReply(kit.getRef())); - reply(new RegisterDataTreeChangeListenerReply(getRef())); + for (int i = 0; i < 20 * 5 && proxy.getListenerRegistrationActor() == null; i++) { + Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS); + } + assertEquals("getListenerRegistrationActor", getSystem().actorSelection(kit.getRef().path()), + proxy.getListenerRegistrationActor()); - for(int i = 0; (i < 20 * 5) && proxy.getListenerRegistrationActor() == null; i++) { - Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS); - } + kit.watch(proxy.getDataChangeListenerActor()); - Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()), - proxy.getListenerRegistrationActor()); + proxy.close(); - watch(proxy.getDataChangeListenerActor()); + // The listener registration actor should get a Close message + kit.expectMsgClass(timeout, CloseDataTreeNotificationListenerRegistration.class); - proxy.close(); + // The DataChangeListener actor should be terminated + kit.expectMsgClass(timeout, Terminated.class); - // The listener registration actor should get a Close message - expectMsgClass(timeout, CloseDataTreeChangeListenerRegistration.class); + proxy.close(); - // The DataChangeListener actor should be terminated - expectMsgClass(timeout, Terminated.class); + kit.expectNoMessage(); + } - proxy.close(); + @Test(timeout = 10000) + public void testSuccessfulRegistrationForClusteredListener() { + final TestKit kit = new TestKit(getSystem()); + ActorContext actorContext = new ActorContext(getSystem(), kit.getRef(), mock(ClusterWrapper.class), + mock(Configuration.class)); + + ClusteredDOMDataTreeChangeListener mockClusteredListener = mock( + ClusteredDOMDataTreeChangeListener.class); + + final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME); + final DataTreeChangeListenerProxy proxy = + new DataTreeChangeListenerProxy<>(actorContext, mockClusteredListener, path); + + new Thread(() -> proxy.init("shard-1")).start(); + + Duration timeout = Duration.ofSeconds(5); + FindLocalShard findLocalShard = kit.expectMsgClass(timeout, FindLocalShard.class); + assertEquals("getShardName", "shard-1", findLocalShard.getShardName()); + + kit.reply(new LocalShardFound(kit.getRef())); - expectNoMsg(); - }}; + RegisterDataTreeChangeListener registerMsg = kit.expectMsgClass(timeout, + RegisterDataTreeChangeListener.class); + assertEquals("getPath", path, registerMsg.getPath()); + assertTrue("isRegisterOnAllInstances", registerMsg.isRegisterOnAllInstances()); + + proxy.close(); } - @Test(timeout=10000) + @Test(timeout = 10000) public void testLocalShardNotFound() { - new JavaTestKit(getSystem()) {{ - ActorContext actorContext = new ActorContext(getSystem(), getRef(), - mock(ClusterWrapper.class), mock(Configuration.class)); + final TestKit kit = new TestKit(getSystem()); + ActorContext actorContext = new ActorContext(getSystem(), kit.getRef(), mock(ClusterWrapper.class), + mock(Configuration.class)); - final DataTreeChangeListenerProxy proxy = - new DataTreeChangeListenerProxy<>(actorContext, mockListener); + final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME); + final DataTreeChangeListenerProxy proxy = new DataTreeChangeListenerProxy<>( + actorContext, mockListener, path); - final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME); - new Thread() { - @Override - public void run() { - proxy.init("shard-1", path); - } + new Thread(() -> proxy.init("shard-1")).start(); - }.start(); + Duration timeout = Duration.ofSeconds(5); + FindLocalShard findLocalShard = kit.expectMsgClass(timeout, FindLocalShard.class); + assertEquals("getShardName", "shard-1", findLocalShard.getShardName()); - FiniteDuration timeout = duration("5 seconds"); - FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class); - Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName()); + kit.reply(new LocalShardNotFound("shard-1")); - reply(new LocalShardNotFound("shard-1")); + kit.expectNoMessage(Duration.ofSeconds(1)); - expectNoMsg(duration("1 seconds")); - }}; + proxy.close(); } - @Test(timeout=10000) + @Test(timeout = 10000) public void testLocalShardNotInitialized() { - new JavaTestKit(getSystem()) {{ - ActorContext actorContext = new ActorContext(getSystem(), getRef(), - mock(ClusterWrapper.class), mock(Configuration.class)); - - final DataTreeChangeListenerProxy proxy = - new DataTreeChangeListenerProxy<>(actorContext, mockListener); - - final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME); - new Thread() { - @Override - public void run() { - proxy.init("shard-1", path); - } - - }.start(); - - FiniteDuration timeout = duration("5 seconds"); - FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class); - Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName()); - - reply(new NotInitializedException("not initialized")); - - new Within(duration("1 seconds")) { - @Override - protected void run() { - expectNoMsg(); - } - }; - }}; + final TestKit kit = new TestKit(getSystem()); + ActorContext actorContext = new ActorContext(getSystem(), kit.getRef(), mock(ClusterWrapper.class), + mock(Configuration.class)); + + final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME); + final DataTreeChangeListenerProxy proxy = new DataTreeChangeListenerProxy<>( + actorContext, mockListener, path); + + new Thread(() -> proxy.init("shard-1")).start(); + + Duration timeout = Duration.ofSeconds(5); + FindLocalShard findLocalShard = kit.expectMsgClass(timeout, FindLocalShard.class); + assertEquals("getShardName", "shard-1", findLocalShard.getShardName()); + + kit.reply(new NotInitializedException("not initialized")); + + kit.within(Duration.ofSeconds(1), () -> { + kit.expectNoMessage(); + return null; + }); + + proxy.close(); } @Test public void testFailedRegistration() { - new JavaTestKit(getSystem()) {{ - ActorSystem mockActorSystem = mock(ActorSystem.class); + final TestKit kit = new TestKit(getSystem()); + ActorSystem mockActorSystem = mock(ActorSystem.class); - ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class), - "testFailedRegistration"); - doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class)); - ExecutionContextExecutor executor = ExecutionContexts.fromExecutor( - MoreExecutors.sameThreadExecutor()); + ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class), "testFailedRegistration"); + doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class)); + ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(MoreExecutors.directExecutor()); + ActorContext actorContext = mock(ActorContext.class); + final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME); - ActorContext actorContext = mock(ActorContext.class); - final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME); + doReturn(executor).when(actorContext).getClientDispatcher(); + doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext(); + doReturn(mockActorSystem).when(actorContext).getActorSystem(); - doReturn(executor).when(actorContext).getClientDispatcher(); - doReturn(mockActorSystem).when(actorContext).getActorSystem(); + String shardName = "shard-1"; + final DataTreeChangeListenerProxy proxy = new DataTreeChangeListenerProxy<>( + actorContext, mockListener, path); - String shardName = "shard-1"; - final DataTreeChangeListenerProxy proxy = - new DataTreeChangeListenerProxy<>(actorContext, mockListener); + doReturn(kit.duration("5 seconds")).when(actorContext).getOperationDuration(); + doReturn(Futures.successful(kit.getRef())).when(actorContext).findLocalShardAsync(eq(shardName)); + doReturn(Futures.failed(new RuntimeException("mock"))).when(actorContext).executeOperationAsync( + any(ActorRef.class), any(Object.class), any(Timeout.class)); + doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext(); - doReturn(duration("5 seconds")).when(actorContext).getOperationDuration(); - doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName)); - doReturn(Futures.failed(new RuntimeException("mock"))). - when(actorContext).executeOperationAsync(any(ActorRef.class), - any(Object.class), any(Timeout.class)); - doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext(); + proxy.init("shard-1"); - proxy.init("shard-1", path); + assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor()); - Assert.assertEquals("getListenerRegistrationActor", null, - proxy.getListenerRegistrationActor()); - }}; + proxy.close(); } @Test public void testCloseBeforeRegistration() { - new JavaTestKit(getSystem()) {{ - ActorContext actorContext = mock(ActorContext.class); + final TestKit kit = new TestKit(getSystem()); + ActorContext actorContext = mock(ActorContext.class); - String shardName = "shard-1"; + String shardName = "shard-1"; - doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext(); - doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher(); - doReturn(getSystem()).when(actorContext).getActorSystem(); - doReturn(Dispatchers.DEFAULT_DISPATCHER_PATH).when(actorContext).getNotificationDispatcherPath(); - doReturn(getSystem().actorSelection(getRef().path())). - when(actorContext).actorSelection(getRef().path()); - doReturn(duration("5 seconds")).when(actorContext).getOperationDuration(); - doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName)); + doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext(); + doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher(); + doReturn(getSystem()).when(actorContext).getActorSystem(); + doReturn(Dispatchers.DEFAULT_DISPATCHER_PATH).when(actorContext).getNotificationDispatcherPath(); + doReturn(getSystem().actorSelection(kit.getRef().path())).when(actorContext).actorSelection( + kit.getRef().path()); + doReturn(kit.duration("5 seconds")).when(actorContext).getOperationDuration(); + doReturn(Futures.successful(kit.getRef())).when(actorContext).findLocalShardAsync(eq(shardName)); - final DataTreeChangeListenerProxy proxy = - new DataTreeChangeListenerProxy<>(actorContext, mockListener); + final DataTreeChangeListenerProxy proxy = new DataTreeChangeListenerProxy<>( + actorContext, mockListener, YangInstanceIdentifier.of(TestModel.TEST_QNAME)); + Answer> answer = invocation -> { + proxy.close(); + return Futures.successful((Object) new RegisterDataTreeNotificationListenerReply(kit.getRef())); + }; - Answer> answer = new Answer>() { - @Override - public Future answer(InvocationOnMock invocation) { - proxy.close(); - return Futures.successful((Object)new RegisterDataTreeChangeListenerReply(getRef())); - } - }; - - doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class), - any(Object.class), any(Timeout.class)); + doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class), any(Object.class), + any(Timeout.class)); - proxy.init(shardName, YangInstanceIdentifier.of(TestModel.TEST_QNAME)); + proxy.init(shardName); - expectMsgClass(duration("5 seconds"), CloseDataTreeChangeListenerRegistration.class); + kit.expectMsgClass(Duration.ofSeconds(5), CloseDataTreeNotificationListenerRegistration.class); - Assert.assertEquals("getListenerRegistrationActor", null, - proxy.getListenerRegistrationActor()); - }}; + assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor()); } }