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.TestActorRef;
15 import akka.testkit.javadsl.TestKit;
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.RemoteOpsProviderConfig;
26 import org.opendaylight.controller.remote.rpc.TerminationMonitor;
28 public class BucketStoreTest {
30 * Dummy class to eliminate rawtype warnings.
34 private static final class T implements BucketData<T> {
36 public Optional<ActorRef> getWatchActor() {
37 return Optional.empty();
41 private static ActorSystem system;
44 public static void setup() {
45 system = ActorSystem.create("opendaylight-rpc", ConfigFactory.load().getConfig("unit-test"));
46 system.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
50 public static void teardown() {
51 TestKit.shutdownActorSystem(system);
55 * Given remote buckets, should merge with local copy of remote buckets.
58 public void testReceiveUpdateRemoteBuckets() {
60 final BucketStoreActor<T> store = createStore();
62 Address localAddress = system.provider().getDefaultAddress();
63 Bucket<T> localBucket = new BucketImpl<>(0L, new T());
65 final Address a1 = new Address("tcp", "system1");
66 final Address a2 = new Address("tcp", "system2");
67 final Address a3 = new Address("tcp", "system3");
69 final Bucket<T> b1 = new BucketImpl<>(0L, new T());
70 final Bucket<T> b2 = new BucketImpl<>(0L, new T());
71 final Bucket<T> b3 = new BucketImpl<>(0L, new T());
73 //Given remote buckets
74 store.updateRemoteBuckets(ImmutableMap.of(a1, b1, a2, b2, localAddress, localBucket));
76 //Should NOT contain local bucket
77 //Should contain ONLY 3 entries i.e a1, a2
78 Map<Address, Bucket<T>> remoteBucketsInStore = store.getRemoteBuckets();
79 Assert.assertFalse("remote buckets contains local bucket", remoteBucketsInStore.containsKey(localAddress));
80 Assert.assertTrue(remoteBucketsInStore.size() == 2);
82 //Add a new remote bucket
83 Address a4 = new Address("tcp", "system4");
84 Bucket<T> b4 = new BucketImpl<>(0L, new T());
85 store.updateRemoteBuckets(ImmutableMap.of(a4, b4));
88 //Should contain 4 entries now i.e a1, a2, a4
89 remoteBucketsInStore = store.getRemoteBuckets();
90 Assert.assertTrue("Does not contain a4", remoteBucketsInStore.containsKey(a4));
91 Assert.assertTrue(remoteBucketsInStore.size() == 3);
94 Bucket<T> b3New = new BucketImpl<>(0L, new T());
95 Map<Address, Bucket<?>> remoteBuckets = new HashMap<>(3);
96 remoteBuckets.put(a3, b3New);
97 remoteBuckets.put(a1, null);
98 remoteBuckets.put(a2, null);
99 store.updateRemoteBuckets(remoteBuckets);
101 //Should only update a3
102 remoteBucketsInStore = store.getRemoteBuckets();
103 Bucket<T> b3InStore = remoteBucketsInStore.get(a3);
104 Assert.assertEquals(b3New.getVersion(), b3InStore.getVersion());
106 //Should NOT update a1 and a2
107 Bucket<T> b1InStore = remoteBucketsInStore.get(a1);
108 Bucket<T> b2InStore = remoteBucketsInStore.get(a2);
109 Assert.assertEquals(b1.getVersion(), b1InStore.getVersion());
110 Assert.assertEquals(b2.getVersion(), b2InStore.getVersion());
111 Assert.assertTrue(remoteBucketsInStore.size() == 4);
113 //Should update versions map
114 //versions map contains versions for all remote buckets (4).
115 Map<Address, Long> versionsInStore = store.getVersions();
116 Assert.assertEquals(4, versionsInStore.size());
117 Assert.assertEquals((Long)b1.getVersion(), versionsInStore.get(a1));
118 Assert.assertEquals((Long)b2.getVersion(), versionsInStore.get(a2));
119 Assert.assertEquals((Long)b3New.getVersion(), versionsInStore.get(a3));
120 Assert.assertEquals((Long)b4.getVersion(), versionsInStore.get(a4));
122 //Send older version of bucket
123 remoteBuckets.clear();
124 remoteBuckets.put(a3, b3);
125 store.updateRemoteBuckets(remoteBuckets);
127 //Should NOT update a3
128 remoteBucketsInStore = store.getRemoteBuckets();
129 b3InStore = remoteBucketsInStore.get(a3);
130 Assert.assertEquals(b3InStore.getVersion(), b3New.getVersion());
135 * Create BucketStore actor and returns the underlying instance of BucketStore class.
137 * @return instance of BucketStore class
139 private static BucketStoreActor<T> createStore() {
140 final Props props = Props.create(TestingBucketStoreActor.class,
141 new RemoteOpsProviderConfig(system.settings().config()), "testing-store",new T());
142 return TestActorRef.<BucketStoreActor<T>>create(system, props, "testStore").underlyingActor();
145 private static final class TestingBucketStoreActor extends BucketStoreActor<T> {
147 protected TestingBucketStoreActor(final RemoteOpsProviderConfig config,
148 final String persistenceId,
149 final T initialData) {
150 super(config, persistenceId, initialData);
154 protected void onBucketRemoved(final Address address, final Bucket<T> bucket) {
159 protected void onBucketsUpdated(final Map<Address, Bucket<T>> newBuckets) {