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(), new ShardContext());
46 final TestActorRef<ShardManager> subject =
47 TestActorRef.create(system, props);
49 new Within(duration("1 seconds")) {
51 protected void run() {
53 subject.tell(new FindPrimary("inventory").toSerializable(), getRef());
55 expectMsgEquals(Duration.Zero(),
56 new PrimaryNotFound("inventory").toSerializable());
65 public void testOnReceiveFindPrimaryForExistentShard() throws Exception {
67 new JavaTestKit(system) {{
68 final Props props = ShardManager
69 .props("config", new MockClusterWrapper(),
70 new MockConfiguration(), new ShardContext());
71 final TestActorRef<ShardManager> subject =
72 TestActorRef.create(system, props);
74 new Within(duration("1 seconds")) {
76 protected void run() {
78 subject.tell(new FindPrimary(Shard.DEFAULT_NAME).toSerializable(), getRef());
80 expectMsgClass(duration("1 seconds"), PrimaryFound.SERIALIZABLE_CLASS);
89 public void testOnReceiveFindLocalShardForNonExistentShard() throws Exception {
91 new JavaTestKit(system) {{
92 final Props props = ShardManager
93 .props("config", new MockClusterWrapper(),
94 new MockConfiguration(), new ShardContext());
95 final TestActorRef<ShardManager> subject =
96 TestActorRef.create(system, props);
98 new Within(duration("1 seconds")) {
100 protected void run() {
102 subject.tell(new FindLocalShard("inventory"), getRef());
104 final String out = new ExpectMsg<String>(duration("1 seconds"), "find local") {
106 protected String match(Object in) {
107 if (in instanceof LocalShardNotFound) {
108 return ((LocalShardNotFound) in).getShardName();
113 }.get(); // this extracts the received message
115 assertEquals("inventory", out);
124 public void testOnReceiveFindLocalShardForExistentShard() throws Exception {
126 final MockClusterWrapper mockClusterWrapper = new MockClusterWrapper();
128 new JavaTestKit(system) {{
129 final Props props = ShardManager
130 .props("config", mockClusterWrapper,
131 new MockConfiguration(), new ShardContext());
132 final TestActorRef<ShardManager> subject =
133 TestActorRef.create(system, props);
135 new Within(duration("1 seconds")) {
137 protected void run() {
139 subject.tell(new FindLocalShard(Shard.DEFAULT_NAME), getRef());
141 final ActorRef out = new ExpectMsg<ActorRef>(duration("1 seconds"), "find local") {
143 protected ActorRef match(Object in) {
144 if (in instanceof LocalShardFound) {
145 return ((LocalShardFound) in).getPath();
150 }.get(); // this extracts the received message
152 assertTrue(out.path().toString(), out.path().toString().contains("member-1-shard-default-config"));
162 public void testOnReceiveMemberUp() throws Exception {
164 new JavaTestKit(system) {{
165 final Props props = ShardManager
166 .props("config", new MockClusterWrapper(),
167 new MockConfiguration(), new ShardContext());
168 final TestActorRef<ShardManager> subject =
169 TestActorRef.create(system, props);
171 // the run() method needs to finish within 3 seconds
172 new Within(duration("1 seconds")) {
174 protected void run() {
176 MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
178 subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
180 final String out = new ExpectMsg<String>(duration("1 seconds"), "primary found") {
181 // do not put code outside this method, will run afterwards
183 protected String match(Object in) {
184 if (in.getClass().equals(PrimaryFound.SERIALIZABLE_CLASS)) {
185 PrimaryFound f = PrimaryFound.fromSerializable(in);
186 return f.getPrimaryPath();
191 }.get(); // this extracts the received message
193 Assert.assertTrue(out, out.contains("member-2-shard-astronauts-config"));
202 public void testOnReceiveMemberDown() throws Exception {
204 new JavaTestKit(system) {{
205 final Props props = ShardManager
206 .props("config", new MockClusterWrapper(),
207 new MockConfiguration(), new ShardContext());
208 final TestActorRef<ShardManager> subject =
209 TestActorRef.create(system, props);
211 // the run() method needs to finish within 3 seconds
212 new Within(duration("1 seconds")) {
214 protected void run() {
216 MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
218 subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
220 expectMsgClass(duration("1 seconds"), PrimaryFound.SERIALIZABLE_CLASS);
222 MockClusterWrapper.sendMemberRemoved(subject, "member-2", getRef().path().toString());
224 subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
226 expectMsgClass(duration("1 seconds"), PrimaryNotFound.SERIALIZABLE_CLASS);