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");
}
};
}
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"));
}
};
}
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);
}
};
}
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));
}
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());
}
Aggregations