Merge "Fixed for bug 1168 : Issue while update subnet"
[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.ActorSystem;
4 import akka.actor.Props;
5 import akka.testkit.JavaTestKit;
6 import akka.testkit.TestActorRef;
7 import junit.framework.Assert;
8 import org.junit.AfterClass;
9 import org.junit.BeforeClass;
10 import org.junit.Test;
11 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
12 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
13 import org.opendaylight.controller.cluster.datastore.messages.PrimaryNotFound;
14 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
15 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
16 import scala.concurrent.duration.Duration;
17
18 public class ShardManagerTest {
19     private static ActorSystem system;
20
21     @BeforeClass
22     public static void setUp() {
23         system = ActorSystem.create("test");
24     }
25
26     @AfterClass
27     public static void tearDown() {
28         JavaTestKit.shutdownActorSystem(system);
29         system = null;
30     }
31
32     @Test
33     public void testOnReceiveFindPrimaryForNonExistentShard() throws Exception {
34
35         new JavaTestKit(system) {{
36             final Props props = ShardManager
37                 .props("config", new MockClusterWrapper(),
38                     new MockConfiguration());
39             final TestActorRef<ShardManager> subject =
40                 TestActorRef.create(system, props);
41
42             new Within(duration("1 seconds")) {
43                 protected void run() {
44
45                     subject.tell(new FindPrimary("inventory").toSerializable(), getRef());
46
47                     expectMsgEquals(Duration.Zero(),
48                         new PrimaryNotFound("inventory").toSerializable());
49
50                     // Will wait for the rest of the 3 seconds
51                     expectNoMsg();
52                 }
53             };
54         }};
55     }
56
57     @Test
58     public void testOnReceiveFindPrimaryForExistentShard() throws Exception {
59
60         new JavaTestKit(system) {{
61             final Props props = ShardManager
62                 .props("config", new MockClusterWrapper(),
63                     new MockConfiguration());
64             final TestActorRef<ShardManager> subject =
65                 TestActorRef.create(system, props);
66
67             // the run() method needs to finish within 3 seconds
68             new Within(duration("1 seconds")) {
69                 protected void run() {
70
71                     subject.tell(new FindPrimary(Shard.DEFAULT_NAME).toSerializable(), getRef());
72
73                     expectMsgClass(PrimaryFound.SERIALIZABLE_CLASS);
74
75                     expectNoMsg();
76                 }
77             };
78         }};
79     }
80
81     @Test
82     public void testOnReceiveMemberUp() throws Exception {
83
84         new JavaTestKit(system) {{
85             final Props props = ShardManager
86                 .props("config", new MockClusterWrapper(),
87                     new MockConfiguration());
88             final TestActorRef<ShardManager> subject =
89                 TestActorRef.create(system, props);
90
91             // the run() method needs to finish within 3 seconds
92             new Within(duration("1 seconds")) {
93                 protected void run() {
94
95                     MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
96
97                     subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
98
99                     final String out = new ExpectMsg<String>("primary found") {
100                         // do not put code outside this method, will run afterwards
101                         protected String match(Object in) {
102                             if (in.getClass().equals(PrimaryFound.SERIALIZABLE_CLASS)) {
103                                 PrimaryFound f = PrimaryFound.fromSerializable(in);
104                                 return f.getPrimaryPath();
105                             } else {
106                                 throw noMatch();
107                             }
108                         }
109                     }.get(); // this extracts the received message
110
111                     Assert.assertTrue(out, out.contains("member-2-shard-astronauts-config"));
112
113                     expectNoMsg();
114                 }
115             };
116         }};
117     }
118
119     @Test
120     public void testOnReceiveMemberDown() throws Exception {
121
122         new JavaTestKit(system) {{
123             final Props props = ShardManager
124                 .props("config", new MockClusterWrapper(),
125                     new MockConfiguration());
126             final TestActorRef<ShardManager> subject =
127                 TestActorRef.create(system, props);
128
129             // the run() method needs to finish within 3 seconds
130             new Within(duration("1 seconds")) {
131                 protected void run() {
132
133                     MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
134
135                     subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
136
137                     expectMsgClass(PrimaryFound.SERIALIZABLE_CLASS);
138
139                     MockClusterWrapper.sendMemberRemoved(subject, "member-2", getRef().path().toString());
140
141                     subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
142
143                     expectMsgClass(PrimaryNotFound.SERIALIZABLE_CLASS);
144
145                     expectNoMsg();
146                 }
147             };
148         }};
149     }
150
151
152 }