003964a0a1a0e881dd80e59722a8b7795591f4ed
[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 import akka.actor.ActorRef;
13 import akka.testkit.JavaTestKit;
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      * @throws Exception
36      */
37     @Test
38     public void testLeaderSchedulesHeartbeatsEvenWhenNoFollowersRespondToInitialAppendEntries() throws Exception {
39         logStart("testLeaderSchedulesHeartbeatsEvenWhenNoFollowersRespondToInitialAppendEntries");
40         new JavaTestKit(getSystem()) {{
41             String leaderActorId = actorFactory.generateActorId("leader");
42             String follower1ActorId = actorFactory.generateActorId("follower");
43             String follower2ActorId = actorFactory.generateActorId("follower");
44
45             TestActorRef<ForwardMessageToBehaviorActor> leaderActor =
46                     actorFactory.createTestActor(ForwardMessageToBehaviorActor.props(), leaderActorId);
47             ActorRef follower1Actor = actorFactory.createActor(MessageCollectorActor.props(), follower1ActorId);
48             ActorRef follower2Actor = actorFactory.createActor(MessageCollectorActor.props(), follower2ActorId);
49
50             MockRaftActorContext leaderActorContext =
51                     new MockRaftActorContext(leaderActorId, getSystem(), leaderActor);
52
53             DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
54             configParams.setHeartBeatInterval(new FiniteDuration(200, TimeUnit.MILLISECONDS));
55             configParams.setIsolatedLeaderCheckInterval(new FiniteDuration(10, TimeUnit.SECONDS));
56
57             leaderActorContext.setConfigParams(configParams);
58
59             leaderActorContext.setReplicatedLog(
60                     new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(1,5,1).build());
61
62             Map<String, String> peerAddresses = new HashMap<>();
63             peerAddresses.put(follower1ActorId,
64                     follower1Actor.path().toString());
65             peerAddresses.put(follower2ActorId,
66                     follower2Actor.path().toString());
67
68
69             leaderActorContext.setPeerAddresses(peerAddresses);
70
71             RaftActorBehavior leader = createBehavior(leaderActorContext);
72
73             leaderActor.underlyingActor().setBehavior(leader);
74
75             Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
76
77             List<SendHeartBeat> allMessages = MessageCollectorActor.getAllMatching(leaderActor, SendHeartBeat.class);
78
79             // Need more than 1 heartbeat to be delivered because we waited for 1 second with heartbeat interval 200ms
80             assertTrue(String.format("%s messages is less than expected", allMessages.size()),
81                     allMessages.size() > 1);
82
83         }};
84     }
85
86 }

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