2 * Copyright (c) 2014 Cisco 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.remote.rpc.registry.gossip;
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.anyMap;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.reset;
15 import static org.mockito.Mockito.spy;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.opendaylight.controller.remote.rpc.registry.gossip.Messages.GossiperMessages.GossipEnvelope;
19 import static org.opendaylight.controller.remote.rpc.registry.gossip.Messages.GossiperMessages.GossipStatus;
20 import akka.actor.ActorSystem;
21 import akka.actor.Address;
22 import akka.actor.Props;
23 import akka.testkit.TestActorRef;
24 import com.typesafe.config.ConfigFactory;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
29 import org.junit.After;
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.opendaylight.controller.remote.rpc.RemoteRpcProviderConfig;
35 import org.opendaylight.controller.remote.rpc.TerminationMonitor;
38 public class GossiperTest {
40 private static ActorSystem system;
41 private static Gossiper gossiper;
43 private Gossiper mockGossiper;
46 public static void setup() throws InterruptedException {
47 system = ActorSystem.create("opendaylight-rpc", ConfigFactory.load().getConfig("unit-test"));
48 system.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
50 gossiper = createGossiper();
54 public static void teardown() {
60 public void createMocks(){
61 mockGossiper = spy(gossiper);
65 public void resetMocks(){
71 public void testReceiveGossipTick_WhenNoRemoteMemberShouldIgnore(){
73 mockGossiper.setClusterMembers(Collections.<Address>emptyList());
74 doNothing().when(mockGossiper).getLocalStatusAndSendTo(any(Address.class));
75 mockGossiper.receiveGossipTick();
76 verify(mockGossiper, times(0)).getLocalStatusAndSendTo(any(Address.class));
80 public void testReceiveGossipTick_WhenRemoteMemberExistsShouldSendStatus(){
81 List<Address> members = new ArrayList<>();
82 Address remote = new Address("tcp", "member");
85 mockGossiper.setClusterMembers(members);
86 doNothing().when(mockGossiper).getLocalStatusAndSendTo(any(Address.class));
87 mockGossiper.receiveGossipTick();
88 verify(mockGossiper, times(1)).getLocalStatusAndSendTo(any(Address.class));
92 public void testReceiveGossipStatus_WhenSenderIsNonMemberShouldIgnore(){
94 Address nonMember = new Address("tcp", "non-member");
95 GossipStatus remoteStatus = new GossipStatus(nonMember, mock(Map.class));
98 List<Address> members = new ArrayList<>();
99 members.add(new Address("tcp", "member"));
101 mockGossiper.setClusterMembers(members);
102 mockGossiper.receiveGossipStatus(remoteStatus);
103 verify(mockGossiper, times(0)).getSender();
107 public void testReceiveGossip_WhenNotAddressedToSelfShouldIgnore(){
108 Address notSelf = new Address("tcp", "not-self");
110 GossipEnvelope envelope = new GossipEnvelope(notSelf, notSelf, mock(Map.class));
111 doNothing().when(mockGossiper).updateRemoteBuckets(anyMap());
112 mockGossiper.receiveGossip(envelope);
113 verify(mockGossiper, times(0)).updateRemoteBuckets(anyMap());
117 * Create Gossiper actor and return the underlying instance of Gossiper class.
119 * @return instance of Gossiper class
121 private static Gossiper createGossiper(){
123 final Props props = Props.create(Gossiper.class, false, new RemoteRpcProviderConfig(system.settings().config()));
124 final TestActorRef<Gossiper> testRef = TestActorRef.create(system, props, "testGossiper");
126 return testRef.underlyingActor();