Refactor MessageCollectorActor and DoNothingActor 47/29947/5
authorGary Wu <gary.wu1@huawei.com>
Fri, 20 Nov 2015 01:16:13 +0000 (17:16 -0800)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 24 Nov 2015 11:52:29 +0000 (11:52 +0000)
Refactor/consolidate the duplicate copies of
MessageCollectorActor and DoNothingActor used in
org/opendaylight/controller/cluster/datastore and
org/opendaylight/controller/cluster/raft/utils to
use just the one in raft.

Also moved the EchoActor into raft.

Change-Id: I72784a6799ae4331ab52d497d421b9a8bb98f34a
Signed-off-by: Gary Wu <gary.wu1@huawei.com>
16 files changed:
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorServerConfigurationSupportTest.java
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractLeaderElectionScenarioTest.java
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/utils/EchoActor.java [moved from opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/EchoActor.java with 73% similarity]
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/utils/MessageCollectorActor.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractTransactionProxyTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DataChangeListenerProxyTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DataChangeListenerRegistrationProxyTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DataTreeChangeListenerProxyTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ForwardingDataTreeChangeListenerTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/RoleChangeNotifierTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ThreePhaseCommitCohortProxyTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/TransactionProxyTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/ActorContextTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/DoNothingActor.java [deleted file]
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/MessageCollectorActor.java [deleted file]
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/TestUtils.java

index 36e6299..da85c67 100644 (file)
@@ -347,7 +347,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest {
 
         // Wait for leader's install snapshot and capture it
 
-        Object installSnapshot = expectFirstMatching(newFollowerCollectorActor, InstallSnapshot.class);
+        InstallSnapshot installSnapshot = expectFirstMatching(newFollowerCollectorActor, InstallSnapshot.class);
 
         // Send a second AddServer - should get queued
         JavaTestKit testKit2 = new JavaTestKit(getSystem());
index 9b2b4d3..13fd698 100644 (file)
@@ -132,9 +132,9 @@ public class AbstractLeaderElectionScenarioTest {
         }
 
         <T> T getCapturedMessage(Class<T> msgClass) throws Exception {
-            Object message = getFirstMatching(getSelf(), msgClass);
+            T message = getFirstMatching(getSelf(), msgClass);
             assertNotNull("Message of type " + msgClass + " not received", message);
-            return (T) message;
+            return message;
         }
     }
 
@@ -6,16 +6,17 @@
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 
-package org.opendaylight.controller.cluster.datastore.utils;
+package org.opendaylight.controller.cluster.raft.utils;
 
 import akka.actor.UntypedActor;
 
 /**
  * The EchoActor simply responds back with the same message that it receives
  */
-public class EchoActor extends UntypedActor{
+public class EchoActor extends UntypedActor {
 
-    @Override public void onReceive(Object message) throws Exception {
+    @Override
+    public void onReceive(Object message) throws Exception {
         getSender().tell(message, getSelf());
     }
 }
index 2c5b6cc..7e3e344 100644 (file)
@@ -31,7 +31,7 @@ import scala.concurrent.duration.FiniteDuration;
 
 public class MessageCollectorActor extends UntypedActor {
     private static final String ARE_YOU_READY = "ARE_YOU_READY";
-    private static final String GET_ALL_MESSAGES = "get-all-messages";
+    public static final String GET_ALL_MESSAGES = "messages";
     private static final String CLEAR_MESSAGES = "clear-messages";
 
     private final List<Object> messages = new ArrayList<>();
@@ -55,7 +55,7 @@ public class MessageCollectorActor extends UntypedActor {
         messages.clear();
     }
 
-    public static List<Object> getAllMessages(ActorRef actor) throws Exception {
+    private static List<Object> getAllMessages(ActorRef actor) throws Exception {
         FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
         Timeout operationTimeout = new Timeout(operationDuration);
         Future<Object> future = Patterns.ask(actor, GET_ALL_MESSAGES, operationTimeout);
@@ -78,7 +78,7 @@ public class MessageCollectorActor extends UntypedActor {
 
         for(Object message : allMessages){
             if(message.getClass().equals(clazz)){
-                return (T) message;
+                return clazz.cast(message);
             }
         }
 
@@ -177,7 +177,7 @@ public class MessageCollectorActor extends UntypedActor {
 
         for(Object message : allMessages){
             if(message.getClass().equals(clazz)){
-                output.add((T) message);
+                output.add(clazz.cast(message));
             }
         }
 
index f7a364b..2e0874d 100644 (file)
@@ -66,8 +66,8 @@ import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardS
 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
-import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
+import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
index 1a408aa..68e7e10 100644 (file)
@@ -19,10 +19,10 @@ import org.junit.Assert;
 import org.junit.Test;
 import org.opendaylight.controller.cluster.datastore.messages.DataChanged;
 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
-import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
-import org.opendaylight.controller.cluster.datastore.utils.MessageCollectorActor;
 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
+import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
+import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
 import org.opendaylight.controller.md.cluster.datastore.model.CompositeModel;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -86,7 +86,7 @@ public class DataChangeListenerProxyTest extends AbstractActorTest {
         ActorContext
             testContext = new ActorContext(getSystem(), getSystem().actorOf(Props.create(DoNothingActor.class)), new MockClusterWrapper(), new MockConfiguration());
         Object messages = testContext
-            .executeOperation(actorRef, "messages");
+            .executeOperation(actorRef, MessageCollectorActor.GET_ALL_MESSAGES);
 
         Assert.assertNotNull(messages);
 
index aa95e78..b338329 100644 (file)
@@ -38,7 +38,7 @@ import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeList
 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
-import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
+import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
index fd1ab2b..5dcc39d 100644 (file)
@@ -37,7 +37,7 @@ import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeCh
 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply;
 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
-import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
+import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
index fb8baf1..1005c2a 100644 (file)
@@ -15,7 +15,7 @@ import org.junit.Assert;
 import org.junit.Test;
 import org.mockito.Mockito;
 import org.opendaylight.controller.cluster.datastore.messages.DataTreeChanged;
-import org.opendaylight.controller.cluster.datastore.utils.MessageCollectorActor;
+import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
 
 public class ForwardingDataTreeChangeListenerTest extends AbstractActorTest {
@@ -31,7 +31,7 @@ public class ForwardingDataTreeChangeListenerTest extends AbstractActorTest {
         Collection<DataTreeCandidate> expected = Arrays.asList(Mockito.mock(DataTreeCandidate.class));
         forwardingListener.onDataTreeChanged(expected);
 
-        DataTreeChanged actual = MessageCollectorActor.expectFirstMatching(actorRef, DataTreeChanged.class);
+        DataTreeChanged actual = MessageCollectorActor.expectFirstMatching(actorRef, DataTreeChanged.class, 5000);
         Assert.assertSame(expected, actual.getChanges());
     }
 }
index 086bc3f..bec679c 100644 (file)
@@ -17,7 +17,6 @@ import akka.actor.Props;
 import akka.testkit.JavaTestKit;
 import akka.testkit.TestActorRef;
 import org.junit.Test;
-import org.opendaylight.controller.cluster.datastore.utils.MessageCollectorActor;
 import org.opendaylight.controller.cluster.notifications.LeaderStateChanged;
 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListenerReply;
@@ -25,6 +24,7 @@ import org.opendaylight.controller.cluster.notifications.RoleChangeNotification;
 import org.opendaylight.controller.cluster.notifications.RoleChangeNotifier;
 import org.opendaylight.controller.cluster.notifications.RoleChanged;
 import org.opendaylight.controller.cluster.raft.RaftState;
+import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
 
 public class RoleChangeNotifierTest extends AbstractActorTest  {
 
@@ -39,11 +39,11 @@ public class RoleChangeNotifierTest extends AbstractActorTest  {
 
             notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
 
-            RegisterRoleChangeListenerReply reply = (RegisterRoleChangeListenerReply)
+            RegisterRoleChangeListenerReply reply =
                 MessageCollectorActor.getFirstMatching(listenerActor, RegisterRoleChangeListenerReply.class);
             assertNotNull(reply);
 
-            RoleChangeNotification notification = (RoleChangeNotification)
+            RoleChangeNotification notification =
                 MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class);
             assertNull(notification);
         }};
@@ -68,11 +68,11 @@ public class RoleChangeNotifierTest extends AbstractActorTest  {
             // listener registers after role has been changed, ensure we sent the latest role change after a reply
             notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
 
-            RegisterRoleChangeListenerReply reply = (RegisterRoleChangeListenerReply)
+            RegisterRoleChangeListenerReply reply =
                 MessageCollectorActor.getFirstMatching(listenerActor, RegisterRoleChangeListenerReply.class);
             assertNotNull(reply);
 
-            RoleChangeNotification notification = (RoleChangeNotification)
+            RoleChangeNotification notification =
                 MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class);
             assertNotNull(notification);
             assertEquals(RaftState.Candidate.name(), notification.getOldRole());
index 8b56652..ce6145d 100644 (file)
@@ -42,7 +42,7 @@ import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransacti
 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
 import org.opendaylight.controller.cluster.datastore.messages.SerializableMessage;
 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
-import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
+import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
 import scala.concurrent.Future;
 import scala.concurrent.duration.Duration;
 
index fdb3683..a562563 100644 (file)
@@ -57,8 +57,8 @@ import org.opendaylight.controller.cluster.datastore.modification.DeleteModifica
 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
-import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
 import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest;
+import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
index 7e484eb..093f0fa 100644 (file)
@@ -31,7 +31,6 @@ import akka.util.Timeout;
 import com.google.common.base.Optional;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
-import com.google.common.util.concurrent.Uninterruptibles;
 import com.typesafe.config.ConfigFactory;
 import java.util.Arrays;
 import java.util.Map;
@@ -55,6 +54,8 @@ import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
 import org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound;
+import org.opendaylight.controller.cluster.raft.utils.EchoActor;
+import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -541,26 +542,9 @@ public class ActorContextTest extends AbstractActorTest{
 
             actorContext.broadcast(new TestMessage());
 
-            expectFirstMatching(shardActorRef1, TestMessage.class);
-            expectFirstMatching(shardActorRef2, TestMessage.class);
+            MessageCollectorActor.expectFirstMatching(shardActorRef1, TestMessage.class);
+            MessageCollectorActor.expectFirstMatching(shardActorRef2, TestMessage.class);
         }};
     }
 
-    private static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz) {
-        int count = 5000 / 50;
-        for(int i = 0; i < count; i++) {
-            try {
-                @SuppressWarnings("unchecked")
-                T message = (T) MessageCollectorActor.getFirstMatching(actor, clazz);
-                if(message != null) {
-                    return message;
-                }
-            } catch (Exception e) {}
-
-            Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
-        }
-
-        Assert.fail("Did not receive message of type " + clazz);
-        return null;
-    }
 }
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/DoNothingActor.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/DoNothingActor.java
deleted file mode 100644 (file)
index 819cfd0..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-/*
- * Copyright (c) 2014 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.cluster.datastore.utils;
-
-import akka.actor.UntypedActor;
-
-public class DoNothingActor extends UntypedActor {
-
-    @Override public void onReceive(Object message) throws Exception {
-    }
-}
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/MessageCollectorActor.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/MessageCollectorActor.java
deleted file mode 100644 (file)
index 18c2985..0000000
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- * Copyright (c) 2014 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.cluster.datastore.utils;
-
-import akka.actor.ActorRef;
-import akka.actor.UntypedActor;
-import akka.pattern.Patterns;
-import akka.util.Timeout;
-import com.google.common.collect.Lists;
-import com.google.common.util.concurrent.Uninterruptibles;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.TimeUnit;
-import org.junit.Assert;
-import scala.concurrent.Await;
-import scala.concurrent.Future;
-import scala.concurrent.duration.Duration;
-import scala.concurrent.duration.FiniteDuration;
-
-/**
- * MessageCollectorActor collects messages as it receives them. It can send
- * those collected messages to any sender which sends it the "messages" message
- * <p>
- *     This class would be useful as a mock to test whether messages were sent
- *     to a remote actor or not.
- * </p>
- */
-public class MessageCollectorActor extends UntypedActor {
-    private final List<Object> messages = new ArrayList<>();
-
-    @Override public void onReceive(Object message) throws Exception {
-        if(message instanceof String){
-            if("messages".equals(message)){
-                getSender().tell(new ArrayList<>(messages), getSelf());
-            }
-        } else {
-            messages.add(message);
-        }
-    }
-
-    public void clear() {
-        messages.clear();
-    }
-
-    public static List<Object> getAllMessages(ActorRef actor) throws Exception {
-        FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
-        Timeout operationTimeout = new Timeout(operationDuration);
-        Future<Object> future = Patterns.ask(actor, "messages", operationTimeout);
-
-        try {
-            return (List<Object>) Await.result(future, operationDuration);
-        } catch (Exception e) {
-            throw e;
-        }
-    }
-
-    /**
-     * Get the first message that matches the specified class
-     * @param actor
-     * @param clazz
-     * @return
-     */
-    public static Object getFirstMatching(ActorRef actor, Class<?> clazz) throws Exception {
-        List<Object> allMessages = getAllMessages(actor);
-
-        for(Object message : allMessages){
-            if(message.getClass().equals(clazz)){
-                return message;
-            }
-        }
-
-        return null;
-    }
-
-    public static List<Object> getAllMatching(ActorRef actor, Class<?> clazz) throws Exception {
-        List<Object> allMessages = getAllMessages(actor);
-
-        List<Object> output = Lists.newArrayList();
-
-        for(Object message : allMessages){
-            if(message.getClass().equals(clazz)){
-                output.add(message);
-            }
-        }
-
-        return output;
-    }
-
-    public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz) {
-        int count = 5000 / 50;
-        for(int i = 0; i < count; i++) {
-            try {
-                T message = (T) getFirstMatching(actor, clazz);
-                if(message != null) {
-                    return message;
-                }
-            } catch (Exception e) {}
-
-            Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
-        }
-
-        Assert.fail("Did not receive message of type " + clazz);
-        return null;
-    }
-}
index a2c44b0..8a141fb 100644 (file)
@@ -13,6 +13,8 @@ import akka.actor.ActorSystem;
 import akka.actor.Props;
 import java.util.List;
 import org.junit.Assert;
+import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
+import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
 
 public class TestUtils {
 
@@ -20,7 +22,7 @@ public class TestUtils {
         ActorContext testContext = new ActorContext(actorSystem, actorSystem.actorOf(
             Props.create(DoNothingActor.class)), new MockClusterWrapper(), new MockConfiguration());
         Object messages = testContext
-            .executeOperation(actorRef, "messages");
+            .executeOperation(actorRef, MessageCollectorActor.GET_ALL_MESSAGES);
 
         Assert.assertNotNull(messages);
 

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.