Serialization/Deserialization and a host of other fixes
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardManagerTest.java
index a2f19d8b2b6b2477a2a71749d6c48b766c459afe..87d257a3f217bf0bfac0b3c8ade1cbe8d4555b04 100644 (file)
@@ -4,6 +4,7 @@ import akka.actor.ActorSystem;
 import akka.actor.Props;
 import akka.testkit.JavaTestKit;
 import akka.testkit.TestActorRef;
+import junit.framework.Assert;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -18,12 +19,12 @@ public class ShardManagerTest {
     private static ActorSystem system;
 
     @BeforeClass
-    public static void setUp(){
+    public static void setUp() {
         system = ActorSystem.create("test");
     }
 
     @AfterClass
-    public static void tearDown(){
+    public static void tearDown() {
         JavaTestKit.shutdownActorSystem(system);
         system = null;
     }
@@ -32,15 +33,19 @@ public class ShardManagerTest {
     public void testOnReceiveFindPrimaryForNonExistentShard() throws Exception {
 
         new JavaTestKit(system) {{
-            final Props props = ShardManager.props("config", new MockClusterWrapper(), new MockConfiguration());
-            final TestActorRef<ShardManager> subject = TestActorRef.create(system, props);
+            final Props props = ShardManager
+                .props("config", new MockClusterWrapper(),
+                    new MockConfiguration());
+            final TestActorRef<ShardManager> subject =
+                TestActorRef.create(system, props);
 
             new Within(duration("1 seconds")) {
                 protected void run() {
 
-                    subject.tell(new FindPrimary("inventory"), getRef());
+                    subject.tell(new FindPrimary("inventory").toSerializable(), getRef());
 
-                    expectMsgEquals(Duration.Zero(), new PrimaryNotFound("inventory"));
+                    expectMsgEquals(Duration.Zero(),
+                        new PrimaryNotFound("inventory").toSerializable());
 
                     // Will wait for the rest of the 3 seconds
                     expectNoMsg();
@@ -49,24 +54,99 @@ public class ShardManagerTest {
         }};
     }
 
-  @Test
-  public void testOnReceiveFindPrimaryForExistentShard() throws Exception {
+    @Test
+    public void testOnReceiveFindPrimaryForExistentShard() throws Exception {
+
+        new JavaTestKit(system) {{
+            final Props props = ShardManager
+                .props("config", new MockClusterWrapper(),
+                    new MockConfiguration());
+            final TestActorRef<ShardManager> subject =
+                TestActorRef.create(system, props);
+
+            // the run() method needs to finish within 3 seconds
+            new Within(duration("1 seconds")) {
+                protected void run() {
+
+                    subject.tell(new FindPrimary(Shard.DEFAULT_NAME).toSerializable(), getRef());
+
+                    expectMsgClass(PrimaryFound.SERIALIZABLE_CLASS);
+
+                    expectNoMsg();
+                }
+            };
+        }};
+    }
+
+    @Test
+    public void testOnReceiveMemberUp() throws Exception {
+
+        new JavaTestKit(system) {{
+            final Props props = ShardManager
+                .props("config", new MockClusterWrapper(),
+                    new MockConfiguration());
+            final TestActorRef<ShardManager> subject =
+                TestActorRef.create(system, props);
 
-    new JavaTestKit(system) {{
-      final Props props = ShardManager.props("config", new MockClusterWrapper(), new MockConfiguration());
-      final TestActorRef<ShardManager> subject = TestActorRef.create(system, props);
+            // the run() method needs to finish within 3 seconds
+            new Within(duration("1 seconds")) {
+                protected void run() {
+
+                    MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
 
-      // the run() method needs to finish within 3 seconds
-      new Within(duration("1 seconds")) {
-        protected void run() {
+                    subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
 
-          subject.tell(new FindPrimary(Shard.DEFAULT_NAME), getRef());
+                    final String out = new ExpectMsg<String>("primary found") {
+                        // do not put code outside this method, will run afterwards
+                        protected String match(Object in) {
+                            if (in.getClass().equals(PrimaryFound.SERIALIZABLE_CLASS)) {
+                                PrimaryFound f = PrimaryFound.fromSerializable(in);
+                                return f.getPrimaryPath();
+                            } else {
+                                throw noMatch();
+                            }
+                        }
+                    }.get(); // this extracts the received message
+
+                    Assert.assertTrue(out, out.contains("member-2-shard-astronauts-config"));
+
+                    expectNoMsg();
+                }
+            };
+        }};
+    }
+
+    @Test
+    public void testOnReceiveMemberDown() throws Exception {
+
+        new JavaTestKit(system) {{
+            final Props props = ShardManager
+                .props("config", new MockClusterWrapper(),
+                    new MockConfiguration());
+            final TestActorRef<ShardManager> subject =
+                TestActorRef.create(system, props);
+
+            // the run() method needs to finish within 3 seconds
+            new Within(duration("1 seconds")) {
+                protected void run() {
+
+                    MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
+
+                    subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
+
+                    expectMsgClass(PrimaryFound.SERIALIZABLE_CLASS);
+
+                    MockClusterWrapper.sendMemberRemoved(subject, "member-2", getRef().path().toString());
+
+                    subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
+
+                    expectMsgClass(PrimaryNotFound.SERIALIZABLE_CLASS);
+
+                    expectNoMsg();
+                }
+            };
+        }};
+    }
 
-          expectMsgClass(PrimaryFound.class);
 
-          expectNoMsg();
-        }
-      };
-    }};
-  }
 }