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 Thread.sleep(1000);//give some time for previous test to stop the system. Netty port conflict arises otherwise.
49 system = ActorSystem.create("opendaylight-rpc", ConfigFactory.load().getConfig("unit-test"));
50 system.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
52 gossiper = createGossiper();
56 public static void teardown() {
62 public void createMocks(){
63 mockGossiper = spy(gossiper);
67 public void resetMocks(){
73 public void testReceiveGossipTick_WhenNoRemoteMemberShouldIgnore(){
75 mockGossiper.setClusterMembers(Collections.EMPTY_LIST);
76 doNothing().when(mockGossiper).getLocalStatusAndSendTo(any(Address.class));
77 mockGossiper.receiveGossipTick();
78 verify(mockGossiper, times(0)).getLocalStatusAndSendTo(any(Address.class));
82 public void testReceiveGossipTick_WhenRemoteMemberExistsShouldSendStatus(){
83 List<Address> members = new ArrayList<>();
84 Address remote = new Address("tcp", "member");
87 mockGossiper.setClusterMembers(members);
88 doNothing().when(mockGossiper).getLocalStatusAndSendTo(any(Address.class));
89 mockGossiper.receiveGossipTick();
90 verify(mockGossiper, times(1)).getLocalStatusAndSendTo(any(Address.class));
94 public void testReceiveGossipStatus_WhenSenderIsNonMemberShouldIgnore(){
96 Address nonMember = new Address("tcp", "non-member");
97 GossipStatus remoteStatus = new GossipStatus(nonMember, mock(Map.class));
100 List<Address> members = new ArrayList<>();
101 members.add(new Address("tcp", "member"));
103 mockGossiper.setClusterMembers(members);
104 mockGossiper.receiveGossipStatus(remoteStatus);
105 verify(mockGossiper, times(0)).getSender();
109 public void testReceiveGossip_WhenNotAddressedToSelfShouldIgnore(){
110 Address notSelf = new Address("tcp", "not-self");
112 GossipEnvelope envelope = new GossipEnvelope(notSelf, notSelf, mock(Map.class));
113 doNothing().when(mockGossiper).updateRemoteBuckets(anyMap());
114 mockGossiper.receiveGossip(envelope);
115 verify(mockGossiper, times(0)).updateRemoteBuckets(anyMap());
119 * Create Gossiper actor and return the underlying instance of Gossiper class.
121 * @return instance of Gossiper class
123 private static Gossiper createGossiper(){
125 final Props props = Props.create(Gossiper.class, false);
126 final TestActorRef<Gossiper> testRef = TestActorRef.create(system, props, "testGossiper");
128 return testRef.underlyingActor();