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;
19 import akka.actor.ActorSystem;
20 import akka.actor.Address;
21 import akka.actor.Props;
22 import akka.testkit.JavaTestKit;
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;
36 import org.opendaylight.controller.remote.rpc.registry.gossip.Messages.GossiperMessages.GossipEnvelope;
37 import org.opendaylight.controller.remote.rpc.registry.gossip.Messages.GossiperMessages.GossipStatus;
40 public class GossiperTest {
42 private static ActorSystem system;
43 private static Gossiper gossiper;
45 private Gossiper mockGossiper;
48 public static void setup() throws InterruptedException {
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() {
57 JavaTestKit.shutdownActorSystem(system);
61 public void createMocks() {
62 mockGossiper = spy(gossiper);
66 public void resetMocks() {
72 public void testReceiveGossipTick_WhenNoRemoteMemberShouldIgnore() {
74 mockGossiper.setClusterMembers(Collections.<Address>emptyList());
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));
92 @SuppressWarnings("unchecked")
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();
108 @SuppressWarnings({ "unchecked", "rawtypes" })
110 public void testReceiveGossipWhenNotAddressedToSelfShouldIgnore() {
111 Address notSelf = new Address("tcp", "not-self");
113 GossipEnvelope envelope = new GossipEnvelope(notSelf, notSelf, mock(Map.class));
114 doNothing().when(mockGossiper).updateRemoteBuckets(anyMap());
115 mockGossiper.receiveGossip(envelope);
116 verify(mockGossiper, times(0)).updateRemoteBuckets(anyMap());
120 * Create Gossiper actor and return the underlying instance of Gossiper class.
122 * @return instance of Gossiper class
124 private static Gossiper createGossiper() {
125 final Props props = Props.create(Gossiper.class, false,
126 new RemoteRpcProviderConfig(system.settings().config()));
127 final TestActorRef<Gossiper> testRef = TestActorRef.create(system, props, "testGossiper");
129 return testRef.underlyingActor();