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.messages.UpdateSchemaContext;
19 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
20 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
21 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
22 import scala.concurrent.duration.Duration;
24 import static junit.framework.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
27 public class ShardManagerTest {
28 private static ActorSystem system;
31 public static void setUp() {
32 system = ActorSystem.create("test");
36 public static void tearDown() {
37 JavaTestKit.shutdownActorSystem(system);
42 public void testOnReceiveFindPrimaryForNonExistentShard() throws Exception {
44 new JavaTestKit(system) {{
45 final Props props = ShardManager
46 .props("config", new MockClusterWrapper(),
47 new MockConfiguration(), new DatastoreContext());
48 final TestActorRef<ShardManager> subject =
49 TestActorRef.create(system, props);
51 new Within(duration("10 seconds")) {
53 protected void run() {
55 subject.tell(new FindPrimary("inventory").toSerializable(), getRef());
57 expectMsgEquals(Duration.Zero(),
58 new PrimaryNotFound("inventory").toSerializable());
67 public void testOnReceiveFindPrimaryForExistentShard() throws Exception {
69 new JavaTestKit(system) {{
70 final Props props = ShardManager
71 .props("config", new MockClusterWrapper(),
72 new MockConfiguration(), new DatastoreContext());
73 final TestActorRef<ShardManager> subject =
74 TestActorRef.create(system, props);
76 subject.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
78 new Within(duration("10 seconds")) {
80 protected void run() {
82 subject.tell(new FindPrimary(Shard.DEFAULT_NAME).toSerializable(), getRef());
84 expectMsgClass(duration("1 seconds"), PrimaryFound.SERIALIZABLE_CLASS);
93 public void testOnReceiveFindLocalShardForNonExistentShard() throws Exception {
95 new JavaTestKit(system) {{
96 final Props props = ShardManager
97 .props("config", new MockClusterWrapper(),
98 new MockConfiguration(), new DatastoreContext());
99 final TestActorRef<ShardManager> subject =
100 TestActorRef.create(system, props);
102 new Within(duration("10 seconds")) {
104 protected void run() {
106 subject.tell(new FindLocalShard("inventory"), getRef());
108 final String out = new ExpectMsg<String>(duration("10 seconds"), "find local") {
110 protected String match(Object in) {
111 if (in instanceof LocalShardNotFound) {
112 return ((LocalShardNotFound) in).getShardName();
117 }.get(); // this extracts the received message
119 assertEquals("inventory", out);
128 public void testOnReceiveFindLocalShardForExistentShard() throws Exception {
130 final MockClusterWrapper mockClusterWrapper = new MockClusterWrapper();
132 new JavaTestKit(system) {{
133 final Props props = ShardManager
134 .props("config", mockClusterWrapper,
135 new MockConfiguration(), new DatastoreContext());
136 final TestActorRef<ShardManager> subject =
137 TestActorRef.create(system, props);
139 subject.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
141 new Within(duration("10 seconds")) {
143 protected void run() {
145 subject.tell(new FindLocalShard(Shard.DEFAULT_NAME), getRef());
147 final ActorRef out = new ExpectMsg<ActorRef>(duration("10 seconds"), "find local") {
149 protected ActorRef match(Object in) {
150 if (in instanceof LocalShardFound) {
151 return ((LocalShardFound) in).getPath();
156 }.get(); // this extracts the received message
158 assertTrue(out.path().toString(), out.path().toString().contains("member-1-shard-default-config"));
168 public void testOnReceiveMemberUp() throws Exception {
170 new JavaTestKit(system) {{
171 final Props props = ShardManager
172 .props("config", new MockClusterWrapper(),
173 new MockConfiguration(), new DatastoreContext());
174 final TestActorRef<ShardManager> subject =
175 TestActorRef.create(system, props);
177 // the run() method needs to finish within 3 seconds
178 new Within(duration("10 seconds")) {
180 protected void run() {
182 MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
184 subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
186 final String out = new ExpectMsg<String>(duration("1 seconds"), "primary found") {
187 // do not put code outside this method, will run afterwards
189 protected String match(Object in) {
190 if (in.getClass().equals(PrimaryFound.SERIALIZABLE_CLASS)) {
191 PrimaryFound f = PrimaryFound.fromSerializable(in);
192 return f.getPrimaryPath();
197 }.get(); // this extracts the received message
199 Assert.assertTrue(out, out.contains("member-2-shard-astronauts-config"));
208 public void testOnReceiveMemberDown() throws Exception {
210 new JavaTestKit(system) {{
211 final Props props = ShardManager
212 .props("config", new MockClusterWrapper(),
213 new MockConfiguration(), new DatastoreContext());
214 final TestActorRef<ShardManager> subject =
215 TestActorRef.create(system, props);
217 // the run() method needs to finish within 3 seconds
218 new Within(duration("10 seconds")) {
220 protected void run() {
222 MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
224 subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
226 expectMsgClass(duration("1 seconds"), PrimaryFound.SERIALIZABLE_CLASS);
228 MockClusterWrapper.sendMemberRemoved(subject, "member-2", getRef().path().toString());
230 subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
232 expectMsgClass(duration("1 seconds"), PrimaryNotFound.SERIALIZABLE_CLASS);