268ed3c27383f3254eafec08e74c8f287456456a
[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());
46             final TestActorRef<ShardManager> subject =
47                 TestActorRef.create(system, props);
48
49             new Within(duration("1 seconds")) {
50                 protected void run() {
51
52                     subject.tell(new FindPrimary("inventory").toSerializable(), getRef());
53
54                     expectMsgEquals(Duration.Zero(),
55                         new PrimaryNotFound("inventory").toSerializable());
56
57                     expectNoMsg();
58                 }
59             };
60         }};
61     }
62
63     @Test
64     public void testOnReceiveFindPrimaryForExistentShard() throws Exception {
65
66         new JavaTestKit(system) {{
67             final Props props = ShardManager
68                 .props("config", new MockClusterWrapper(),
69                     new MockConfiguration());
70             final TestActorRef<ShardManager> subject =
71                 TestActorRef.create(system, props);
72
73             new Within(duration("1 seconds")) {
74                 protected void run() {
75
76                     subject.tell(new FindPrimary(Shard.DEFAULT_NAME).toSerializable(), getRef());
77
78                     expectMsgClass(PrimaryFound.SERIALIZABLE_CLASS);
79
80                     expectNoMsg();
81                 }
82             };
83         }};
84     }
85
86     @Test
87     public void testOnReceiveFindLocalShardForNonExistentShard() throws Exception {
88
89         new JavaTestKit(system) {{
90             final Props props = ShardManager
91                 .props("config", new MockClusterWrapper(),
92                     new MockConfiguration());
93             final TestActorRef<ShardManager> subject =
94                 TestActorRef.create(system, props);
95
96             new Within(duration("1 seconds")) {
97                 protected void run() {
98
99                     subject.tell(new FindLocalShard("inventory"), getRef());
100
101                     final String out = new ExpectMsg<String>(duration("1 seconds"), "find local") {
102                         protected String match(Object in) {
103                             if (in instanceof LocalShardNotFound) {
104                                 return ((LocalShardNotFound) in).getShardName();
105                             } else {
106                                 throw noMatch();
107                             }
108                         }
109                     }.get(); // this extracts the received message
110
111                     assertEquals("inventory", out);
112
113                     expectNoMsg();
114                 }
115             };
116         }};
117     }
118
119     @Test
120     public void testOnReceiveFindLocalShardForExistentShard() throws Exception {
121
122         final MockClusterWrapper mockClusterWrapper = new MockClusterWrapper();
123
124         new JavaTestKit(system) {{
125             final Props props = ShardManager
126                 .props("config", mockClusterWrapper,
127                     new MockConfiguration());
128             final TestActorRef<ShardManager> subject =
129                 TestActorRef.create(system, props);
130
131             new Within(duration("1 seconds")) {
132                 protected void run() {
133
134                     subject.tell(new FindLocalShard(Shard.DEFAULT_NAME), getRef());
135
136                     final ActorRef out = new ExpectMsg<ActorRef>(duration("1 seconds"), "find local") {
137                         protected ActorRef match(Object in) {
138                             if (in instanceof LocalShardFound) {
139                                 return ((LocalShardFound) in).getPath();
140                             } else {
141                                 throw noMatch();
142                             }
143                         }
144                     }.get(); // this extracts the received message
145
146                     assertTrue(out.path().toString(), out.path().toString().contains("member-1-shard-default-config"));
147
148
149                     expectNoMsg();
150                 }
151             };
152         }};
153     }
154
155     @Test
156     public void testOnReceiveMemberUp() throws Exception {
157
158         new JavaTestKit(system) {{
159             final Props props = ShardManager
160                 .props("config", new MockClusterWrapper(),
161                     new MockConfiguration());
162             final TestActorRef<ShardManager> subject =
163                 TestActorRef.create(system, props);
164
165             // the run() method needs to finish within 3 seconds
166             new Within(duration("1 seconds")) {
167                 protected void run() {
168
169                     MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
170
171                     subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
172
173                     final String out = new ExpectMsg<String>("primary found") {
174                         // do not put code outside this method, will run afterwards
175                         protected String match(Object in) {
176                             if (in.getClass().equals(PrimaryFound.SERIALIZABLE_CLASS)) {
177                                 PrimaryFound f = PrimaryFound.fromSerializable(in);
178                                 return f.getPrimaryPath();
179                             } else {
180                                 throw noMatch();
181                             }
182                         }
183                     }.get(); // this extracts the received message
184
185                     Assert.assertTrue(out, out.contains("member-2-shard-astronauts-config"));
186
187                     expectNoMsg();
188                 }
189             };
190         }};
191     }
192
193     @Test
194     public void testOnReceiveMemberDown() throws Exception {
195
196         new JavaTestKit(system) {{
197             final Props props = ShardManager
198                 .props("config", new MockClusterWrapper(),
199                     new MockConfiguration());
200             final TestActorRef<ShardManager> subject =
201                 TestActorRef.create(system, props);
202
203             // the run() method needs to finish within 3 seconds
204             new Within(duration("1 seconds")) {
205                 protected void run() {
206
207                     MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
208
209                     subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
210
211                     expectMsgClass(PrimaryFound.SERIALIZABLE_CLASS);
212
213                     MockClusterWrapper.sendMemberRemoved(subject, "member-2", getRef().path().toString());
214
215                     subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
216
217                     expectMsgClass(PrimaryNotFound.SERIALIZABLE_CLASS);
218
219                     expectNoMsg();
220                 }
221             };
222         }};
223     }
224
225
226 }