Merge "Be sure to shutdown instance when destroyed"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardManagerTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import akka.actor.ActorRef;
4 import akka.actor.ActorSystem;
5 import akka.actor.Props;
6 import akka.testkit.JavaTestKit;
7 import akka.testkit.TestActorRef;
8 import junit.framework.Assert;
9 import org.junit.AfterClass;
10 import org.junit.BeforeClass;
11 import org.junit.Test;
12 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
13 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
14 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
15 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
16 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
17 import org.opendaylight.controller.cluster.datastore.messages.PrimaryNotFound;
18 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
19 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
20 import scala.concurrent.duration.Duration;
21
22 import static junit.framework.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24
25 public class ShardManagerTest {
26     private static ActorSystem system;
27
28     @BeforeClass
29     public static void setUp() {
30         system = ActorSystem.create("test");
31     }
32
33     @AfterClass
34     public static void tearDown() {
35         JavaTestKit.shutdownActorSystem(system);
36         system = null;
37     }
38
39     @Test
40     public void testOnReceiveFindPrimaryForNonExistentShard() throws Exception {
41
42         new JavaTestKit(system) {{
43             final Props props = ShardManager
44                 .props("config", new MockClusterWrapper(),
45                     new MockConfiguration(), new DatastoreContext());
46             final TestActorRef<ShardManager> subject =
47                 TestActorRef.create(system, props);
48
49             new Within(duration("10 seconds")) {
50                 @Override
51                 protected void run() {
52
53                     subject.tell(new FindPrimary("inventory").toSerializable(), getRef());
54
55                     expectMsgEquals(Duration.Zero(),
56                         new PrimaryNotFound("inventory").toSerializable());
57
58                     expectNoMsg();
59                 }
60             };
61         }};
62     }
63
64     @Test
65     public void testOnReceiveFindPrimaryForExistentShard() throws Exception {
66
67         new JavaTestKit(system) {{
68             final Props props = ShardManager
69                 .props("config", new MockClusterWrapper(),
70                     new MockConfiguration(), new DatastoreContext());
71             final TestActorRef<ShardManager> subject =
72                 TestActorRef.create(system, props);
73
74             new Within(duration("10 seconds")) {
75                 @Override
76                 protected void run() {
77
78                     subject.tell(new FindPrimary(Shard.DEFAULT_NAME).toSerializable(), getRef());
79
80                     expectMsgClass(duration("1 seconds"), PrimaryFound.SERIALIZABLE_CLASS);
81
82                     expectNoMsg();
83                 }
84             };
85         }};
86     }
87
88     @Test
89     public void testOnReceiveFindLocalShardForNonExistentShard() throws Exception {
90
91         new JavaTestKit(system) {{
92             final Props props = ShardManager
93                 .props("config", new MockClusterWrapper(),
94                     new MockConfiguration(), new DatastoreContext());
95             final TestActorRef<ShardManager> subject =
96                 TestActorRef.create(system, props);
97
98             new Within(duration("10 seconds")) {
99                 @Override
100                 protected void run() {
101
102                     subject.tell(new FindLocalShard("inventory"), getRef());
103
104                     final String out = new ExpectMsg<String>(duration("10 seconds"), "find local") {
105                         @Override
106                         protected String match(Object in) {
107                             if (in instanceof LocalShardNotFound) {
108                                 return ((LocalShardNotFound) in).getShardName();
109                             } else {
110                                 throw noMatch();
111                             }
112                         }
113                     }.get(); // this extracts the received message
114
115                     assertEquals("inventory", out);
116
117                     expectNoMsg();
118                 }
119             };
120         }};
121     }
122
123     @Test
124     public void testOnReceiveFindLocalShardForExistentShard() throws Exception {
125
126         final MockClusterWrapper mockClusterWrapper = new MockClusterWrapper();
127
128         new JavaTestKit(system) {{
129             final Props props = ShardManager
130                 .props("config", mockClusterWrapper,
131                     new MockConfiguration(), new DatastoreContext());
132             final TestActorRef<ShardManager> subject =
133                 TestActorRef.create(system, props);
134
135             new Within(duration("10 seconds")) {
136                 @Override
137                 protected void run() {
138
139                     subject.tell(new FindLocalShard(Shard.DEFAULT_NAME), getRef());
140
141                     final ActorRef out = new ExpectMsg<ActorRef>(duration("10 seconds"), "find local") {
142                         @Override
143                         protected ActorRef match(Object in) {
144                             if (in instanceof LocalShardFound) {
145                                 return ((LocalShardFound) in).getPath();
146                             } else {
147                                 throw noMatch();
148                             }
149                         }
150                     }.get(); // this extracts the received message
151
152                     assertTrue(out.path().toString(), out.path().toString().contains("member-1-shard-default-config"));
153
154
155                     expectNoMsg();
156                 }
157             };
158         }};
159     }
160
161     @Test
162     public void testOnReceiveMemberUp() throws Exception {
163
164         new JavaTestKit(system) {{
165             final Props props = ShardManager
166                 .props("config", new MockClusterWrapper(),
167                     new MockConfiguration(), new DatastoreContext());
168             final TestActorRef<ShardManager> subject =
169                 TestActorRef.create(system, props);
170
171             // the run() method needs to finish within 3 seconds
172             new Within(duration("10 seconds")) {
173                 @Override
174                 protected void run() {
175
176                     MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
177
178                     subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
179
180                     final String out = new ExpectMsg<String>(duration("1 seconds"), "primary found") {
181                         // do not put code outside this method, will run afterwards
182                         @Override
183                         protected String match(Object in) {
184                             if (in.getClass().equals(PrimaryFound.SERIALIZABLE_CLASS)) {
185                                 PrimaryFound f = PrimaryFound.fromSerializable(in);
186                                 return f.getPrimaryPath();
187                             } else {
188                                 throw noMatch();
189                             }
190                         }
191                     }.get(); // this extracts the received message
192
193                     Assert.assertTrue(out, out.contains("member-2-shard-astronauts-config"));
194
195                     expectNoMsg();
196                 }
197             };
198         }};
199     }
200
201     @Test
202     public void testOnReceiveMemberDown() throws Exception {
203
204         new JavaTestKit(system) {{
205             final Props props = ShardManager
206                 .props("config", new MockClusterWrapper(),
207                     new MockConfiguration(), new DatastoreContext());
208             final TestActorRef<ShardManager> subject =
209                 TestActorRef.create(system, props);
210
211             // the run() method needs to finish within 3 seconds
212             new Within(duration("10 seconds")) {
213                 @Override
214                 protected void run() {
215
216                     MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
217
218                     subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
219
220                     expectMsgClass(duration("1 seconds"), PrimaryFound.SERIALIZABLE_CLASS);
221
222                     MockClusterWrapper.sendMemberRemoved(subject, "member-2", getRef().path().toString());
223
224                     subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
225
226                     expectMsgClass(duration("1 seconds"), PrimaryNotFound.SERIALIZABLE_CLASS);
227
228                     expectNoMsg();
229                 }
230             };
231         }};
232     }
233
234
235 }