1 package org.opendaylight.controller.cluster.datastore;
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;
22 import static junit.framework.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
25 public class ShardManagerTest {
26 private static ActorSystem system;
29 public static void setUp() {
30 system = ActorSystem.create("test");
34 public static void tearDown() {
35 JavaTestKit.shutdownActorSystem(system);
40 public void testOnReceiveFindPrimaryForNonExistentShard() throws Exception {
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);
49 new Within(duration("1 seconds")) {
50 protected void run() {
52 subject.tell(new FindPrimary("inventory").toSerializable(), getRef());
54 expectMsgEquals(Duration.Zero(),
55 new PrimaryNotFound("inventory").toSerializable());
64 public void testOnReceiveFindPrimaryForExistentShard() throws Exception {
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);
73 new Within(duration("1 seconds")) {
74 protected void run() {
76 subject.tell(new FindPrimary(Shard.DEFAULT_NAME).toSerializable(), getRef());
78 expectMsgClass(duration("1 seconds"), PrimaryFound.SERIALIZABLE_CLASS);
87 public void testOnReceiveFindLocalShardForNonExistentShard() throws Exception {
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);
96 new Within(duration("1 seconds")) {
97 protected void run() {
99 subject.tell(new FindLocalShard("inventory"), getRef());
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();
109 }.get(); // this extracts the received message
111 assertEquals("inventory", out);
120 public void testOnReceiveFindLocalShardForExistentShard() throws Exception {
122 final MockClusterWrapper mockClusterWrapper = new MockClusterWrapper();
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);
131 new Within(duration("1 seconds")) {
132 protected void run() {
134 subject.tell(new FindLocalShard(Shard.DEFAULT_NAME), getRef());
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();
144 }.get(); // this extracts the received message
146 assertTrue(out.path().toString(), out.path().toString().contains("member-1-shard-default-config"));
156 public void testOnReceiveMemberUp() throws Exception {
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);
165 // the run() method needs to finish within 3 seconds
166 new Within(duration("1 seconds")) {
167 protected void run() {
169 MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
171 subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
173 final String out = new ExpectMsg<String>(duration("1 seconds"), "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();
183 }.get(); // this extracts the received message
185 Assert.assertTrue(out, out.contains("member-2-shard-astronauts-config"));
194 public void testOnReceiveMemberDown() throws Exception {
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);
203 // the run() method needs to finish within 3 seconds
204 new Within(duration("1 seconds")) {
205 protected void run() {
207 MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
209 subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
211 expectMsgClass(duration("1 seconds"), PrimaryFound.SERIALIZABLE_CLASS);
213 MockClusterWrapper.sendMemberRemoved(subject, "member-2", getRef().path().toString());
215 subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
217 expectMsgClass(duration("1 seconds"), PrimaryNotFound.SERIALIZABLE_CLASS);