Merge "Implement finding a primary based on the shard name and do basic wiring of...
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardManagerTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import akka.actor.ActorSystem;
4 import akka.actor.Props;
5 import akka.testkit.JavaTestKit;
6 import akka.testkit.TestActorRef;
7 import org.junit.AfterClass;
8 import org.junit.BeforeClass;
9 import org.junit.Test;
10 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
11 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
12 import org.opendaylight.controller.cluster.datastore.messages.PrimaryNotFound;
13 import scala.concurrent.duration.Duration;
14
15 public class ShardManagerTest {
16     private static ActorSystem system;
17
18     @BeforeClass
19     public static void setUp(){
20         system = ActorSystem.create("test");
21     }
22
23     @AfterClass
24     public static void tearDown(){
25         JavaTestKit.shutdownActorSystem(system);
26         system = null;
27     }
28
29     @Test
30     public void testOnReceiveFindPrimaryForNonExistentShard() throws Exception {
31
32         new JavaTestKit(system) {{
33             final Props props = ShardManager.props("config");
34             final TestActorRef<ShardManager> subject = TestActorRef.create(system, props);
35
36             new Within(duration("1 seconds")) {
37                 protected void run() {
38
39                     subject.tell(new FindPrimary("inventory"), getRef());
40
41                     expectMsgEquals(Duration.Zero(), new PrimaryNotFound("inventory"));
42
43                     // Will wait for the rest of the 3 seconds
44                     expectNoMsg();
45                 }
46             };
47         }};
48     }
49
50   @Test
51   public void testOnReceiveFindPrimaryForExistentShard() throws Exception {
52
53     new JavaTestKit(system) {{
54       final Props props = ShardManager.props("config");
55       final TestActorRef<ShardManager> subject = TestActorRef.create(system, props);
56
57       // the run() method needs to finish within 3 seconds
58       new Within(duration("1 seconds")) {
59         protected void run() {
60
61           subject.tell(new FindPrimary(Shard.DEFAULT_NAME), getRef());
62
63           expectMsgClass(PrimaryFound.class);
64
65           expectNoMsg();
66         }
67       };
68     }};
69   }
70 }