1 package org.opendaylight.controller.cluster.datastore.utils;
3 import akka.actor.ActorRef;
4 import akka.actor.ActorSelection;
5 import akka.actor.Props;
6 import akka.actor.UntypedActor;
7 import akka.japi.Creator;
8 import akka.testkit.JavaTestKit;
9 import com.google.common.base.Optional;
10 import org.junit.Test;
11 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
12 import org.opendaylight.controller.cluster.datastore.ClusterWrapper;
13 import org.opendaylight.controller.cluster.datastore.Configuration;
14 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
15 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
16 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
17 import scala.concurrent.Await;
18 import scala.concurrent.Future;
19 import scala.concurrent.duration.Duration;
20 import java.util.concurrent.TimeUnit;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23 import static org.mockito.Mockito.mock;
25 public class ActorContextTest extends AbstractActorTest{
27 private static class MockShardManager extends UntypedActor {
29 private final boolean found;
30 private final ActorRef actorRef;
32 private MockShardManager(boolean found, ActorRef actorRef){
35 this.actorRef = actorRef;
38 @Override public void onReceive(Object message) throws Exception {
40 getSender().tell(new LocalShardFound(actorRef), getSelf());
42 getSender().tell(new LocalShardNotFound(((FindLocalShard) message).getShardName()), getSelf());
46 private static Props props(final boolean found, final ActorRef actorRef){
47 return Props.create(new MockShardManagerCreator(found, actorRef) );
50 @SuppressWarnings("serial")
51 private static class MockShardManagerCreator implements Creator<MockShardManager> {
53 final ActorRef actorRef;
55 MockShardManagerCreator(boolean found, ActorRef actorRef) {
57 this.actorRef = actorRef;
61 public MockShardManager create() throws Exception {
62 return new MockShardManager(found, actorRef);
68 public void testFindLocalShardWithShardFound(){
69 new JavaTestKit(getSystem()) {{
71 new Within(duration("1 seconds")) {
73 protected void run() {
75 ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
77 ActorRef shardManagerActorRef = getSystem()
78 .actorOf(MockShardManager.props(true, shardActorRef));
80 ActorContext actorContext =
81 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
82 mock(Configuration.class));
84 Optional<ActorRef> out = actorContext.findLocalShard("default");
86 assertEquals(shardActorRef, out.get());
97 public void testFindLocalShardWithShardNotFound(){
98 new JavaTestKit(getSystem()) {{
99 ActorRef shardManagerActorRef = getSystem()
100 .actorOf(MockShardManager.props(false, null));
102 ActorContext actorContext =
103 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
104 mock(Configuration.class));
106 Optional<ActorRef> out = actorContext.findLocalShard("default");
107 assertTrue(!out.isPresent());
113 public void testExecuteRemoteOperation() {
114 new JavaTestKit(getSystem()) {{
115 ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
117 ActorRef shardManagerActorRef = getSystem()
118 .actorOf(MockShardManager.props(true, shardActorRef));
120 ActorContext actorContext =
121 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
122 mock(Configuration.class));
124 ActorSelection actor = actorContext.actorSelection(shardActorRef.path());
126 Object out = actorContext.executeOperation(actor, "hello");
128 assertEquals("hello", out);
133 public void testExecuteRemoteOperationAsync() {
134 new JavaTestKit(getSystem()) {{
135 ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
137 ActorRef shardManagerActorRef = getSystem()
138 .actorOf(MockShardManager.props(true, shardActorRef));
140 ActorContext actorContext =
141 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
142 mock(Configuration.class));
144 ActorSelection actor = actorContext.actorSelection(shardActorRef.path());
146 Future<Object> future = actorContext.executeOperationAsync(actor, "hello");
149 Object result = Await.result(future, Duration.create(3, TimeUnit.SECONDS));
150 assertEquals("Result", "hello", result);
151 } catch(Exception e) {
152 throw new AssertionError(e);
158 public void testIsLocalPath() {
159 MockClusterWrapper clusterWrapper = new MockClusterWrapper();
160 ActorContext actorContext =
161 new ActorContext(getSystem(), null, clusterWrapper, mock(Configuration.class));
163 clusterWrapper.setSelfAddress("");
164 assertEquals(false, actorContext.isLocalPath(null));
165 assertEquals(false, actorContext.isLocalPath(""));
167 clusterWrapper.setSelfAddress(null);
168 assertEquals(false, actorContext.isLocalPath(""));
170 clusterWrapper.setSelfAddress("akka://test/user/$b");
171 assertEquals(false, actorContext.isLocalPath("akka://test/user/$a"));
173 clusterWrapper.setSelfAddress("akka.tcp://system@127.0.0.1:2550/");
174 assertEquals(true, actorContext.isLocalPath("akka.tcp://system@127.0.0.1:2550/"));
176 clusterWrapper.setSelfAddress("akka.tcp://system@127.0.0.1:2550");
177 assertEquals(false, actorContext.isLocalPath("akka.tcp://system@127.0.0.1:2550/"));
179 clusterWrapper.setSelfAddress("akka.tcp://system@128.0.0.1:2550/");
180 assertEquals(false, actorContext.isLocalPath("akka.tcp://system@127.0.0.1:2550/"));
182 clusterWrapper.setSelfAddress("akka.tcp://system@127.0.0.1:2551/");
183 assertEquals(false, actorContext.isLocalPath("akka.tcp://system@127.0.0.1:2550/"));