1 package org.opendaylight.controller.cluster.datastore;
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;
18 public class ShardManagerTest {
19 private static ActorSystem system;
22 public static void setUp() {
23 system = ActorSystem.create("test");
27 public static void tearDown() {
28 JavaTestKit.shutdownActorSystem(system);
33 public void testOnReceiveFindPrimaryForNonExistentShard() throws Exception {
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);
42 new Within(duration("1 seconds")) {
43 protected void run() {
45 subject.tell(new FindPrimary("inventory").toSerializable(), getRef());
47 expectMsgEquals(Duration.Zero(),
48 new PrimaryNotFound("inventory").toSerializable());
50 // Will wait for the rest of the 3 seconds
58 public void testOnReceiveFindPrimaryForExistentShard() throws Exception {
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);
67 // the run() method needs to finish within 3 seconds
68 new Within(duration("1 seconds")) {
69 protected void run() {
71 subject.tell(new FindPrimary(Shard.DEFAULT_NAME).toSerializable(), getRef());
73 expectMsgClass(PrimaryFound.SERIALIZABLE_CLASS);
82 public void testOnReceiveMemberUp() throws Exception {
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);
91 // the run() method needs to finish within 3 seconds
92 new Within(duration("1 seconds")) {
93 protected void run() {
95 MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
97 subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
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();
109 }.get(); // this extracts the received message
111 Assert.assertTrue(out, out.contains("member-2-shard-astronauts-config"));
120 public void testOnReceiveMemberDown() throws Exception {
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);
129 // the run() method needs to finish within 3 seconds
130 new Within(duration("1 seconds")) {
131 protected void run() {
133 MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
135 subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
137 expectMsgClass(PrimaryFound.SERIALIZABLE_CLASS);
139 MockClusterWrapper.sendMemberRemoved(subject, "member-2", getRef().path().toString());
141 subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
143 expectMsgClass(PrimaryNotFound.SERIALIZABLE_CLASS);