Search in sources :

Example 51 with TestKit

use of akka.testkit.javadsl.TestKit in project controller by opendaylight.

the class ShardManagerTest method testShardAvailabilityChangeOnMemberUnreachableAndLeadershipChange.

@Test
public void testShardAvailabilityChangeOnMemberUnreachableAndLeadershipChange() throws Exception {
    LOG.info("testShardAvailabilityChangeOnMemberUnreachableAndLeadershipChange starting");
    String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
    // Create an ActorSystem ShardManager actor for member-1.
    final ActorSystem system1 = newActorSystem("Member1");
    Cluster.get(system1).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558"));
    final ActorRef mockShardActor1 = newMockShardActor(system1, Shard.DEFAULT_NAME, "member-1");
    final PrimaryShardInfoFutureCache primaryShardInfoCache = new PrimaryShardInfoFutureCache();
    final TestActorRef<TestShardManager> shardManager1 = TestActorRef.create(system1, newTestShardMgrBuilder().shardActor(mockShardActor1).cluster(new ClusterWrapperImpl(system1)).primaryShardInfoCache(primaryShardInfoCache).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
    // Create an ActorSystem ShardManager actor for member-2.
    final ActorSystem system2 = newActorSystem("Member2");
    Cluster.get(system2).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558"));
    final ActorRef mockShardActor2 = newMockShardActor(system2, Shard.DEFAULT_NAME, "member-2");
    MockConfiguration mockConfig2 = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("default", Arrays.asList("member-1", "member-2")).build());
    final TestActorRef<TestShardManager> shardManager2 = TestActorRef.create(system2, newTestShardMgrBuilder(mockConfig2).shardActor(mockShardActor2).cluster(new ClusterWrapperImpl(system2)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
    new TestKit(system1) {

        {
            shardManager1.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
            shardManager2.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
            shardManager1.tell(new ActorInitialized(), mockShardActor1);
            shardManager2.tell(new ActorInitialized(), mockShardActor2);
            String memberId2 = "member-2-shard-default-" + shardMrgIDSuffix;
            String memberId1 = "member-1-shard-default-" + shardMrgIDSuffix;
            shardManager1.tell(new ShardLeaderStateChanged(memberId1, memberId2, mock(DataTree.class), DataStoreVersions.CURRENT_VERSION), mockShardActor1);
            shardManager1.tell(new RoleChangeNotification(memberId1, RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor1);
            shardManager2.tell(new ShardLeaderStateChanged(memberId2, memberId2, mock(DataTree.class), DataStoreVersions.CURRENT_VERSION), mockShardActor2);
            shardManager2.tell(new RoleChangeNotification(memberId2, RaftState.Candidate.name(), RaftState.Leader.name()), mockShardActor2);
            shardManager1.underlyingActor().waitForMemberUp();
            shardManager1.tell(new FindPrimary("default", true), getRef());
            RemotePrimaryShardFound found = expectMsgClass(duration("5 seconds"), RemotePrimaryShardFound.class);
            String path = found.getPrimaryPath();
            assertTrue("Unexpected primary path " + path, path.contains("member-2-shard-default-config"));
            primaryShardInfoCache.putSuccessful("default", new PrimaryShardInfo(system1.actorSelection(mockShardActor1.path()), DataStoreVersions.CURRENT_VERSION));
            shardManager1.tell(MockClusterWrapper.createUnreachableMember("member-2", "akka://cluster-test@127.0.0.1:2558"), getRef());
            shardManager1.underlyingActor().waitForUnreachableMember();
            shardManager1.tell(new FindPrimary("default", true), getRef());
            expectMsgClass(duration("5 seconds"), NoShardLeaderException.class);
            assertNull("Expected primaryShardInfoCache entry removed", primaryShardInfoCache.getIfPresent("default"));
            shardManager1.tell(new ShardLeaderStateChanged(memberId1, memberId1, mock(DataTree.class), DataStoreVersions.CURRENT_VERSION), mockShardActor1);
            shardManager1.tell(new RoleChangeNotification(memberId1, RaftState.Follower.name(), RaftState.Leader.name()), mockShardActor1);
            shardManager1.tell(new FindPrimary("default", true), getRef());
            LocalPrimaryShardFound found1 = expectMsgClass(duration("5 seconds"), LocalPrimaryShardFound.class);
            String path1 = found1.getPrimaryPath();
            assertTrue("Unexpected primary path " + path1, path1.contains("member-1-shard-default-config"));
        }
    };
    LOG.info("testShardAvailabilityChangeOnMemberUnreachableAndLeadershipChange ending");
}
Also used : ActorSystem(akka.actor.ActorSystem) ClusterWrapperImpl(org.opendaylight.controller.cluster.datastore.ClusterWrapperImpl) UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) ShardLeaderStateChanged(org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) RoleChangeNotification(org.opendaylight.controller.cluster.notifications.RoleChangeNotification) AddressFromURIString(akka.actor.AddressFromURIString) TestKit(akka.testkit.javadsl.TestKit) RemotePrimaryShardFound(org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound) FindPrimary(org.opendaylight.controller.cluster.datastore.messages.FindPrimary) LocalPrimaryShardFound(org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound) PrimaryShardInfo(org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo) PrimaryShardInfoFutureCache(org.opendaylight.controller.cluster.datastore.utils.PrimaryShardInfoFutureCache) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) ActorInitialized(org.opendaylight.controller.cluster.datastore.messages.ActorInitialized) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 52 with TestKit

use of akka.testkit.javadsl.TestKit in project controller by opendaylight.

the class ActorContextTest method testExecuteRemoteOperationAsync.

@Test
@SuppressWarnings("checkstyle:IllegalCatch")
public void testExecuteRemoteOperationAsync() {
    new TestKit(getSystem()) {

        {
            ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
            ActorRef shardManagerActorRef = getSystem().actorOf(MockShardManager.props(true, shardActorRef));
            ActorContext actorContext = new ActorContext(getSystem(), shardManagerActorRef, mock(ClusterWrapper.class), mock(Configuration.class));
            ActorSelection actor = actorContext.actorSelection(shardActorRef.path());
            Future<Object> future = actorContext.executeOperationAsync(actor, "hello");
            try {
                Object result = Await.result(future, Duration.create(3, TimeUnit.SECONDS));
                assertEquals("Result", "hello", result);
            } catch (Exception e) {
                throw new AssertionError(e);
            }
        }
    };
}
Also used : Configuration(org.opendaylight.controller.cluster.datastore.config.Configuration) ActorSelection(akka.actor.ActorSelection) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) ClusterWrapper(org.opendaylight.controller.cluster.datastore.ClusterWrapper) EchoActor(org.opendaylight.controller.cluster.raft.utils.EchoActor) TestKit(akka.testkit.javadsl.TestKit) NotInitializedException(org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException) PrimaryNotFoundException(org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException) NoShardLeaderException(org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException) AbstractActorTest(org.opendaylight.controller.cluster.datastore.AbstractActorTest) Test(org.junit.Test)

Example 53 with TestKit

use of akka.testkit.javadsl.TestKit in project controller by opendaylight.

the class ActorContextTest method testSetDatastoreContext.

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

        {
            ActorContext actorContext = new ActorContext(getSystem(), getRef(), mock(ClusterWrapper.class), mock(Configuration.class), DatastoreContext.newBuilder().operationTimeoutInSeconds(5).shardTransactionCommitTimeoutInSeconds(7).build(), new PrimaryShardInfoFutureCache());
            assertEquals("getOperationDuration", 5, actorContext.getOperationDuration().toSeconds());
            assertEquals("getTransactionCommitOperationTimeout", 7, actorContext.getTransactionCommitOperationTimeout().duration().toSeconds());
            DatastoreContext newContext = DatastoreContext.newBuilder().operationTimeoutInSeconds(6).shardTransactionCommitTimeoutInSeconds(8).build();
            DatastoreContextFactory mockContextFactory = mock(DatastoreContextFactory.class);
            Mockito.doReturn(newContext).when(mockContextFactory).getBaseDatastoreContext();
            actorContext.setDatastoreContext(mockContextFactory);
            expectMsgClass(duration("5 seconds"), DatastoreContextFactory.class);
            Assert.assertSame("getDatastoreContext", newContext, actorContext.getDatastoreContext());
            assertEquals("getOperationDuration", 6, actorContext.getOperationDuration().toSeconds());
            assertEquals("getTransactionCommitOperationTimeout", 8, actorContext.getTransactionCommitOperationTimeout().duration().toSeconds());
        }
    };
}
Also used : Configuration(org.opendaylight.controller.cluster.datastore.config.Configuration) DatastoreContextFactory(org.opendaylight.controller.cluster.datastore.DatastoreContextFactory) DatastoreContext(org.opendaylight.controller.cluster.datastore.DatastoreContext) ClusterWrapper(org.opendaylight.controller.cluster.datastore.ClusterWrapper) TestKit(akka.testkit.javadsl.TestKit) AbstractActorTest(org.opendaylight.controller.cluster.datastore.AbstractActorTest) Test(org.junit.Test)

Example 54 with TestKit

use of akka.testkit.javadsl.TestKit in project controller by opendaylight.

the class ActorContextTest method testFindLocalShardWithShardNotFound.

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

        {
            ActorRef shardManagerActorRef = getSystem().actorOf(MockShardManager.props(false, null));
            ActorContext actorContext = new ActorContext(getSystem(), shardManagerActorRef, mock(ClusterWrapper.class), mock(Configuration.class));
            Optional<ActorRef> out = actorContext.findLocalShard("default");
            assertTrue(!out.isPresent());
        }
    };
}
Also used : Configuration(org.opendaylight.controller.cluster.datastore.config.Configuration) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) ClusterWrapper(org.opendaylight.controller.cluster.datastore.ClusterWrapper) TestKit(akka.testkit.javadsl.TestKit) AbstractActorTest(org.opendaylight.controller.cluster.datastore.AbstractActorTest) Test(org.junit.Test)

Example 55 with TestKit

use of akka.testkit.javadsl.TestKit in project controller by opendaylight.

the class ActorContextTest method testFindLocalShardWithShardFound.

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

        {
            within(duration("1 seconds"), () -> {
                ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
                ActorRef shardManagerActorRef = getSystem().actorOf(MockShardManager.props(true, shardActorRef));
                ActorContext actorContext = new ActorContext(getSystem(), shardManagerActorRef, mock(ClusterWrapper.class), mock(Configuration.class));
                Optional<ActorRef> out = actorContext.findLocalShard("default");
                assertEquals(shardActorRef, out.get());
                expectNoMsg();
                return null;
            });
        }
    };
}
Also used : Configuration(org.opendaylight.controller.cluster.datastore.config.Configuration) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) ClusterWrapper(org.opendaylight.controller.cluster.datastore.ClusterWrapper) EchoActor(org.opendaylight.controller.cluster.raft.utils.EchoActor) TestKit(akka.testkit.javadsl.TestKit) AbstractActorTest(org.opendaylight.controller.cluster.datastore.AbstractActorTest) Test(org.junit.Test)

Aggregations

TestKit (akka.testkit.javadsl.TestKit)124 Test (org.junit.Test)115 ActorRef (akka.actor.ActorRef)84 TestActorRef (akka.testkit.TestActorRef)63 AbstractShardManagerTest (org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)44 UpdateSchemaContext (org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext)37 AddressFromURIString (akka.actor.AddressFromURIString)28 ActorInitialized (org.opendaylight.controller.cluster.datastore.messages.ActorInitialized)26 FindLocalShard (org.opendaylight.controller.cluster.datastore.messages.FindLocalShard)22 RoleChangeNotification (org.opendaylight.controller.cluster.notifications.RoleChangeNotification)22 Configuration (org.opendaylight.controller.cluster.datastore.config.Configuration)17 ShardLeaderStateChanged (org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged)17 FindPrimary (org.opendaylight.controller.cluster.datastore.messages.FindPrimary)16 MockConfiguration (org.opendaylight.controller.cluster.datastore.utils.MockConfiguration)16 Props (akka.actor.Props)15 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)15 Failure (akka.actor.Status.Failure)14 AbstractActorTest (org.opendaylight.controller.cluster.datastore.AbstractActorTest)14 FiniteDuration (scala.concurrent.duration.FiniteDuration)14 ActorContext (org.opendaylight.controller.cluster.datastore.utils.ActorContext)13