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 akka.actor.ActorSystem;
11 import akka.actor.Address;
12 import akka.actor.Props;
13 import akka.testkit.TestActorRef;
14 import com.typesafe.config.ConfigFactory;
15 import org.junit.After;
16 import org.junit.AfterClass;
17 import org.junit.Before;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.controller.remote.rpc.TerminationMonitor;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
27 import static org.mockito.Matchers.any;
28 import static org.mockito.Matchers.anyMap;
29 import static org.mockito.Mockito.doNothing;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.reset;
32 import static org.mockito.Mockito.spy;
33 import static org.mockito.Mockito.times;
34 import static org.mockito.Mockito.verify;
35 import static org.opendaylight.controller.remote.rpc.registry.gossip.Messages.GossiperMessages.GossipEnvelope;
36 import static org.opendaylight.controller.remote.rpc.registry.gossip.Messages.GossiperMessages.GossipStatus;
39 public class GossiperTest {
41 private static ActorSystem system;
42 private static Gossiper gossiper;
44 private Gossiper mockGossiper;
47 public static void setup() throws InterruptedException {
48 system = ActorSystem.create("opendaylight-rpc", ConfigFactory.load().getConfig("unit-test"));
49 system.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
51 gossiper = createGossiper();
55 public static void teardown() {
61 public void createMocks(){
62 mockGossiper = spy(gossiper);
66 public void resetMocks(){
72 public void testReceiveGossipTick_WhenNoRemoteMemberShouldIgnore(){
74 mockGossiper.setClusterMembers(Collections.EMPTY_LIST);
75 doNothing().when(mockGossiper).getLocalStatusAndSendTo(any(Address.class));
76 mockGossiper.receiveGossipTick();
77 verify(mockGossiper, times(0)).getLocalStatusAndSendTo(any(Address.class));
81 public void testReceiveGossipTick_WhenRemoteMemberExistsShouldSendStatus(){
82 List<Address> members = new ArrayList<>();
83 Address remote = new Address("tcp", "member");
86 mockGossiper.setClusterMembers(members);
87 doNothing().when(mockGossiper).getLocalStatusAndSendTo(any(Address.class));
88 mockGossiper.receiveGossipTick();
89 verify(mockGossiper, times(1)).getLocalStatusAndSendTo(any(Address.class));
93 public void testReceiveGossipStatus_WhenSenderIsNonMemberShouldIgnore(){
95 Address nonMember = new Address("tcp", "non-member");
96 GossipStatus remoteStatus = new GossipStatus(nonMember, mock(Map.class));
99 List<Address> members = new ArrayList<>();
100 members.add(new Address("tcp", "member"));
102 mockGossiper.setClusterMembers(members);
103 mockGossiper.receiveGossipStatus(remoteStatus);
104 verify(mockGossiper, times(0)).getSender();
108 public void testReceiveGossip_WhenNotAddressedToSelfShouldIgnore(){
109 Address notSelf = new Address("tcp", "not-self");
111 GossipEnvelope envelope = new GossipEnvelope(notSelf, notSelf, mock(Map.class));
112 doNothing().when(mockGossiper).updateRemoteBuckets(anyMap());
113 mockGossiper.receiveGossip(envelope);
114 verify(mockGossiper, times(0)).updateRemoteBuckets(anyMap());
118 * Create Gossiper actor and return the underlying instance of Gossiper class.
120 * @return instance of Gossiper class
122 private static Gossiper createGossiper(){
124 final Props props = Props.create(Gossiper.class, false);
125 final TestActorRef<Gossiper> testRef = TestActorRef.create(system, props, "testGossiper");
127 return testRef.underlyingActor();