Merge "Custom mailbox that is bounded and instrumented."
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / utils / ActorContextTest.java
1 package org.opendaylight.controller.cluster.datastore.utils;
2
3 import akka.actor.ActorRef;
4 import akka.actor.ActorSystem;
5 import akka.actor.Props;
6 import akka.actor.UntypedActor;
7 import akka.japi.Creator;
8 import akka.testkit.JavaTestKit;
9 import org.junit.Test;
10 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
11 import org.opendaylight.controller.cluster.datastore.ClusterWrapper;
12 import org.opendaylight.controller.cluster.datastore.Configuration;
13 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
14 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
15 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
16
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertNull;
19 import static org.mockito.Mockito.mock;
20
21 public class ActorContextTest extends AbstractActorTest{
22     @Test
23     public void testResolvePathForRemoteActor(){
24         ActorContext actorContext =
25             new ActorContext(mock(ActorSystem.class), mock(ActorRef.class),mock(
26                 ClusterWrapper.class),
27                 mock(Configuration.class));
28
29         String actual = actorContext.resolvePath(
30             "akka.tcp://system@127.0.0.1:2550/user/shardmanager/shard",
31             "akka://system/user/shardmanager/shard/transaction");
32
33         String expected = "akka.tcp://system@127.0.0.1:2550/user/shardmanager/shard/transaction";
34
35         assertEquals(expected, actual);
36     }
37
38     @Test
39     public void testResolvePathForLocalActor(){
40         ActorContext actorContext =
41             new ActorContext(getSystem(), mock(ActorRef.class), mock(ClusterWrapper.class),
42                 mock(Configuration.class));
43
44         String actual = actorContext.resolvePath(
45             "akka://system/user/shardmanager/shard",
46             "akka://system/user/shardmanager/shard/transaction");
47
48         String expected = "akka://system/user/shardmanager/shard/transaction";
49
50         assertEquals(expected, actual);
51
52         System.out.println(actorContext
53             .actorFor("akka://system/user/shardmanager/shard/transaction"));
54     }
55
56
57     private static class MockShardManager extends UntypedActor {
58
59         private final boolean found;
60         private final ActorRef actorRef;
61
62         private MockShardManager(boolean found, ActorRef actorRef){
63
64             this.found = found;
65             this.actorRef = actorRef;
66         }
67
68         @Override public void onReceive(Object message) throws Exception {
69             if(found){
70                 getSender().tell(new LocalShardFound(actorRef), getSelf());
71             } else {
72                 getSender().tell(new LocalShardNotFound(((FindLocalShard) message).getShardName()), getSelf());
73             }
74         }
75
76         private static Props props(final boolean found, final ActorRef actorRef){
77             return Props.create(new Creator<MockShardManager>() {
78
79                 @Override public MockShardManager create()
80                     throws Exception {
81                     return new MockShardManager(found,
82                         actorRef);
83                 }
84             });
85         }
86     }
87
88     @Test
89     public void testExecuteLocalShardOperationWithShardFound(){
90         new JavaTestKit(getSystem()) {{
91
92             new Within(duration("1 seconds")) {
93                 protected void run() {
94
95                     ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
96
97                     ActorRef shardManagerActorRef = getSystem()
98                         .actorOf(MockShardManager.props(true, shardActorRef));
99
100                     ActorContext actorContext =
101                         new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
102                             mock(Configuration.class));
103
104                     Object out = actorContext.executeLocalShardOperation("default", "hello", duration("1 seconds"));
105
106                     assertEquals("hello", out);
107
108
109                     expectNoMsg();
110                 }
111             };
112         }};
113
114     }
115
116     @Test
117     public void testExecuteLocalShardOperationWithShardNotFound(){
118         new JavaTestKit(getSystem()) {{
119
120             new Within(duration("1 seconds")) {
121                 protected void run() {
122
123                     ActorRef shardManagerActorRef = getSystem()
124                         .actorOf(MockShardManager.props(false, null));
125
126                     ActorContext actorContext =
127                         new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
128                             mock(Configuration.class));
129
130                     Object out = actorContext.executeLocalShardOperation("default", "hello", duration("1 seconds"));
131
132                     assertNull(out);
133
134
135                     expectNoMsg();
136                 }
137             };
138         }};
139
140     }
141
142
143     @Test
144     public void testFindLocalShardWithShardFound(){
145         new JavaTestKit(getSystem()) {{
146
147             new Within(duration("1 seconds")) {
148                 protected void run() {
149
150                     ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
151
152                     ActorRef shardManagerActorRef = getSystem()
153                         .actorOf(MockShardManager.props(true, shardActorRef));
154
155                     ActorContext actorContext =
156                         new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
157                             mock(Configuration.class));
158
159                     Object out = actorContext.findLocalShard("default");
160
161                     assertEquals(shardActorRef, out);
162
163
164                     expectNoMsg();
165                 }
166             };
167         }};
168
169     }
170
171     @Test
172     public void testFindLocalShardWithShardNotFound(){
173         new JavaTestKit(getSystem()) {{
174
175             new Within(duration("1 seconds")) {
176                 protected void run() {
177
178                     ActorRef shardManagerActorRef = getSystem()
179                         .actorOf(MockShardManager.props(false, null));
180
181                     ActorContext actorContext =
182                         new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
183                             mock(Configuration.class));
184
185                     Object out = actorContext.findLocalShard("default");
186
187                     assertNull(out);
188
189
190                     expectNoMsg();
191                 }
192             };
193         }};
194
195     }
196 }