Search in sources :

Example 21 with TestKit

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);
}
Also used : DOMEntityOwnershipListener(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener) DOMEntityOwnershipChange(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) ArrayList(java.util.ArrayList) DOMEntity(org.opendaylight.mdsal.eos.dom.api.DOMEntity) TestKit(akka.testkit.javadsl.TestKit) Test(org.junit.Test)

Example 22 with TestKit

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());
}
Also used : LoadSnapshot(akka.persistence.SnapshotProtocol.LoadSnapshot) LoadSnapshotResult(akka.persistence.SnapshotProtocol.LoadSnapshotResult) SelectedSnapshot(akka.persistence.SelectedSnapshot) SnapshotMetadata(akka.persistence.SnapshotMetadata) TestKit(akka.testkit.javadsl.TestKit) File(java.io.File) Test(org.junit.Test)

Example 23 with TestKit

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();
}
Also used : SnapshotProtocol(akka.persistence.SnapshotProtocol) LoadSnapshotFailed(akka.persistence.SnapshotProtocol.LoadSnapshotFailed) LoadSnapshot(akka.persistence.SnapshotProtocol.LoadSnapshot) TestKit(akka.testkit.javadsl.TestKit) Test(org.junit.Test)

Example 24 with TestKit

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());
}
Also used : LoadSnapshot(akka.persistence.SnapshotProtocol.LoadSnapshot) LoadSnapshotResult(akka.persistence.SnapshotProtocol.LoadSnapshotResult) SelectedSnapshot(akka.persistence.SelectedSnapshot) SnapshotMetadata(akka.persistence.SnapshotMetadata) TestKit(akka.testkit.javadsl.TestKit) Test(org.junit.Test)

Example 25 with TestKit

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);
}
Also used : HashMap(java.util.HashMap) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) ByteString(com.google.protobuf.ByteString) TestKit(akka.testkit.javadsl.TestKit) Terminated(akka.actor.Terminated)

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