Search in sources :

Example 1 with LeaderLocationListener

use of org.opendaylight.controller.cluster.dom.api.LeaderLocationListener in project controller by opendaylight.

the class CDSShardAccessImplTest method testRegisterLeaderLocationListener.

@Test
@SuppressWarnings("checkstyle:IllegalCatch")
public void testRegisterLeaderLocationListener() {
    final LeaderLocationListener listener1 = mock(LeaderLocationListener.class);
    // first registration should be OK
    shardAccess.registerLeaderLocationListener(listener1);
    // second registration should fail with IllegalArgumentEx
    try {
        shardAccess.registerLeaderLocationListener(listener1);
        fail("Should throw exception");
    } catch (final Exception e) {
        assertTrue(e instanceof IllegalArgumentException);
    }
    // null listener registration should fail with NPE
    try {
        shardAccess.registerLeaderLocationListener(null);
        fail("Should throw exception");
    } catch (final Exception e) {
        assertTrue(e instanceof NullPointerException);
    }
    // registering listener on closed shard access should fail with IllegalStateEx
    final LeaderLocationListener listener2 = mock(LeaderLocationListener.class);
    shardAccess.close();
    try {
        shardAccess.registerLeaderLocationListener(listener2);
        fail("Should throw exception");
    } catch (final Exception ex) {
        assertTrue(ex instanceof IllegalStateException);
    }
}
Also used : LeaderLocationListener(org.opendaylight.controller.cluster.dom.api.LeaderLocationListener) LocalShardNotFoundException(org.opendaylight.controller.cluster.datastore.exceptions.LocalShardNotFoundException) LeadershipTransferFailedException(org.opendaylight.controller.cluster.raft.LeadershipTransferFailedException) AbstractActorTest(org.opendaylight.controller.cluster.datastore.AbstractActorTest) Test(org.junit.Test)

Example 2 with LeaderLocationListener

use of org.opendaylight.controller.cluster.dom.api.LeaderLocationListener in project controller by opendaylight.

the class RoleChangeListenerActorTest method testOnDataTreeChanged.

@Test
public void testOnDataTreeChanged() {
    final LeaderLocationListener listener = mock(LeaderLocationListener.class);
    doNothing().when(listener).onLeaderLocationChanged(any());
    final Props props = RoleChangeListenerActor.props(getSystem().deadLetters(), listener);
    final ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedChanged");
    subject.tell(new LeaderStateChanged("member-1", null, (short) 0), noSender());
    verify(listener, timeout(5000)).onLeaderLocationChanged(eq(LeaderLocation.UNKNOWN));
    subject.tell(new LeaderStateChanged("member-1", "member-1", (short) 0), noSender());
    verify(listener, timeout(5000)).onLeaderLocationChanged(eq(LeaderLocation.LOCAL));
    subject.tell(new LeaderStateChanged("member-1", "member-2", (short) 0), noSender());
    verify(listener, timeout(5000)).onLeaderLocationChanged(eq(LeaderLocation.REMOTE));
}
Also used : ActorRef(akka.actor.ActorRef) LeaderLocationListener(org.opendaylight.controller.cluster.dom.api.LeaderLocationListener) Props(akka.actor.Props) LeaderStateChanged(org.opendaylight.controller.cluster.notifications.LeaderStateChanged) AbstractActorTest(org.opendaylight.controller.cluster.datastore.AbstractActorTest) Test(org.junit.Test)

Example 3 with LeaderLocationListener

use of org.opendaylight.controller.cluster.dom.api.LeaderLocationListener in project controller by opendaylight.

the class CDSShardAccessImplTest method testOnLeaderLocationChanged.

@Test
@SuppressWarnings("checkstyle:IllegalCatch")
public void testOnLeaderLocationChanged() {
    final LeaderLocationListener listener1 = mock(LeaderLocationListener.class);
    doThrow(new RuntimeException("Failed")).when(listener1).onLeaderLocationChanged(any());
    final LeaderLocationListener listener2 = mock(LeaderLocationListener.class);
    doNothing().when(listener2).onLeaderLocationChanged(any());
    final LeaderLocationListener listener3 = mock(LeaderLocationListener.class);
    doNothing().when(listener3).onLeaderLocationChanged(any());
    final LeaderLocationListenerRegistration<?> reg1 = shardAccess.registerLeaderLocationListener(listener1);
    final LeaderLocationListenerRegistration<?> reg2 = shardAccess.registerLeaderLocationListener(listener2);
    final LeaderLocationListenerRegistration<?> reg3 = shardAccess.registerLeaderLocationListener(listener3);
    // Error in listener1 should not affect dispatching change to other listeners
    shardAccess.onLeaderLocationChanged(LeaderLocation.LOCAL);
    verify(listener1).onLeaderLocationChanged(eq(LeaderLocation.LOCAL));
    verify(listener2).onLeaderLocationChanged(eq(LeaderLocation.LOCAL));
    verify(listener3).onLeaderLocationChanged(eq(LeaderLocation.LOCAL));
    // Closed listeners shouldn't see new leader location changes
    reg1.close();
    reg2.close();
    shardAccess.onLeaderLocationChanged(LeaderLocation.REMOTE);
    verify(listener3).onLeaderLocationChanged(eq(LeaderLocation.REMOTE));
    verifyNoMoreInteractions(listener1);
    verifyNoMoreInteractions(listener2);
    // Closed shard access should not dispatch any new events
    shardAccess.close();
    shardAccess.onLeaderLocationChanged(LeaderLocation.UNKNOWN);
    verifyNoMoreInteractions(listener1);
    verifyNoMoreInteractions(listener2);
    verifyNoMoreInteractions(listener3);
    reg3.close();
}
Also used : LeaderLocationListener(org.opendaylight.controller.cluster.dom.api.LeaderLocationListener) AbstractActorTest(org.opendaylight.controller.cluster.datastore.AbstractActorTest) Test(org.junit.Test)

Example 4 with LeaderLocationListener

use of org.opendaylight.controller.cluster.dom.api.LeaderLocationListener in project controller by opendaylight.

the class RoleChangeListenerActorTest method testRegisterRoleChangeListenerOnStart.

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

        {
            final LeaderLocationListener listener = mock(LeaderLocationListener.class);
            final Props props = RoleChangeListenerActor.props(getRef(), listener);
            getSystem().actorOf(props, "testRegisterRoleChangeListenerOnStart");
            expectMsgClass(RegisterRoleChangeListener.class);
        }
    };
}
Also used : TestKit(akka.testkit.javadsl.TestKit) LeaderLocationListener(org.opendaylight.controller.cluster.dom.api.LeaderLocationListener) Props(akka.actor.Props) AbstractActorTest(org.opendaylight.controller.cluster.datastore.AbstractActorTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 AbstractActorTest (org.opendaylight.controller.cluster.datastore.AbstractActorTest)4 LeaderLocationListener (org.opendaylight.controller.cluster.dom.api.LeaderLocationListener)4 Props (akka.actor.Props)2 ActorRef (akka.actor.ActorRef)1 TestKit (akka.testkit.javadsl.TestKit)1 LocalShardNotFoundException (org.opendaylight.controller.cluster.datastore.exceptions.LocalShardNotFoundException)1 LeaderStateChanged (org.opendaylight.controller.cluster.notifications.LeaderStateChanged)1 LeadershipTransferFailedException (org.opendaylight.controller.cluster.raft.LeadershipTransferFailedException)1