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.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.actor.Address;
13 import akka.actor.Props;
14 import akka.testkit.JavaTestKit;
15 import akka.testkit.TestActorRef;
16 import com.google.common.collect.ImmutableMap;
17 import com.typesafe.config.ConfigFactory;
18 import java.util.HashMap;
20 import java.util.Optional;
21 import org.junit.AfterClass;
22 import org.junit.Assert;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.remote.rpc.RemoteRpcProviderConfig;
26 import org.opendaylight.controller.remote.rpc.TerminationMonitor;
28 public class BucketStoreTest {
31 * Dummy class to eliminate rawtype warnings.
36 private static class T implements BucketData<T> {
38 public Optional<ActorRef> getWatchActor() {
39 return Optional.empty();
43 private static ActorSystem system;
46 public static void setup() {
47 system = ActorSystem.create("opendaylight-rpc", ConfigFactory.load().getConfig("unit-test"));
48 system.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
52 public static void teardown() {
53 JavaTestKit.shutdownActorSystem(system);
57 * Given remote buckets, should merge with local copy of remote buckets.
60 public void testReceiveUpdateRemoteBuckets() {
62 final BucketStoreActor<T> store = createStore();
64 Address localAddress = system.provider().getDefaultAddress();
65 Bucket<T> localBucket = new BucketImpl<>(0L, new T());
67 final Address a1 = new Address("tcp", "system1");
68 final Address a2 = new Address("tcp", "system2");
69 final Address a3 = new Address("tcp", "system3");
71 final Bucket<T> b1 = new BucketImpl<>(0L, new T());
72 final Bucket<T> b2 = new BucketImpl<>(0L, new T());
73 final Bucket<T> b3 = new BucketImpl<>(0L, new T());
75 //Given remote buckets
76 store.updateRemoteBuckets(ImmutableMap.of(a1, b1, a2, b2, localAddress, localBucket));
78 //Should NOT contain local bucket
79 //Should contain ONLY 3 entries i.e a1, a2
80 Map<Address, Bucket<T>> remoteBucketsInStore = store.getRemoteBuckets();
81 Assert.assertFalse("remote buckets contains local bucket", remoteBucketsInStore.containsKey(localAddress));
82 Assert.assertTrue(remoteBucketsInStore.size() == 2);
84 //Add a new remote bucket
85 Address a4 = new Address("tcp", "system4");
86 Bucket<T> b4 = new BucketImpl<>(0L, new T());
87 store.updateRemoteBuckets(ImmutableMap.of(a4, b4));
90 //Should contain 4 entries now i.e a1, a2, a4
91 remoteBucketsInStore = store.getRemoteBuckets();
92 Assert.assertTrue("Does not contain a4", remoteBucketsInStore.containsKey(a4));
93 Assert.assertTrue(remoteBucketsInStore.size() == 3);
96 Bucket<T> b3New = new BucketImpl<>(0L, new T());
97 Map<Address, Bucket<?>> remoteBuckets = new HashMap<>(3);
98 remoteBuckets.put(a3, b3New);
99 remoteBuckets.put(a1, null);
100 remoteBuckets.put(a2, null);
101 store.updateRemoteBuckets(remoteBuckets);
103 //Should only update a3
104 remoteBucketsInStore = store.getRemoteBuckets();
105 Bucket<T> b3InStore = remoteBucketsInStore.get(a3);
106 Assert.assertEquals(b3New.getVersion(), b3InStore.getVersion());
108 //Should NOT update a1 and a2
109 Bucket<T> b1InStore = remoteBucketsInStore.get(a1);
110 Bucket<T> b2InStore = remoteBucketsInStore.get(a2);
111 Assert.assertEquals(b1.getVersion(), b1InStore.getVersion());
112 Assert.assertEquals(b2.getVersion(), b2InStore.getVersion());
113 Assert.assertTrue(remoteBucketsInStore.size() == 4);
115 //Should update versions map
116 //versions map contains versions for all remote buckets (4).
117 Map<Address, Long> versionsInStore = store.getVersions();
118 Assert.assertEquals(4, versionsInStore.size());
119 Assert.assertEquals((Long)b1.getVersion(), versionsInStore.get(a1));
120 Assert.assertEquals((Long)b2.getVersion(), versionsInStore.get(a2));
121 Assert.assertEquals((Long)b3New.getVersion(), versionsInStore.get(a3));
122 Assert.assertEquals((Long)b4.getVersion(), versionsInStore.get(a4));
124 //Send older version of bucket
125 remoteBuckets.clear();
126 remoteBuckets.put(a3, b3);
127 store.updateRemoteBuckets(remoteBuckets);
129 //Should NOT update a3
130 remoteBucketsInStore = store.getRemoteBuckets();
131 b3InStore = remoteBucketsInStore.get(a3);
132 Assert.assertEquals(b3InStore.getVersion(), b3New.getVersion());
137 * Create BucketStore actor and returns the underlying instance of BucketStore class.
139 * @return instance of BucketStore class
141 private static BucketStoreActor<T> createStore() {
142 final Props props = Props.create(TestingBucketStoreActor.class,
143 new RemoteRpcProviderConfig(system.settings().config()), "testing-store",new T());
144 return TestActorRef.<BucketStoreActor<T>>create(system, props, "testStore").underlyingActor();
147 private static final class TestingBucketStoreActor extends BucketStoreActor<T> {
149 protected TestingBucketStoreActor(final RemoteRpcProviderConfig config,
150 final String persistenceId,
151 final T initialData) {
152 super(config, persistenceId, initialData);
156 protected void onBucketRemoved(final Address address, final Bucket<T> bucket) {
161 protected void onBucketsUpdated(final Map<Address, Bucket<T>> newBuckets) {