Fix warnings in sal-akka-raft test classes
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / behaviors / AbstractLeaderTest.java
1 /*
2  * Copyright (c) 2015 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.raft.behaviors;
10
11 import static org.junit.Assert.assertTrue;
12
13 import akka.actor.ActorRef;
14 import akka.testkit.TestActorRef;
15 import com.google.common.util.concurrent.Uninterruptibles;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.concurrent.TimeUnit;
20 import org.junit.Test;
21 import org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl;
22 import org.opendaylight.controller.cluster.raft.MockRaftActorContext;
23 import org.opendaylight.controller.cluster.raft.base.messages.SendHeartBeat;
24 import org.opendaylight.controller.cluster.raft.utils.ForwardMessageToBehaviorActor;
25 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
26 import scala.concurrent.duration.FiniteDuration;
27
28 public abstract class AbstractLeaderTest<T extends AbstractLeader> extends AbstractRaftActorBehaviorTest<T> {
29
30     /**
31      * When we removed scheduling of heartbeat in the AbstractLeader constructor we ended up with a situation where
32      * if no follower responded to an initial AppendEntries heartbeats would not be sent to it. This test verifies
33      * that regardless of whether followers respond or not we schedule heartbeats.
34      */
35     @Test
36     public void testLeaderSchedulesHeartbeatsEvenWhenNoFollowersRespondToInitialAppendEntries() throws Exception {
37         logStart("testLeaderSchedulesHeartbeatsEvenWhenNoFollowersRespondToInitialAppendEntries");
38
39         String leaderActorId = actorFactory.generateActorId("leader");
40         String follower1ActorId = actorFactory.generateActorId("follower");
41         String follower2ActorId = actorFactory.generateActorId("follower");
42
43         TestActorRef<ForwardMessageToBehaviorActor> leaderActor =
44                 actorFactory.createTestActor(ForwardMessageToBehaviorActor.props(), leaderActorId);
45         final ActorRef follower1Actor = actorFactory.createActor(MessageCollectorActor.props(), follower1ActorId);
46         final ActorRef follower2Actor = actorFactory.createActor(MessageCollectorActor.props(), follower2ActorId);
47
48         MockRaftActorContext leaderActorContext =
49                 new MockRaftActorContext(leaderActorId, getSystem(), leaderActor);
50
51         DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
52         configParams.setHeartBeatInterval(new FiniteDuration(200, TimeUnit.MILLISECONDS));
53         configParams.setIsolatedLeaderCheckInterval(new FiniteDuration(10, TimeUnit.SECONDS));
54
55         leaderActorContext.setConfigParams(configParams);
56
57         leaderActorContext.setReplicatedLog(
58                 new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(1,5,1).build());
59
60         Map<String, String> peerAddresses = new HashMap<>();
61         peerAddresses.put(follower1ActorId,
62                 follower1Actor.path().toString());
63         peerAddresses.put(follower2ActorId,
64                 follower2Actor.path().toString());
65
66
67         leaderActorContext.setPeerAddresses(peerAddresses);
68
69         RaftActorBehavior leader = createBehavior(leaderActorContext);
70
71         leaderActor.underlyingActor().setBehavior(leader);
72
73         Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
74
75         List<SendHeartBeat> allMessages = MessageCollectorActor.getAllMatching(leaderActor, SendHeartBeat.class);
76
77         // Need more than 1 heartbeat to be delivered because we waited for 1 second with heartbeat interval 200ms
78         assertTrue(String.format("%s messages is less than expected", allMessages.size()),
79                 allMessages.size() > 1);
80     }
81 }

©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.