Search in sources :

Example 26 with ActorContext

use of org.opendaylight.controller.cluster.datastore.utils.ActorContext in project controller by opendaylight.

the class AbstractDataStoreClientBehaviorTest method createActorContextMock.

private static ActorContext createActorContextMock(final ActorSystem system, final ActorRef actor) {
    final ActorContext mock = mock(ActorContext.class);
    final Promise<PrimaryShardInfo> promise = new scala.concurrent.impl.Promise.DefaultPromise<>();
    final ActorSelection selection = system.actorSelection(actor.path());
    final PrimaryShardInfo shardInfo = new PrimaryShardInfo(selection, (short) 0);
    promise.success(shardInfo);
    when(mock.findPrimaryShardAsync(SHARD)).thenReturn(promise.future());
    return mock;
}
Also used : ActorSelection(akka.actor.ActorSelection) PrimaryShardInfo(org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo) ActorContext(org.opendaylight.controller.cluster.datastore.utils.ActorContext) ClientActorContext(org.opendaylight.controller.cluster.access.client.ClientActorContext)

Example 27 with ActorContext

use of org.opendaylight.controller.cluster.datastore.utils.ActorContext in project controller by opendaylight.

the class DataChangeListenerProxyTest method testOnDataChanged.

@Test
public void testOnDataChanged() throws Exception {
    final ActorRef actorRef = getSystem().actorOf(MessageCollectorActor.props());
    DataChangeListenerProxy dataChangeListenerProxy = new DataChangeListenerProxy(getSystem().actorSelection(actorRef.path()));
    dataChangeListenerProxy.onDataChanged(new MockDataChangedEvent());
    // Check if it was received by the remote actor
    ActorContext testContext = new ActorContext(getSystem(), getSystem().actorOf(Props.create(DoNothingActor.class)), new MockClusterWrapper(), new MockConfiguration());
    Object messages = testContext.executeOperation(actorRef, MessageCollectorActor.GET_ALL_MESSAGES);
    Assert.assertNotNull(messages);
    Assert.assertTrue(messages instanceof List);
    List<?> listMessages = (List<?>) messages;
    Assert.assertEquals(1, listMessages.size());
    Assert.assertTrue(listMessages.get(0).getClass().equals(DataChanged.class));
}
Also used : DataChanged(org.opendaylight.controller.cluster.datastore.messages.DataChanged) ActorRef(akka.actor.ActorRef) MockClusterWrapper(org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) List(java.util.List) ActorContext(org.opendaylight.controller.cluster.datastore.utils.ActorContext) Test(org.junit.Test)

Example 28 with ActorContext

use of org.opendaylight.controller.cluster.datastore.utils.ActorContext in project controller by opendaylight.

the class DataChangeListenerRegistrationProxyTest method testSuccessfulRegistration.

@Test(timeout = 10000)
public void testSuccessfulRegistration() {
    new TestKit(getSystem()) {

        {
            ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class), mock(Configuration.class));
            final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy("shard-1", actorContext, mockListener);
            final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
            final DataChangeScope scope = AsyncDataBroker.DataChangeScope.ONE;
            new Thread(() -> proxy.init(path, scope)).start();
            FiniteDuration timeout = duration("5 seconds");
            FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
            Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
            reply(new LocalShardFound(getRef()));
            RegisterChangeListener registerMsg = expectMsgClass(timeout, RegisterChangeListener.class);
            Assert.assertEquals("getPath", path, registerMsg.getPath());
            Assert.assertEquals("getScope", scope, registerMsg.getScope());
            Assert.assertEquals("isRegisterOnAllInstances", false, registerMsg.isRegisterOnAllInstances());
            reply(new RegisterDataTreeNotificationListenerReply(getRef()));
            for (int i = 0; i < 20 * 5 && proxy.getListenerRegistrationActor() == null; i++) {
                Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
            }
            Assert.assertEquals("getListenerRegistrationActor", getSystem().actorSelection(getRef().path()), proxy.getListenerRegistrationActor());
            watch(proxy.getDataChangeListenerActor());
            proxy.close();
            // The listener registration actor should get a Close message
            expectMsgClass(timeout, CloseDataTreeNotificationListenerRegistration.class);
            // The DataChangeListener actor should be terminated
            expectMsgClass(timeout, Terminated.class);
            proxy.close();
            expectNoMsg();
        }
    };
}
Also used : DataChangeScope(org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope) RegisterDataTreeNotificationListenerReply(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply) LocalShardFound(org.opendaylight.controller.cluster.datastore.messages.LocalShardFound) Configuration(org.opendaylight.controller.cluster.datastore.config.Configuration) RegisterChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener) FiniteDuration(scala.concurrent.duration.FiniteDuration) FindLocalShard(org.opendaylight.controller.cluster.datastore.messages.FindLocalShard) TestKit(akka.testkit.javadsl.TestKit) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) ActorContext(org.opendaylight.controller.cluster.datastore.utils.ActorContext) Test(org.junit.Test)

Example 29 with ActorContext

use of org.opendaylight.controller.cluster.datastore.utils.ActorContext in project controller by opendaylight.

the class DataTreeChangeListenerProxyTest method testCloseBeforeRegistration.

@Test
public void testCloseBeforeRegistration() {
    new TestKit(getSystem()) {

        {
            ActorContext actorContext = mock(ActorContext.class);
            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));
            final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(actorContext, mockListener, YangInstanceIdentifier.of(TestModel.TEST_QNAME));
            Answer<Future<Object>> answer = invocation -> {
                proxy.close();
                return Futures.successful((Object) new RegisterDataTreeNotificationListenerReply(getRef()));
            };
            doAnswer(answer).when(actorContext).executeOperationAsync(any(ActorRef.class), any(Object.class), any(Timeout.class));
            proxy.init(shardName);
            expectMsgClass(duration("5 seconds"), CloseDataTreeNotificationListenerRegistration.class);
            Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());
        }
    };
}
Also used : Configuration(org.opendaylight.controller.cluster.datastore.config.Configuration) Dispatchers(org.opendaylight.controller.cluster.common.actor.Dispatchers) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) Terminated(akka.actor.Terminated) NotInitializedException(org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException) TestModel(org.opendaylight.controller.md.cluster.datastore.model.TestModel) DOMDataTreeChangeListener(org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener) Timeout(akka.util.Timeout) LocalShardFound(org.opendaylight.controller.cluster.datastore.messages.LocalShardFound) Answer(org.mockito.stubbing.Answer) LocalShardNotFound(org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound) DoNothingActor(org.opendaylight.controller.cluster.raft.utils.DoNothingActor) ActorRef(akka.actor.ActorRef) Matchers.eq(org.mockito.Matchers.eq) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ActorContext(org.opendaylight.controller.cluster.datastore.utils.ActorContext) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) CloseDataTreeNotificationListenerRegistration(org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistration) ClusteredDOMDataTreeChangeListener(org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener) Mockito.doReturn(org.mockito.Mockito.doReturn) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) Future(scala.concurrent.Future) FiniteDuration(scala.concurrent.duration.FiniteDuration) Test(org.junit.Test) TestKit(akka.testkit.javadsl.TestKit) ExecutionContexts(akka.dispatch.ExecutionContexts) Matchers.any(org.mockito.Matchers.any) Futures(akka.dispatch.Futures) TimeUnit(java.util.concurrent.TimeUnit) RegisterDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener) RegisterDataTreeNotificationListenerReply(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply) ActorSystem(akka.actor.ActorSystem) ExecutionContextExecutor(scala.concurrent.ExecutionContextExecutor) FindLocalShard(org.opendaylight.controller.cluster.datastore.messages.FindLocalShard) Assert(org.junit.Assert) Props(akka.actor.Props) Mockito.mock(org.mockito.Mockito.mock) RegisterDataTreeNotificationListenerReply(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply) ActorRef(akka.actor.ActorRef) Timeout(akka.util.Timeout) TestKit(akka.testkit.javadsl.TestKit) ActorContext(org.opendaylight.controller.cluster.datastore.utils.ActorContext) DOMDataTreeChangeListener(org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener) ClusteredDOMDataTreeChangeListener(org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener) Future(scala.concurrent.Future) Test(org.junit.Test)

Example 30 with ActorContext

use of org.opendaylight.controller.cluster.datastore.utils.ActorContext in project controller by opendaylight.

the class DataTreeChangeListenerProxyTest method testSuccessfulRegistrationForClusteredListener.

@Test(timeout = 10000)
public void testSuccessfulRegistrationForClusteredListener() {
    new TestKit(getSystem()) {

        {
            ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class), mock(Configuration.class));
            ClusteredDOMDataTreeChangeListener mockClusteredListener = mock(ClusteredDOMDataTreeChangeListener.class);
            final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
            final DataTreeChangeListenerProxy<ClusteredDOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(actorContext, mockClusteredListener, path);
            new Thread(() -> proxy.init("shard-1")).start();
            FiniteDuration timeout = duration("5 seconds");
            FindLocalShard findLocalShard = expectMsgClass(timeout, FindLocalShard.class);
            Assert.assertEquals("getShardName", "shard-1", findLocalShard.getShardName());
            reply(new LocalShardFound(getRef()));
            RegisterDataTreeChangeListener registerMsg = expectMsgClass(timeout, RegisterDataTreeChangeListener.class);
            Assert.assertEquals("getPath", path, registerMsg.getPath());
            Assert.assertEquals("isRegisterOnAllInstances", true, registerMsg.isRegisterOnAllInstances());
            proxy.close();
        }
    };
}
Also used : LocalShardFound(org.opendaylight.controller.cluster.datastore.messages.LocalShardFound) Configuration(org.opendaylight.controller.cluster.datastore.config.Configuration) FiniteDuration(scala.concurrent.duration.FiniteDuration) FindLocalShard(org.opendaylight.controller.cluster.datastore.messages.FindLocalShard) TestKit(akka.testkit.javadsl.TestKit) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) RegisterDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener) ActorContext(org.opendaylight.controller.cluster.datastore.utils.ActorContext) ClusteredDOMDataTreeChangeListener(org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener) Test(org.junit.Test)

Aggregations

ActorContext (org.opendaylight.controller.cluster.datastore.utils.ActorContext)36 Test (org.junit.Test)15 ActorRef (akka.actor.ActorRef)13 TestKit (akka.testkit.javadsl.TestKit)13 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)13 Configuration (org.opendaylight.controller.cluster.datastore.config.Configuration)11 FiniteDuration (scala.concurrent.duration.FiniteDuration)11 FindLocalShard (org.opendaylight.controller.cluster.datastore.messages.FindLocalShard)10 Before (org.junit.Before)7 ClientActorContext (org.opendaylight.controller.cluster.access.client.ClientActorContext)7 Props (akka.actor.Props)6 LocalShardFound (org.opendaylight.controller.cluster.datastore.messages.LocalShardFound)6 ClusteredDOMDataTreeChangeListener (org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener)6 TestProbe (akka.testkit.TestProbe)5 RegisterDataTreeNotificationListenerReply (org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply)5 DOMDataTreeChangeListener (org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener)5 ActorSelection (akka.actor.ActorSelection)4 ActorSystem (akka.actor.ActorSystem)4 Timeout (akka.util.Timeout)4 PrimaryShardInfo (org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo)4