Initial support for multiple-shards
[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 org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
14 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
15 import scala.concurrent.duration.Duration;
16
17 public class ShardManagerTest {
18     private static ActorSystem system;
19
20     @BeforeClass
21     public static void setUp(){
22         system = ActorSystem.create("test");
23     }
24
25     @AfterClass
26     public static void tearDown(){
27         JavaTestKit.shutdownActorSystem(system);
28         system = null;
29     }
30
31     @Test
32     public void testOnReceiveFindPrimaryForNonExistentShard() throws Exception {
33
34         new JavaTestKit(system) {{
35             final Props props = ShardManager.props("config", new MockClusterWrapper(), new MockConfiguration());
36             final TestActorRef<ShardManager> subject = TestActorRef.create(system, props);
37
38             new Within(duration("1 seconds")) {
39                 protected void run() {
40
41                     subject.tell(new FindPrimary("inventory"), getRef());
42
43                     expectMsgEquals(Duration.Zero(), new PrimaryNotFound("inventory"));
44
45                     // Will wait for the rest of the 3 seconds
46                     expectNoMsg();
47                 }
48             };
49         }};
50     }
51
52   @Test
53   public void testOnReceiveFindPrimaryForExistentShard() throws Exception {
54
55     new JavaTestKit(system) {{
56       final Props props = ShardManager.props("config", new MockClusterWrapper(), new MockConfiguration());
57       final TestActorRef<ShardManager> subject = TestActorRef.create(system, props);
58
59       // the run() method needs to finish within 3 seconds
60       new Within(duration("1 seconds")) {
61         protected void run() {
62
63           subject.tell(new FindPrimary(Shard.DEFAULT_NAME), getRef());
64
65           expectMsgClass(PrimaryFound.class);
66
67           expectNoMsg();
68         }
69       };
70     }};
71   }
72 }