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