15e6b0364509f8521b58f2248ec2ee9eed97a410
[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.MockitoAnnotations.initMocks;
12
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSystem;
15 import akka.actor.PoisonPill;
16 import akka.actor.Props;
17 import akka.testkit.JavaTestKit;
18 import akka.testkit.TestActorRef;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.concurrent.CountDownLatch;
22 import java.util.concurrent.TimeUnit;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.mockito.Mock;
26 import org.opendaylight.controller.cluster.access.concepts.MemberName;
27 import org.opendaylight.controller.cluster.datastore.config.Configuration;
28 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
29 import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManagerTest.TestShardManager;
30 import org.opendaylight.controller.cluster.raft.TestActorFactory;
31 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
32 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
33 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
34
35 public class AbstractShardManagerTest extends AbstractActorTest {
36
37     protected static final MemberName MEMBER_1 = MemberName.forName("member-1");
38
39     protected static int ID_COUNTER = 1;
40     protected static TestActorRef<MessageCollectorActor> mockShardActor;
41     protected static ShardIdentifier mockShardName;
42
43     protected final String shardMrgIDSuffix = "config" + ID_COUNTER++;
44     protected final TestActorFactory actorFactory = new TestActorFactory(getSystem());
45     protected final Collection<ActorSystem> actorSystems = new ArrayList<>();
46     protected final DatastoreContext.Builder datastoreContextBuilder = DatastoreContext.newBuilder()
47             .dataStoreName(shardMrgIDSuffix).shardInitializationTimeout(600, TimeUnit.MILLISECONDS)
48             .shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(6);
49
50     @Mock
51     protected static CountDownLatch ready;
52
53     protected TestShardManager.Builder newTestShardMgrBuilder() {
54         return TestShardManager.builder(datastoreContextBuilder);
55     }
56
57     protected TestShardManager.Builder newTestShardMgrBuilder(final Configuration config) {
58         return TestShardManager.builder(datastoreContextBuilder).configuration(config);
59     }
60
61     protected Props newShardMgrProps(final Configuration config) {
62         return newTestShardMgrBuilder(config).waitTillReadyCountDownLatch(ready).props();
63     }
64
65     @Before
66     public void setUp() throws Exception {
67         initMocks(this);
68
69         InMemoryJournal.clear();
70         InMemorySnapshotStore.clear();
71
72         if (mockShardActor == null) {
73             mockShardName = ShardIdentifier.create(Shard.DEFAULT_NAME, MEMBER_1, "config");
74             mockShardActor = TestActorRef.create(getSystem(), Props.create(MessageCollectorActor.class),
75                     mockShardName.toString());
76         }
77
78         mockShardActor.underlyingActor().clear();
79     }
80
81     @After
82     public void tearDown() {
83         InMemoryJournal.clear();
84         InMemorySnapshotStore.clear();
85
86         for (final ActorSystem system : actorSystems) {
87             JavaTestKit.shutdownActorSystem(system, null, Boolean.TRUE);
88         }
89
90         mockShardActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
91         mockShardActor = null;
92
93         actorFactory.close();
94     }
95 }