2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.raft;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.mockito.ArgumentMatchers.anyString;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.never;
17 import static org.mockito.Mockito.reset;
18 import static org.mockito.Mockito.verify;
20 import akka.actor.Props;
21 import akka.testkit.TestActorRef;
22 import com.google.common.util.concurrent.MoreExecutors;
23 import java.util.HashMap;
24 import java.util.List;
26 import org.junit.After;
27 import org.junit.Test;
28 import org.opendaylight.controller.cluster.DataPersistenceProvider;
29 import org.opendaylight.controller.cluster.NonPersistentDataProvider;
30 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
31 import org.opendaylight.controller.cluster.raft.persisted.ServerInfo;
32 import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Unit tests for RaftActorContextImpl.
39 * @author Thomas Pantelis
41 public class RaftActorContextImplTest extends AbstractActorTest {
42 private final TestActorFactory actorFactory = new TestActorFactory(getSystem());
44 private final TestActorRef<DoNothingActor> actor = actorFactory.createTestActor(
45 Props.create(DoNothingActor.class), actorFactory.generateActorId("actor"));
47 private static final Logger LOG = LoggerFactory.getLogger(RaftActorContextImplTest.class);
50 public void tearDown() {
55 public void testGetPeerAddress() {
56 Map<String, String> peerMap = new HashMap<>();
57 peerMap.put("peer1", "peerAddress1");
58 peerMap.put("peer2", null);
59 DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
60 RaftActorContextImpl context = new RaftActorContextImpl(actor, actor.underlyingActor().getContext(),
61 "test", new ElectionTermImpl(createProvider(), "test", LOG), -1, -1,
62 peerMap, configParams, createProvider(), applyState -> { }, LOG, MoreExecutors.directExecutor());
64 assertEquals("getPeerAddress", "peerAddress1", context.getPeerAddress("peer1"));
65 assertEquals("getPeerAddress", null, context.getPeerAddress("peer2"));
67 PeerAddressResolver mockResolver = mock(PeerAddressResolver.class);
68 doReturn("peerAddress2").when(mockResolver).resolve("peer2");
69 doReturn("peerAddress3").when(mockResolver).resolve("peer3");
70 configParams.setPeerAddressResolver(mockResolver);
72 assertEquals("getPeerAddress", "peerAddress2", context.getPeerAddress("peer2"));
73 assertEquals("getPeerAddress", "peerAddress3", context.getPeerAddress("peer3"));
76 assertEquals("getPeerAddress", "peerAddress1", context.getPeerAddress("peer1"));
77 assertEquals("getPeerAddress", "peerAddress2", context.getPeerAddress("peer2"));
78 verify(mockResolver, never()).resolve(anyString());
82 public void testSetPeerAddress() {
83 DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
84 RaftActorContextImpl context = new RaftActorContextImpl(actor, actor.underlyingActor().getContext(),
85 "test", new ElectionTermImpl(createProvider(), "test", LOG), -1, -1,
86 Map.of("peer1", "peerAddress1"), configParams,
87 createProvider(), applyState -> { }, LOG, MoreExecutors.directExecutor());
89 context.setPeerAddress("peer1", "peerAddress1_1");
90 assertEquals("getPeerAddress", "peerAddress1_1", context.getPeerAddress("peer1"));
92 context.setPeerAddress("peer2", "peerAddress2");
93 assertEquals("getPeerAddress", null, context.getPeerAddress("peer2"));
97 public void testUpdatePeerIds() {
98 RaftActorContextImpl context = new RaftActorContextImpl(actor, actor.underlyingActor().getContext(),
99 "self", new ElectionTermImpl(createProvider(), "test", LOG), -1, -1,
100 Map.of("peer1", "peerAddress1"),
101 new DefaultConfigParamsImpl(), createProvider(), applyState -> { }, LOG,
102 MoreExecutors.directExecutor());
104 context.updatePeerIds(new ServerConfigurationPayload(List.of(new ServerInfo("self", false),
105 new ServerInfo("peer2", true), new ServerInfo("peer3", false))));
106 verifyPeerInfo(context, "peer1", null);
107 verifyPeerInfo(context, "peer2", true);
108 verifyPeerInfo(context, "peer3", false);
109 assertEquals("isVotingMember", false, context.isVotingMember());
111 context.updatePeerIds(new ServerConfigurationPayload(List.of(new ServerInfo("self", true),
112 new ServerInfo("peer2", true), new ServerInfo("peer3", true))));
113 verifyPeerInfo(context, "peer2", true);
114 verifyPeerInfo(context, "peer3", true);
115 assertEquals("isVotingMember", true, context.isVotingMember());
117 context.updatePeerIds(new ServerConfigurationPayload(List.of(new ServerInfo("peer2", true),
118 new ServerInfo("peer3", true))));
119 verifyPeerInfo(context, "peer2", true);
120 verifyPeerInfo(context, "peer3", true);
121 assertEquals("isVotingMember", false, context.isVotingMember());
124 private static DataPersistenceProvider createProvider() {
125 return new NonPersistentDataProvider(Runnable::run);
128 private static void verifyPeerInfo(final RaftActorContextImpl context, final String peerId, final Boolean voting) {
129 PeerInfo peerInfo = context.getPeerInfo(peerId);
130 if (voting != null) {
131 assertNotNull("Expected peer " + peerId, peerInfo);
132 assertEquals("getVotingState for " + peerId, voting
133 ? VotingState.VOTING : VotingState.NON_VOTING, peerInfo.getVotingState());
135 assertNull("Unexpected peer " + peerId, peerInfo);