1 package org.opendaylight.controller.cluster.datastore.utils;
3 import java.util.concurrent.TimeUnit;
4 import akka.actor.ActorRef;
5 import akka.actor.ActorSelection;
6 import akka.actor.ActorSystem;
7 import akka.actor.Props;
8 import akka.actor.UntypedActor;
9 import akka.japi.Creator;
10 import akka.testkit.JavaTestKit;
12 import org.junit.Test;
13 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
14 import org.opendaylight.controller.cluster.datastore.ClusterWrapper;
15 import org.opendaylight.controller.cluster.datastore.Configuration;
16 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
17 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
18 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
20 import scala.concurrent.Await;
21 import scala.concurrent.Future;
22 import scala.concurrent.duration.Duration;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import static org.mockito.Mockito.mock;
27 public class ActorContextTest extends AbstractActorTest{
29 public void testResolvePathForRemoteActor(){
30 ActorContext actorContext =
31 new ActorContext(mock(ActorSystem.class), mock(ActorRef.class),mock(
32 ClusterWrapper.class),
33 mock(Configuration.class));
35 String actual = actorContext.resolvePath(
36 "akka.tcp://system@127.0.0.1:2550/user/shardmanager/shard",
37 "akka://system/user/shardmanager/shard/transaction");
39 String expected = "akka.tcp://system@127.0.0.1:2550/user/shardmanager/shard/transaction";
41 assertEquals(expected, actual);
45 public void testResolvePathForLocalActor(){
46 ActorContext actorContext =
47 new ActorContext(getSystem(), mock(ActorRef.class), mock(ClusterWrapper.class),
48 mock(Configuration.class));
50 String actual = actorContext.resolvePath(
51 "akka://system/user/shardmanager/shard",
52 "akka://system/user/shardmanager/shard/transaction");
54 String expected = "akka://system/user/shardmanager/shard/transaction";
56 assertEquals(expected, actual);
58 System.out.println(actorContext
59 .actorFor("akka://system/user/shardmanager/shard/transaction"));
63 private static class MockShardManager extends UntypedActor {
65 private final boolean found;
66 private final ActorRef actorRef;
68 private MockShardManager(boolean found, ActorRef actorRef){
71 this.actorRef = actorRef;
74 @Override public void onReceive(Object message) throws Exception {
76 getSender().tell(new LocalShardFound(actorRef), getSelf());
78 getSender().tell(new LocalShardNotFound(((FindLocalShard) message).getShardName()), getSelf());
82 private static Props props(final boolean found, final ActorRef actorRef){
83 return Props.create(new MockShardManagerCreator(found, actorRef) );
86 @SuppressWarnings("serial")
87 private static class MockShardManagerCreator implements Creator<MockShardManager> {
89 final ActorRef actorRef;
91 MockShardManagerCreator(boolean found, ActorRef actorRef) {
93 this.actorRef = actorRef;
97 public MockShardManager create() throws Exception {
98 return new MockShardManager(found, actorRef);
104 public void testExecuteLocalShardOperationWithShardFound(){
105 new JavaTestKit(getSystem()) {{
107 new Within(duration("1 seconds")) {
109 protected void run() {
111 ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
113 ActorRef shardManagerActorRef = getSystem()
114 .actorOf(MockShardManager.props(true, shardActorRef));
116 ActorContext actorContext =
117 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
118 mock(Configuration.class));
120 Object out = actorContext.executeLocalShardOperation("default", "hello");
122 assertEquals("hello", out);
133 public void testExecuteLocalShardOperationWithShardNotFound(){
134 new JavaTestKit(getSystem()) {{
136 new Within(duration("1 seconds")) {
138 protected void run() {
140 ActorRef shardManagerActorRef = getSystem()
141 .actorOf(MockShardManager.props(false, null));
143 ActorContext actorContext =
144 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
145 mock(Configuration.class));
147 Object out = actorContext.executeLocalShardOperation("default", "hello");
161 public void testFindLocalShardWithShardFound(){
162 new JavaTestKit(getSystem()) {{
164 new Within(duration("1 seconds")) {
166 protected void run() {
168 ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
170 ActorRef shardManagerActorRef = getSystem()
171 .actorOf(MockShardManager.props(true, shardActorRef));
173 ActorContext actorContext =
174 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
175 mock(Configuration.class));
177 Object out = actorContext.findLocalShard("default");
179 assertEquals(shardActorRef, out);
190 public void testFindLocalShardWithShardNotFound(){
191 new JavaTestKit(getSystem()) {{
193 new Within(duration("1 seconds")) {
195 protected void run() {
197 ActorRef shardManagerActorRef = getSystem()
198 .actorOf(MockShardManager.props(false, null));
200 ActorContext actorContext =
201 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
202 mock(Configuration.class));
204 Object out = actorContext.findLocalShard("default");
217 public void testExecuteRemoteOperation() {
218 new JavaTestKit(getSystem()) {{
220 new Within(duration("3 seconds")) {
222 protected void run() {
224 ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
226 ActorRef shardManagerActorRef = getSystem()
227 .actorOf(MockShardManager.props(true, shardActorRef));
229 ActorContext actorContext =
230 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
231 mock(Configuration.class));
233 ActorSelection actor = actorContext.actorSelection(shardActorRef.path());
235 Object out = actorContext.executeRemoteOperation(actor, "hello");
237 assertEquals("hello", out);
246 public void testExecuteRemoteOperationAsync() {
247 new JavaTestKit(getSystem()) {{
249 new Within(duration("3 seconds")) {
251 protected void run() {
253 ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
255 ActorRef shardManagerActorRef = getSystem()
256 .actorOf(MockShardManager.props(true, shardActorRef));
258 ActorContext actorContext =
259 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
260 mock(Configuration.class));
262 ActorSelection actor = actorContext.actorSelection(shardActorRef.path());
264 Future<Object> future = actorContext.executeRemoteOperationAsync(actor, "hello");
267 Object result = Await.result(future, Duration.create(3, TimeUnit.SECONDS));
268 assertEquals("Result", "hello", result);
269 } catch(Exception e) {
270 throw new AssertionError(e);