Fix ConcurrentModificationEx in RpcRegistry.onBucketsUpdated
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / registry / RpcRegistryTest.java
index 5b7b7e4fdc61dfaaa2377078853b691b48b07394..772dae232f8dd5ca1546ee170108a95e4e4b7d4c 100644 (file)
@@ -1,12 +1,30 @@
+/*
+ * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
 package org.opendaylight.controller.remote.rpc.registry;
 
+import static org.junit.Assert.fail;
 import akka.actor.ActorRef;
 import akka.actor.ActorSystem;
 import akka.actor.Address;
 import akka.actor.Props;
+import akka.cluster.Cluster;
+import akka.cluster.ClusterEvent.CurrentClusterState;
+import akka.cluster.Member;
+import akka.cluster.MemberStatus;
+import akka.cluster.UniqueAddress;
 import akka.japi.Pair;
 import akka.testkit.JavaTestKit;
+import com.google.common.base.Stopwatch;
+import com.google.common.collect.Sets;
 import com.google.common.util.concurrent.Uninterruptibles;
+import com.typesafe.config.Config;
+import com.typesafe.config.ConfigFactory;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
@@ -14,6 +32,7 @@ import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.TimeUnit;
 import org.junit.After;
 import org.junit.AfterClass;
@@ -21,6 +40,7 @@ import org.junit.Assert;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.opendaylight.controller.cluster.common.actor.AkkaConfigurationReader;
 import org.opendaylight.controller.remote.rpc.RemoteRpcProviderConfig;
 import org.opendaylight.controller.remote.rpc.RouteIdentifierImpl;
 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.AddOrUpdateRoutes;
@@ -53,26 +73,61 @@ public class RpcRegistryTest {
 
     @BeforeClass
     public static void staticSetup() throws InterruptedException {
-      RemoteRpcProviderConfig config1 = new RemoteRpcProviderConfig.Builder("memberA").build();
-      RemoteRpcProviderConfig config2 = new RemoteRpcProviderConfig.Builder("memberB").build();
-      RemoteRpcProviderConfig config3 = new RemoteRpcProviderConfig.Builder("memberC").build();
-      node1 = ActorSystem.create("opendaylight-rpc", config1.get());
-      node2 = ActorSystem.create("opendaylight-rpc", config2.get());
-      node3 = ActorSystem.create("opendaylight-rpc", config3.get());
+        AkkaConfigurationReader reader = new AkkaConfigurationReader() {
+            @Override
+            public Config read() {
+                return ConfigFactory.load();
+            }
+        };
+
+        RemoteRpcProviderConfig config1 = new RemoteRpcProviderConfig.Builder("memberA").gossipTickInterval("200ms").
+                withConfigReader(reader).build();
+        RemoteRpcProviderConfig config2 = new RemoteRpcProviderConfig.Builder("memberB").gossipTickInterval("200ms")
+                .withConfigReader(reader).build();
+        RemoteRpcProviderConfig config3 = new RemoteRpcProviderConfig.Builder("memberC").gossipTickInterval("200ms")
+                .withConfigReader(reader).build();
+        node1 = ActorSystem.create("opendaylight-rpc", config1.get());
+        node2 = ActorSystem.create("opendaylight-rpc", config2.get());
+        node3 = ActorSystem.create("opendaylight-rpc", config3.get());
+
+        waitForMembersUp(node1, Cluster.get(node2).selfUniqueAddress(), Cluster.get(node3).selfUniqueAddress());
+        waitForMembersUp(node2, Cluster.get(node1).selfUniqueAddress(), Cluster.get(node3).selfUniqueAddress());
+    }
+
+    static void waitForMembersUp(ActorSystem node, UniqueAddress... addresses) {
+        Set<UniqueAddress> otherMembersSet = Sets.newHashSet(addresses);
+        Stopwatch sw = Stopwatch.createStarted();
+        while(sw.elapsed(TimeUnit.SECONDS) <= 10) {
+            CurrentClusterState state = Cluster.get(node).state();
+            for(Member m: state.getMembers()) {
+                if(m.status() == MemberStatus.up() && otherMembersSet.remove(m.uniqueAddress()) &&
+                        otherMembersSet.isEmpty()) {
+                    return;
+                }
+            }
+
+            Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
+        }
+
+        fail("Member(s) " + otherMembersSet + " are not Up");
     }
 
     @AfterClass
     public static void staticTeardown() {
-      JavaTestKit.shutdownActorSystem(node1);
-      JavaTestKit.shutdownActorSystem(node2);
-      JavaTestKit.shutdownActorSystem(node3);
+        JavaTestKit.shutdownActorSystem(node1);
+        JavaTestKit.shutdownActorSystem(node2);
+        JavaTestKit.shutdownActorSystem(node3);
     }
 
     @Before
     public void setup() {
-        registry1 = node1.actorOf(Props.create(RpcRegistry.class));
-        registry2 = node2.actorOf(Props.create(RpcRegistry.class));
-        registry3 = node3.actorOf(Props.create(RpcRegistry.class));
+        registry1 = node1.actorOf(Props.create(RpcRegistry.class, config(node1)));
+        registry2 = node2.actorOf(Props.create(RpcRegistry.class, config(node2)));
+        registry3 = node3.actorOf(Props.create(RpcRegistry.class, config(node3)));
+    }
+
+    private RemoteRpcProviderConfig config(ActorSystem node){
+        return new RemoteRpcProviderConfig(node.settings().config());
     }
 
     @After
@@ -113,7 +168,7 @@ public class RpcRegistryTest {
 
         // Bucket store should get an update bucket message. Updated bucket contains added rpc.
 
-        Map<Address, Bucket> buckets = retrieveBuckets(registry1, mockBroker, nodeAddress);
+        Map<Address, Bucket<RoutingTable>> buckets = retrieveBuckets(registry1, mockBroker, nodeAddress);
         verifyBucket(buckets.get(nodeAddress), addedRouteIds);
 
         Map<Address, Long> versions = retrieveVersions(registry1, mockBroker);
@@ -156,7 +211,7 @@ public class RpcRegistryTest {
 
         // Bucket store on node2 should get a message to update its local copy of remote buckets
 
-        Map<Address, Bucket> buckets = retrieveBuckets(registry2, mockBroker2, node1Address);
+        Map<Address, Bucket<RoutingTable>> buckets = retrieveBuckets(registry2, mockBroker2, node1Address);
         verifyBucket(buckets.get(node1Address), addedRouteIds);
 
         // Now remove
@@ -172,7 +227,7 @@ public class RpcRegistryTest {
 
     private void verifyEmptyBucket(JavaTestKit testKit, ActorRef registry, Address address)
             throws AssertionError {
-        Map<Address, Bucket> buckets;
+        Map<Address, Bucket<RoutingTable>> buckets;
         int nTries = 0;
         while(true) {
             buckets = retrieveBuckets(registry1, testKit, address);
@@ -217,7 +272,7 @@ public class RpcRegistryTest {
         Address node1Address = node1.provider().getDefaultAddress();
         Address node2Address = node2.provider().getDefaultAddress();
 
-        Map<Address, Bucket> buckets = retrieveBuckets(registry3, mockBroker3, node1Address,
+        Map<Address, Bucket<RoutingTable>> buckets = retrieveBuckets(registry3, mockBroker3, node1Address,
                 node2Address);
 
         verifyBucket(buckets.get(node1Address), addedRouteIds1);
@@ -261,18 +316,19 @@ public class RpcRegistryTest {
         Assert.assertEquals("RoutingTable size", expRouteIds.size(), table.size());
     }
 
-    private Map<Address, Bucket> retrieveBuckets(ActorRef bucketStore, JavaTestKit testKit,
+    private Map<Address, Bucket<RoutingTable>> retrieveBuckets(ActorRef bucketStore, JavaTestKit testKit,
             Address... addresses) {
         int nTries = 0;
         while(true) {
             bucketStore.tell(new GetAllBuckets(), testKit.getRef());
-            GetAllBucketsReply reply = testKit.expectMsgClass(Duration.create(3, TimeUnit.SECONDS),
+            @SuppressWarnings("unchecked")
+            GetAllBucketsReply<RoutingTable> reply = testKit.expectMsgClass(Duration.create(3, TimeUnit.SECONDS),
                     GetAllBucketsReply.class);
 
-            Map<Address, Bucket> buckets = reply.getBuckets();
+            Map<Address, Bucket<RoutingTable>> buckets = reply.getBuckets();
             boolean foundAll = true;
             for(Address addr: addresses) {
-                Bucket bucket = buckets.get(addr);
+                Bucket<RoutingTable> bucket = buckets.get(addr);
                 if(bucket  == null) {
                     foundAll = false;
                     break;
@@ -292,7 +348,6 @@ public class RpcRegistryTest {
         }
     }
 
-    @SuppressWarnings("unchecked")
     @Test
     public void testAddRoutesConcurrency() throws Exception {
         final JavaTestKit testKit = new JavaTestKit(node1);
@@ -316,7 +371,8 @@ public class RpcRegistryTest {
         int nTries = 0;
         while(true) {
             registry1.tell(getAllBuckets, testKit.getRef());
-            GetAllBucketsReply reply = testKit.expectMsgClass(duration, GetAllBucketsReply.class);
+            @SuppressWarnings("unchecked")
+            GetAllBucketsReply<RoutingTable> reply = testKit.expectMsgClass(duration, GetAllBucketsReply.class);
 
             Bucket<RoutingTable> localBucket = reply.getBuckets().values().iterator().next();
             RoutingTable table = localBucket.getData();
@@ -342,4 +398,49 @@ public class RpcRegistryTest {
         routeIds.add(new RouteIdentifierImpl(null, type, null));
         return routeIds;
     }
+
+    @Test
+    public void testFindRoutersNotPresentInitially() throws Exception {
+
+        final JavaTestKit mockBroker1 = new JavaTestKit(node1);
+        final JavaTestKit mockBroker2 = new JavaTestKit(node2);
+
+        registry1.tell(new SetLocalRouter(mockBroker1.getRef()), mockBroker1.getRef());
+        registry2.tell(new SetLocalRouter(mockBroker2.getRef()), mockBroker2.getRef());
+
+        List<RpcRouter.RouteIdentifier<?, ?, ?>> routeIds = createRouteIds();
+        routeIds.addAll(createRouteIds());
+
+        JavaTestKit replyKit1 = new JavaTestKit(node1);
+        registry1.tell(new FindRouters(routeIds.get(0)), replyKit1.getRef());
+        JavaTestKit replyKit2 = new JavaTestKit(node1);
+        registry1.tell(new FindRouters(routeIds.get(1)), replyKit2.getRef());
+
+        registry2.tell(new AddOrUpdateRoutes(routeIds), mockBroker2.getRef());
+
+        FindRoutersReply reply = replyKit1.expectMsgClass(Duration.create(7, TimeUnit.SECONDS),
+                FindRoutersReply.class);
+        Assert.assertEquals("getRouterWithUpdateTime size", 1, reply.getRouterWithUpdateTime().size());
+
+        reply = replyKit2.expectMsgClass(Duration.create(7, TimeUnit.SECONDS),
+                FindRoutersReply.class);
+        Assert.assertEquals("getRouterWithUpdateTime size", 1, reply.getRouterWithUpdateTime().size());
+    }
+
+    @Test
+    public void testFindRoutersNonExistent() throws Exception {
+
+        final JavaTestKit mockBroker1 = new JavaTestKit(node1);
+
+        registry1.tell(new SetLocalRouter(mockBroker1.getRef()), mockBroker1.getRef());
+
+        List<RpcRouter.RouteIdentifier<?, ?, ?>> routeIds = createRouteIds();
+
+        registry1.tell(new FindRouters(routeIds.get(0)), mockBroker1.getRef());
+
+        FindRoutersReply reply = mockBroker1.expectMsgClass(Duration.create(7, TimeUnit.SECONDS),
+                FindRoutersReply.class);
+        List<Pair<ActorRef, Long>> respList = reply.getRouterWithUpdateTime();
+        Assert.assertEquals("getRouterWithUpdateTime size", 0, respList.size());
+    }
 }