Search in sources :

Example 6 with TestActorRef

use of akka.testkit.TestActorRef in project controller by opendaylight.

the class DataChangeListenerSupportTest method testChangeListenerWithNoInitialData.

@Test
public void testChangeListenerWithNoInitialData() throws Exception {
    new ShardTestKit(getSystem()) {

        {
            final TestActorRef<Shard> actor = actorFactory.createTestActor(newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testChangeListenerWithNoInitialData");
            waitUntilLeader(actor);
            final Shard shard = actor.underlyingActor();
            final MockDataChangeListener listener = new MockDataChangeListener(0);
            final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener, TEST_PATH), "testChangeListenerWithNoInitialData-DataChangeListener");
            final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
            support.onMessage(new RegisterChangeListener(TEST_PATH, dclActor, DataChangeScope.ONE, false), true, true);
            listener.expectNoMoreChanges("Unexpected initial change event");
        }
    };
}
Also used : ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) MockDataChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener) RegisterChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener) Test(org.junit.Test)

Example 7 with TestActorRef

use of akka.testkit.TestActorRef in project controller by opendaylight.

the class DataChangeListenerSupportTest method testInitialChangeListenerEventWhenNotInitiallyLeader.

@Test
public void testInitialChangeListenerEventWhenNotInitiallyLeader() throws Exception {
    new ShardTestKit(getSystem()) {

        {
            final TestActorRef<Shard> actor = actorFactory.createTestActor(newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testInitialChangeListenerEventWhenNotInitiallyLeader");
            waitUntilLeader(actor);
            final Shard shard = actor.underlyingActor();
            mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(outerNode(outerNodeEntry(1, innerNode("one", "two")), outerNodeEntry(2, innerNode("three", "four")))));
            final MockDataChangeListener listener = new MockDataChangeListener(0);
            final YangInstanceIdentifier path = OUTER_LIST_PATH.node(OUTER_LIST_QNAME).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME);
            final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener, path), "testInitialChangeListenerEventWhenNotInitiallyLeader-DataChangeListener");
            final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
            support.onMessage(new RegisterChangeListener(path, dclActor, DataChangeScope.ONE, false), false, true);
            listener.expectNoMoreChanges("Unexpected initial change event");
            listener.reset(1);
            support.onLeadershipChange(true, true);
            listener.waitForChangeEvents();
            listener.verifyCreatedData(0, innerEntryPath(1, "one"));
            listener.verifyCreatedData(0, innerEntryPath(1, "two"));
            listener.verifyCreatedData(0, innerEntryPath(2, "three"));
            listener.verifyCreatedData(0, innerEntryPath(2, "four"));
        }
    };
}
Also used : ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) MockDataChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener) RegisterChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Example 8 with TestActorRef

use of akka.testkit.TestActorRef in project controller by opendaylight.

the class DataChangeListenerSupportTest method testInitialChangeListenerEventWithContainerPath.

@Test
public void testInitialChangeListenerEventWithContainerPath() throws Exception {
    new ShardTestKit(getSystem()) {

        {
            final TestActorRef<Shard> actor = actorFactory.createTestActor(newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testInitialChangeListenerEventWithContainerPath");
            waitUntilLeader(actor);
            final Shard shard = actor.underlyingActor();
            writeToStore(shard.getDataStore(), TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
            final MockDataChangeListener listener = new MockDataChangeListener(1);
            final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener, TEST_PATH), "testInitialChangeListenerEventWithContainerPath-DataChangeListener");
            final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
            support.onMessage(new RegisterChangeListener(TEST_PATH, dclActor, DataChangeScope.ONE, false), true, true);
            listener.waitForChangeEvents(TEST_PATH);
        }
    };
}
Also used : ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) MockDataChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener) RegisterChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener) Test(org.junit.Test)

Example 9 with TestActorRef

use of akka.testkit.TestActorRef in project controller by opendaylight.

the class RaftActorTest method testRaftActorForwardsToRaftActorSnapshotMessageSupport.

@Test
public void testRaftActorForwardsToRaftActorSnapshotMessageSupport() {
    String persistenceId = factory.generateActorId("leader-");
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
    RaftActorSnapshotMessageSupport mockSupport = mock(RaftActorSnapshotMessageSupport.class);
    TestActorRef<MockRaftActor> mockActorRef = factory.createTestActor(MockRaftActor.builder().id(persistenceId).config(config).snapshotMessageSupport(mockSupport).props());
    MockRaftActor mockRaftActor = mockActorRef.underlyingActor();
    // Wait for akka's recovery to complete so it doesn't interfere.
    mockRaftActor.waitForRecoveryComplete();
    ApplySnapshot applySnapshot = new ApplySnapshot(mock(Snapshot.class));
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(applySnapshot), any(ActorRef.class));
    mockRaftActor.handleCommand(applySnapshot);
    CaptureSnapshotReply captureSnapshotReply = new CaptureSnapshotReply(ByteState.empty(), java.util.Optional.empty());
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(captureSnapshotReply), any(ActorRef.class));
    mockRaftActor.handleCommand(captureSnapshotReply);
    SaveSnapshotSuccess saveSnapshotSuccess = new SaveSnapshotSuccess(new SnapshotMetadata("", 0L, 0L));
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(saveSnapshotSuccess), any(ActorRef.class));
    mockRaftActor.handleCommand(saveSnapshotSuccess);
    SaveSnapshotFailure saveSnapshotFailure = new SaveSnapshotFailure(new SnapshotMetadata("", 0L, 0L), new Throwable());
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(saveSnapshotFailure), any(ActorRef.class));
    mockRaftActor.handleCommand(saveSnapshotFailure);
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT), any(ActorRef.class));
    mockRaftActor.handleCommand(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT);
    doReturn(true).when(mockSupport).handleSnapshotMessage(same(GetSnapshot.INSTANCE), any(ActorRef.class));
    mockRaftActor.handleCommand(GetSnapshot.INSTANCE);
    verify(mockSupport).handleSnapshotMessage(same(applySnapshot), any(ActorRef.class));
    verify(mockSupport).handleSnapshotMessage(same(captureSnapshotReply), any(ActorRef.class));
    verify(mockSupport).handleSnapshotMessage(same(saveSnapshotSuccess), any(ActorRef.class));
    verify(mockSupport).handleSnapshotMessage(same(saveSnapshotFailure), any(ActorRef.class));
    verify(mockSupport).handleSnapshotMessage(same(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT), any(ActorRef.class));
    verify(mockSupport).handleSnapshotMessage(same(GetSnapshot.INSTANCE), any(ActorRef.class));
}
Also used : CaptureSnapshotReply(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply) ApplySnapshot(org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) FiniteDuration(scala.concurrent.duration.FiniteDuration) ByteString(com.google.protobuf.ByteString) SaveSnapshotFailure(akka.persistence.SaveSnapshotFailure) ApplySnapshot(org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) GetSnapshot(org.opendaylight.controller.cluster.raft.client.messages.GetSnapshot) SnapshotMetadata(akka.persistence.SnapshotMetadata) SaveSnapshotSuccess(akka.persistence.SaveSnapshotSuccess) Test(org.junit.Test)

Example 10 with TestActorRef

use of akka.testkit.TestActorRef in project controller by opendaylight.

the class RaftActorTest method testFakeSnapshotsForLeaderWithInRealSnapshots.

@Test
public void testFakeSnapshotsForLeaderWithInRealSnapshots() throws Exception {
    final String persistenceId = factory.generateActorId("leader-");
    final String follower1Id = factory.generateActorId("follower-");
    ActorRef followerActor1 = factory.createActor(MessageCollectorActor.props());
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
    config.setIsolatedLeaderCheckInterval(new FiniteDuration(1, TimeUnit.DAYS));
    DataPersistenceProvider dataPersistenceProvider = mock(DataPersistenceProvider.class);
    Map<String, String> peerAddresses = new HashMap<>();
    peerAddresses.put(follower1Id, followerActor1.path().toString());
    TestActorRef<MockRaftActor> mockActorRef = factory.createTestActor(MockRaftActor.props(persistenceId, peerAddresses, config, dataPersistenceProvider), persistenceId);
    MockRaftActor leaderActor = mockActorRef.underlyingActor();
    leaderActor.getRaftActorContext().setCommitIndex(4);
    leaderActor.getRaftActorContext().setLastApplied(4);
    leaderActor.getRaftActorContext().getTermInformation().update(1, persistenceId);
    leaderActor.waitForInitializeBehaviorComplete();
    // create 8 entries in the log - 0 to 4 are applied and will get picked up as part of the capture snapshot
    Leader leader = new Leader(leaderActor.getRaftActorContext());
    leaderActor.setCurrentBehavior(leader);
    assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
    MockRaftActorContext.MockReplicatedLogBuilder logBuilder = new MockRaftActorContext.MockReplicatedLogBuilder();
    leaderActor.getRaftActorContext().setReplicatedLog(logBuilder.createEntries(0, 8, 1).build());
    assertEquals(8, leaderActor.getReplicatedLog().size());
    leaderActor.getRaftActorContext().getSnapshotManager().capture(new SimpleReplicatedLogEntry(6, 1, new MockRaftActorContext.MockPayload("x")), 4);
    verify(leaderActor.snapshotCohortDelegate).createSnapshot(anyObject(), anyObject());
    assertEquals(8, leaderActor.getReplicatedLog().size());
    assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
    // fake snapshot on index 5
    leaderActor.onReceiveCommand(new AppendEntriesReply(follower1Id, 1, true, 5, 1, (short) 0));
    assertEquals(8, leaderActor.getReplicatedLog().size());
    // fake snapshot on index 6
    assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
    leaderActor.onReceiveCommand(new AppendEntriesReply(follower1Id, 1, true, 6, 1, (short) 0));
    assertEquals(8, leaderActor.getReplicatedLog().size());
    assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
    assertEquals(8, leaderActor.getReplicatedLog().size());
    MockSnapshotState snapshotState = new MockSnapshotState(Arrays.asList(new MockRaftActorContext.MockPayload("foo-0"), new MockRaftActorContext.MockPayload("foo-1"), new MockRaftActorContext.MockPayload("foo-2"), new MockRaftActorContext.MockPayload("foo-3"), new MockRaftActorContext.MockPayload("foo-4")));
    leaderActor.getRaftActorContext().getSnapshotManager().persist(snapshotState, java.util.Optional.empty(), Runtime.getRuntime().totalMemory());
    assertTrue(leaderActor.getRaftActorContext().getSnapshotManager().isCapturing());
    // The commit is needed to complete the snapshot creation process
    leaderActor.getRaftActorContext().getSnapshotManager().commit(-1, -1);
    // capture snapshot reply should remove the snapshotted entries only
    assertEquals(3, leaderActor.getReplicatedLog().size());
    assertEquals(7, leaderActor.getReplicatedLog().lastIndex());
    // add another non-replicated entry
    leaderActor.getReplicatedLog().append(new SimpleReplicatedLogEntry(8, 1, new MockRaftActorContext.MockPayload("foo-8")));
    // fake snapshot on index 7, since lastApplied = 7 , we would keep the last applied
    leaderActor.onReceiveCommand(new AppendEntriesReply(follower1Id, 1, true, 7, 1, (short) 0));
    assertEquals(2, leaderActor.getReplicatedLog().size());
    assertEquals(8, leaderActor.getReplicatedLog().lastIndex());
}
Also used : MockSnapshotState(org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState) Leader(org.opendaylight.controller.cluster.raft.behaviors.Leader) HashMap(java.util.HashMap) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) FiniteDuration(scala.concurrent.duration.FiniteDuration) ByteString(com.google.protobuf.ByteString) MockPayload(org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) DataPersistenceProvider(org.opendaylight.controller.cluster.DataPersistenceProvider) Test(org.junit.Test)

Aggregations

ActorRef (akka.actor.ActorRef)65 TestActorRef (akka.testkit.TestActorRef)65 Test (org.junit.Test)64 FiniteDuration (scala.concurrent.duration.FiniteDuration)18 TestKit (akka.testkit.javadsl.TestKit)17 AddServer (org.opendaylight.controller.cluster.raft.messages.AddServer)14 AddServerReply (org.opendaylight.controller.cluster.raft.messages.AddServerReply)13 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)13 DisableElectionsRaftPolicy (org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy)12 UpdateSchemaContext (org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext)11 ByteString (com.google.protobuf.ByteString)10 RegisterChangeListener (org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener)10 MockDataChangeListener (org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener)10 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)10 AddressFromURIString (akka.actor.AddressFromURIString)9 AbstractShardManagerTest (org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)9 RegisterDataTreeNotificationListenerReply (org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply)9 RoleChangeNotification (org.opendaylight.controller.cluster.notifications.RoleChangeNotification)9 Follower (org.opendaylight.controller.cluster.raft.behaviors.Follower)9 ShardLeaderStateChanged (org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged)8