fc6445e8f29dee2639d77727636d72844138f837
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / AbstractShardManagerTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.MockitoAnnotations.initMocks;
13
14 import akka.actor.ActorRef;
15 import akka.actor.ActorSystem;
16 import akka.actor.PoisonPill;
17 import akka.actor.Props;
18 import akka.testkit.JavaTestKit;
19 import akka.testkit.TestActorRef;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.concurrent.CountDownLatch;
23 import java.util.concurrent.TimeUnit;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.mockito.Mock;
27 import org.opendaylight.controller.cluster.access.concepts.MemberName;
28 import org.opendaylight.controller.cluster.datastore.config.Configuration;
29 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
30 import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManagerTest.TestShardManager;
31 import org.opendaylight.controller.cluster.raft.TestActorFactory;
32 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
33 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
34 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
35
36 public class AbstractShardManagerTest extends AbstractClusterRefActorTest {
37
38     protected static final MemberName MEMBER_1 = MemberName.forName("member-1");
39
40     protected static int ID_COUNTER = 1;
41     protected static TestActorRef<MessageCollectorActor> mockShardActor;
42     protected static ShardIdentifier mockShardName;
43
44     protected final String shardMrgIDSuffix = "config" + ID_COUNTER++;
45     protected final TestActorFactory actorFactory = new TestActorFactory(getSystem());
46     protected final Collection<ActorSystem> actorSystems = new ArrayList<>();
47     protected final DatastoreContext.Builder datastoreContextBuilder = DatastoreContext.newBuilder()
48             .dataStoreName(shardMrgIDSuffix).shardInitializationTimeout(600, TimeUnit.MILLISECONDS)
49             .shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(6);
50
51     @Mock
52     protected static CountDownLatch ready;
53
54     protected TestShardManager.Builder newTestShardMgrBuilder() {
55         return TestShardManager.builder(datastoreContextBuilder).distributedDataStore(mock(DistributedDataStore.class));
56     }
57
58     protected TestShardManager.Builder newTestShardMgrBuilder(final Configuration config) {
59         return TestShardManager.builder(datastoreContextBuilder).configuration(config)
60                 .distributedDataStore(mock(DistributedDataStore.class));
61     }
62
63     protected Props newShardMgrProps(final Configuration config) {
64         return newTestShardMgrBuilder(config).waitTillReadyCountDownLatch(ready).props();
65     }
66
67     @Before
68     public void setUp() throws Exception {
69         initMocks(this);
70
71         InMemoryJournal.clear();
72         InMemorySnapshotStore.clear();
73
74         if (mockShardActor == null) {
75             mockShardName = ShardIdentifier.create(Shard.DEFAULT_NAME, MEMBER_1, "config");
76             mockShardActor = TestActorRef.create(getSystem(), Props.create(MessageCollectorActor.class),
77                     mockShardName.toString());
78         }
79
80         mockShardActor.underlyingActor().clear();
81     }
82
83     @After
84     public void tearDown() {
85         InMemoryJournal.clear();
86         InMemorySnapshotStore.clear();
87
88         for (final ActorSystem system : actorSystems) {
89             JavaTestKit.shutdownActorSystem(system, null, Boolean.TRUE);
90         }
91
92         mockShardActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
93         mockShardActor = null;
94
95         actorFactory.close();
96     }
97 }