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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.