X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Futils%2FActorContextTest.java;h=0698f011c6aea98d237429914b8f721f1f27cb26;hb=refs%2Fchanges%2F68%2F42668%2F2;hp=7e484ebde3d79bcadbb182162c55c23c56675d78;hpb=c2d1b9207fe82d36db83501e1baaffe7bc7da9ae;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/ActorContextTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/ActorContextTest.java index 7e484ebde3..0698f011c6 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/ActorContextTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/ActorContextTest.java @@ -31,11 +31,11 @@ import akka.util.Timeout; import com.google.common.base.Optional; import com.google.common.collect.Maps; import com.google.common.collect.Sets; -import com.google.common.util.concurrent.Uninterruptibles; import com.typesafe.config.ConfigFactory; import java.util.Arrays; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.function.Function; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; @@ -55,6 +55,9 @@ import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound; import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound; import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo; import org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound; +import org.opendaylight.controller.cluster.raft.utils.EchoActor; +import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -76,13 +79,13 @@ public class ActorContextTest extends AbstractActorTest{ private final ActorRef actorRef; private final Map findPrimaryResponses = Maps.newHashMap(); - private MockShardManager(boolean found, ActorRef actorRef){ + private MockShardManager(final boolean found, final ActorRef actorRef){ this.found = found; this.actorRef = actorRef; } - @Override public void onReceive(Object message) throws Exception { + @Override public void onReceive(final Object message) throws Exception { if(message instanceof FindPrimary) { FindPrimary fp = (FindPrimary)message; Object resp = findPrimaryResponses.get(fp.getShardName()); @@ -102,7 +105,7 @@ public class ActorContextTest extends AbstractActorTest{ } } - void addFindPrimaryResp(String shardName, Object resp) { + void addFindPrimaryResp(final String shardName, final Object resp) { findPrimaryResponses.put(shardName, resp); } @@ -124,7 +127,7 @@ public class ActorContextTest extends AbstractActorTest{ this.actorRef = null; } - MockShardManagerCreator(boolean found, ActorRef actorRef) { + MockShardManagerCreator(final boolean found, final ActorRef actorRef) { this.found = found; this.actorRef = actorRef; } @@ -290,53 +293,6 @@ public class ActorContextTest extends AbstractActorTest{ assertEquals(false, actorContext.isPathLocal("akka.tcp://system@127.0.0.1:2551/")); } - @Test - public void testResolvePathForRemoteActor() { - ActorContext actorContext = - new ActorContext(getSystem(), mock(ActorRef.class), mock( - ClusterWrapper.class), - mock(Configuration.class)); - - String actual = actorContext.resolvePath( - "akka.tcp://system@127.0.0.1:2550/user/shardmanager/shard", - "akka://system/user/shardmanager/shard/transaction"); - - String expected = "akka.tcp://system@127.0.0.1:2550/user/shardmanager/shard/transaction"; - - assertEquals(expected, actual); - } - - @Test - public void testResolvePathForLocalActor() { - ActorContext actorContext = - new ActorContext(getSystem(), mock(ActorRef.class), mock(ClusterWrapper.class), - mock(Configuration.class)); - - String actual = actorContext.resolvePath( - "akka://system/user/shardmanager/shard", - "akka://system/user/shardmanager/shard/transaction"); - - String expected = "akka://system/user/shardmanager/shard/transaction"; - - assertEquals(expected, actual); - } - - @Test - public void testResolvePathForRemoteActorWithProperRemoteAddress() { - ActorContext actorContext = - new ActorContext(getSystem(), mock(ActorRef.class), mock(ClusterWrapper.class), - mock(Configuration.class)); - - String actual = actorContext.resolvePath( - "akka.tcp://system@7.0.0.1:2550/user/shardmanager/shard", - "akka.tcp://system@7.0.0.1:2550/user/shardmanager/shard/transaction"); - - String expected = "akka.tcp://system@7.0.0.1:2550/user/shardmanager/shard/transaction"; - - assertEquals(expected, actual); - } - - @Test public void testClientDispatcherIsGlobalDispatcher(){ ActorContext actorContext = @@ -344,7 +300,6 @@ public class ActorContextTest extends AbstractActorTest{ mock(Configuration.class), DatastoreContext.newBuilder().build(), new PrimaryShardInfoFutureCache()); assertEquals(getSystem().dispatchers().defaultGlobalDispatcher(), actorContext.getClientDispatcher()); - } @Test @@ -357,8 +312,7 @@ public class ActorContextTest extends AbstractActorTest{ assertNotEquals(actorSystem.dispatchers().defaultGlobalDispatcher(), actorContext.getClientDispatcher()); - actorSystem.shutdown(); - + actorSystem.terminate(); } @Test @@ -396,7 +350,8 @@ public class ActorContextTest extends AbstractActorTest{ TestActorRef shardManager = TestActorRef.create(getSystem(), Props.create(MessageCollectorActor.class)); - DatastoreContext dataStoreContext = DatastoreContext.newBuilder().dataStoreType("config"). + DatastoreContext dataStoreContext = DatastoreContext.newBuilder(). + logicalStoreType(LogicalDatastoreType.CONFIGURATION). shardLeaderElectionTimeout(100, TimeUnit.MILLISECONDS).build(); final String expPrimaryPath = "akka://test-system/find-primary-shard"; @@ -405,7 +360,7 @@ public class ActorContextTest extends AbstractActorTest{ new ActorContext(getSystem(), shardManager, mock(ClusterWrapper.class), mock(Configuration.class), dataStoreContext, new PrimaryShardInfoFutureCache()) { @Override - protected Future doAsk(ActorRef actorRef, Object message, Timeout timeout) { + protected Future doAsk(final ActorRef actorRef, final Object message, final Timeout timeout) { return Futures.successful((Object) new RemotePrimaryShardFound(expPrimaryPath, expPrimaryVersion)); } }; @@ -438,7 +393,8 @@ public class ActorContextTest extends AbstractActorTest{ TestActorRef shardManager = TestActorRef.create(getSystem(), Props.create(MessageCollectorActor.class)); - DatastoreContext dataStoreContext = DatastoreContext.newBuilder().dataStoreType("config"). + DatastoreContext dataStoreContext = DatastoreContext.newBuilder(). + logicalStoreType(LogicalDatastoreType.CONFIGURATION). shardLeaderElectionTimeout(100, TimeUnit.MILLISECONDS).build(); final DataTree mockDataTree = Mockito.mock(DataTree.class); @@ -447,7 +403,7 @@ public class ActorContextTest extends AbstractActorTest{ new ActorContext(getSystem(), shardManager, mock(ClusterWrapper.class), mock(Configuration.class), dataStoreContext, new PrimaryShardInfoFutureCache()) { @Override - protected Future doAsk(ActorRef actorRef, Object message, Timeout timeout) { + protected Future doAsk(final ActorRef actorRef, final Object message, final Timeout timeout) { return Futures.successful((Object) new LocalPrimaryShardFound(expPrimaryPath, mockDataTree)); } }; @@ -485,18 +441,19 @@ public class ActorContextTest extends AbstractActorTest{ testFindPrimaryExceptions(new NotInitializedException("not initialized")); } - private void testFindPrimaryExceptions(final Object expectedException) throws Exception { + private static void testFindPrimaryExceptions(final Object expectedException) throws Exception { TestActorRef shardManager = TestActorRef.create(getSystem(), Props.create(MessageCollectorActor.class)); - DatastoreContext dataStoreContext = DatastoreContext.newBuilder().dataStoreType("config"). + DatastoreContext dataStoreContext = DatastoreContext.newBuilder(). + logicalStoreType(LogicalDatastoreType.CONFIGURATION). shardLeaderElectionTimeout(100, TimeUnit.MILLISECONDS).build(); ActorContext actorContext = new ActorContext(getSystem(), shardManager, mock(ClusterWrapper.class), mock(Configuration.class), dataStoreContext, new PrimaryShardInfoFutureCache()) { @Override - protected Future doAsk(ActorRef actorRef, Object message, Timeout timeout) { + protected Future doAsk(final ActorRef actorRef, final Object message, final Timeout timeout) { return Futures.successful(expectedException); } }; @@ -539,28 +496,16 @@ public class ActorContextTest extends AbstractActorTest{ mock(ClusterWrapper.class), mockConfig, DatastoreContext.newBuilder().shardInitializationTimeout(200, TimeUnit.MILLISECONDS).build(), new PrimaryShardInfoFutureCache()); - actorContext.broadcast(new TestMessage()); + actorContext.broadcast(new Function() { + @Override + public Object apply(final Short v) { + return new TestMessage(); + } + }, TestMessage.class); - expectFirstMatching(shardActorRef1, TestMessage.class); - expectFirstMatching(shardActorRef2, TestMessage.class); + MessageCollectorActor.expectFirstMatching(shardActorRef1, TestMessage.class); + MessageCollectorActor.expectFirstMatching(shardActorRef2, TestMessage.class); }}; } - private static T expectFirstMatching(ActorRef actor, Class clazz) { - int count = 5000 / 50; - for(int i = 0; i < count; i++) { - try { - @SuppressWarnings("unchecked") - T message = (T) MessageCollectorActor.getFirstMatching(actor, clazz); - if(message != null) { - return message; - } - } catch (Exception e) {} - - Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS); - } - - Assert.fail("Did not receive message of type " + clazz); - return null; - } }