use of akka.testkit.javadsl.TestKit in project controller by opendaylight.
the class EntityOwnershipListenerSupportTest method testNotifyEntityOwnershipListeners.
@Test
public void testNotifyEntityOwnershipListeners() {
EntityOwnershipListenerSupport support = new EntityOwnershipListenerSupport(actorContext, "test");
DOMEntityOwnershipListener mockListener1 = mock(DOMEntityOwnershipListener.class, "EntityOwnershipListener1");
DOMEntityOwnershipListener mockListener2 = mock(DOMEntityOwnershipListener.class, "EntityOwnershipListener2");
DOMEntityOwnershipListener mockListener12 = mock(DOMEntityOwnershipListener.class, "EntityOwnershipListener1_2");
String entityType1 = "type1";
String entityType2 = "type2";
final DOMEntity entity1 = new DOMEntity(entityType1, YangInstanceIdentifier.of(QName.create("test", "id1")));
final DOMEntity entity2 = new DOMEntity(entityType2, YangInstanceIdentifier.of(QName.create("test", "id2")));
final DOMEntity entity3 = new DOMEntity("noListener", YangInstanceIdentifier.of(QName.create("test", "id5")));
// Add EntityOwnershipListener registrations.
support.addEntityOwnershipListener(entityType1, mockListener1);
// register again - should be noop
support.addEntityOwnershipListener(entityType1, mockListener1);
support.addEntityOwnershipListener(entityType1, mockListener12);
support.addEntityOwnershipListener(entityType2, mockListener2);
// Notify entity1 changed and verify appropriate listeners are notified.
support.notifyEntityOwnershipListeners(entity1, false, true, true);
verify(mockListener1, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, true, true));
verify(mockListener12, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, true, true));
Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
verify(mockListener2, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
assertEquals("# of listener actors", 2, actorContext.children().size());
reset(mockListener1, mockListener2, mockListener12);
// Notify entity2 changed and verify appropriate listeners are notified.
support.notifyEntityOwnershipListeners(entity2, false, true, true);
verify(mockListener2, timeout(5000)).ownershipChanged(ownershipChange(entity2, false, true, true));
Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
verify(mockListener1, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
verify(mockListener12, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
assertEquals("# of listener actors", 3, actorContext.children().size());
reset(mockListener1, mockListener2, mockListener12);
// Notify entity3 changed and verify no listeners are notified.
support.notifyEntityOwnershipListeners(entity3, true, false, true);
Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
verify(mockListener1, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
verify(mockListener2, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
verify(mockListener12, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
reset(mockListener1, mockListener2, mockListener12);
Iterable<ActorRef> listenerActors = actorContext.children();
assertEquals("# of listener actors", 3, listenerActors.size());
// Unregister mockListener1, issue a change for entity1 and verify only remaining listeners are notified.
support.removeEntityOwnershipListener(entityType1, mockListener1);
support.notifyEntityOwnershipListeners(entity1, true, false, true);
verify(mockListener12, timeout(5000)).ownershipChanged(ownershipChange(entity1, true, false, true));
Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
verify(mockListener1, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
reset(mockListener1, mockListener2, mockListener12);
// Unregister all listeners and verify their listener actors are destroyed.
List<TestKit> watchers = new ArrayList<>();
for (Iterator<ActorRef> iter = listenerActors.iterator(); iter.hasNext(); ) {
TestKit kit = new TestKit(getSystem());
kit.watch(iter.next());
watchers.add(kit);
}
support.removeEntityOwnershipListener(entityType1, mockListener12);
// un-register again - should be noop
support.removeEntityOwnershipListener(entityType1, mockListener12);
support.removeEntityOwnershipListener(entityType2, mockListener2);
Iterator<ActorRef> iter = listenerActors.iterator();
for (TestKit kit : watchers) {
kit.expectTerminated(kit.duration("3 seconds"), iter.next());
}
assertEquals("# of listener actors", 0, actorContext.children().size());
// Re-register mockListener1 and verify it is notified.
reset(mockListener1, mockListener2);
support.addEntityOwnershipListener(entityType1, mockListener1);
support.notifyEntityOwnershipListeners(entity1, false, false, true);
verify(mockListener1, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, false, true));
verify(mockListener12, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
verify(mockListener2, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
// Quickly register and unregister mockListener2 - expecting no exceptions.
support.addEntityOwnershipListener(entityType1, mockListener2);
support.removeEntityOwnershipListener(entityType1, mockListener2);
}
use of akka.testkit.javadsl.TestKit in project controller by opendaylight.
the class LocalSnapshotStoreTest method testDoLoadAsync.
@Test
public void testDoLoadAsync() throws IOException {
createSnapshotFile(PERSISTENCE_ID, "one", 0, 1000);
createSnapshotFile(PERSISTENCE_ID, "two", 1, 2000);
createSnapshotFile(PERSISTENCE_ID, "three", 1, 3000);
createSnapshotFile(PREFIX_BASED_SHARD_PERSISTENCE_ID, "foo", 0, 1000);
createSnapshotFile(PREFIX_BASED_SHARD_PERSISTENCE_ID, "bar", 1, 2000);
createSnapshotFile(PREFIX_BASED_SHARD_PERSISTENCE_ID, "foobar", 1, 3000);
createSnapshotFile("member-1-shard-default-oper", "foo", 0, 1000);
createSnapshotFile("member-1-shard-toaster-oper", "foo", 0, 1000);
new File(SNAPSHOT_DIR, "other").createNewFile();
new File(SNAPSHOT_DIR, "other-1485349217290").createNewFile();
SnapshotMetadata metadata3 = new SnapshotMetadata(PERSISTENCE_ID, 1, 3000);
TestKit probe = new TestKit(system);
snapshotStore.tell(new LoadSnapshot(PERSISTENCE_ID, SnapshotSelectionCriteria.latest(), Long.MAX_VALUE), probe.getRef());
LoadSnapshotResult result = probe.expectMsgClass(LoadSnapshotResult.class);
Option<SelectedSnapshot> possibleSnapshot = result.snapshot();
assertEquals("SelectedSnapshot present", TRUE, possibleSnapshot.nonEmpty());
assertEquals("SelectedSnapshot metadata", metadata3, possibleSnapshot.get().metadata());
assertEquals("SelectedSnapshot snapshot", "three", possibleSnapshot.get().snapshot());
snapshotStore.tell(new LoadSnapshot(PREFIX_BASED_SHARD_PERSISTENCE_ID, SnapshotSelectionCriteria.latest(), Long.MAX_VALUE), probe.getRef());
result = probe.expectMsgClass(LoadSnapshotResult.class);
possibleSnapshot = result.snapshot();
SnapshotMetadata prefixBasedShardMetada3 = new SnapshotMetadata(PREFIX_BASED_SHARD_PERSISTENCE_ID, 1, 3000);
assertEquals("SelectedSnapshot present", TRUE, possibleSnapshot.nonEmpty());
assertEquals("SelectedSnapshot metadata", prefixBasedShardMetada3, possibleSnapshot.get().metadata());
assertEquals("SelectedSnapshot snapshot", "foobar", possibleSnapshot.get().snapshot());
}
use of akka.testkit.javadsl.TestKit in project controller by opendaylight.
the class LocalSnapshotStoreTest method testDoLoadAsyncWithFailure.
@SuppressWarnings("checkstyle:illegalThrows")
@Test(expected = IOException.class)
public void testDoLoadAsyncWithFailure() throws Throwable {
createSnapshotFile(PERSISTENCE_ID, null, 1, 2000);
TestKit probe = new TestKit(system);
snapshotStore.tell(new SnapshotProtocol.LoadSnapshot(PERSISTENCE_ID, SnapshotSelectionCriteria.latest(), Long.MAX_VALUE), probe.getRef());
LoadSnapshotFailed failed = probe.expectMsgClass(LoadSnapshotFailed.class);
throw failed.cause();
}
use of akka.testkit.javadsl.TestKit in project controller by opendaylight.
the class LocalSnapshotStoreTest method testDoLoadAsyncWithRetry.
@Test
public void testDoLoadAsyncWithRetry() throws IOException {
createSnapshotFile(PERSISTENCE_ID, "one", 0, 1000);
createSnapshotFile(PERSISTENCE_ID, null, 1, 2000);
SnapshotMetadata metadata = new SnapshotMetadata(PERSISTENCE_ID, 0, 1000);
TestKit probe = new TestKit(system);
snapshotStore.tell(new LoadSnapshot(PERSISTENCE_ID, SnapshotSelectionCriteria.latest(), Long.MAX_VALUE), probe.getRef());
LoadSnapshotResult result = probe.expectMsgClass(LoadSnapshotResult.class);
Option<SelectedSnapshot> possibleSnapshot = result.snapshot();
assertEquals("SelectedSnapshot present", TRUE, possibleSnapshot.nonEmpty());
assertEquals("SelectedSnapshot metadata", metadata, possibleSnapshot.get().metadata());
assertEquals("SelectedSnapshot snapshot", "one", possibleSnapshot.get().snapshot());
}
use of akka.testkit.javadsl.TestKit in project controller by opendaylight.
the class LeaderTest method setupIsolatedLeaderCheckTestWithTwoFollowers.
private RaftActorBehavior setupIsolatedLeaderCheckTestWithTwoFollowers(final RaftPolicy raftPolicy) {
ActorRef followerActor1 = getSystem().actorOf(MessageCollectorActor.props(), "follower-1");
ActorRef followerActor2 = getSystem().actorOf(MessageCollectorActor.props(), "follower-2");
MockRaftActorContext leaderActorContext = createActorContext();
Map<String, String> peerAddresses = new HashMap<>();
peerAddresses.put("follower-1", followerActor1.path().toString());
peerAddresses.put("follower-2", followerActor2.path().toString());
leaderActorContext.setPeerAddresses(peerAddresses);
leaderActorContext.setRaftPolicy(raftPolicy);
leader = new Leader(leaderActorContext);
leader.markFollowerActive("follower-1");
leader.markFollowerActive("follower-2");
RaftActorBehavior newBehavior = leader.handleMessage(leaderActor, Leader.ISOLATED_LEADER_CHECK);
assertTrue("Behavior not instance of Leader when all followers are active", newBehavior instanceof Leader);
// kill 1 follower and verify if that got killed
final TestKit probe = new TestKit(getSystem());
probe.watch(followerActor1);
followerActor1.tell(PoisonPill.getInstance(), ActorRef.noSender());
final Terminated termMsg1 = probe.expectMsgClass(Terminated.class);
assertEquals(termMsg1.getActor(), followerActor1);
leader.markFollowerInActive("follower-1");
leader.markFollowerActive("follower-2");
newBehavior = leader.handleMessage(leaderActor, Leader.ISOLATED_LEADER_CHECK);
assertTrue("Behavior not instance of Leader when majority of followers are active", newBehavior instanceof Leader);
// kill 2nd follower and leader should change to Isolated leader
followerActor2.tell(PoisonPill.getInstance(), null);
probe.watch(followerActor2);
followerActor2.tell(PoisonPill.getInstance(), ActorRef.noSender());
final Terminated termMsg2 = probe.expectMsgClass(Terminated.class);
assertEquals(termMsg2.getActor(), followerActor2);
leader.markFollowerInActive("follower-2");
return leader.handleMessage(leaderActor, Leader.ISOLATED_LEADER_CHECK);
}
Aggregations