Reduce use of scala.concurrent.duration.Duration
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / utils / MessageCollectorActor.java
index 68d6b619cd43bc6aa2d1ce582e32b2eb0bb03d4e..8d3377310581571aad360eb06c4cc7137be80340 100644 (file)
@@ -5,19 +5,18 @@
  * 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.raft.utils;
 
 import akka.actor.ActorRef;
 import akka.actor.Props;
-import akka.actor.UntypedActor;
+import akka.actor.UntypedAbstractActor;
+import akka.dispatch.ControlMessage;
 import akka.pattern.Patterns;
 import akka.util.Timeout;
 import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
 import com.google.common.base.Throwables;
 import com.google.common.collect.Iterables;
-import com.google.common.collect.Lists;
 import com.google.common.util.concurrent.Uninterruptibles;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -27,42 +26,49 @@ import java.util.concurrent.TimeoutException;
 import org.junit.Assert;
 import scala.concurrent.Await;
 import scala.concurrent.Future;
-import scala.concurrent.duration.Duration;
 import scala.concurrent.duration.FiniteDuration;
 
-public class MessageCollectorActor extends UntypedActor {
+public class MessageCollectorActor extends UntypedAbstractActor {
     private static final String ARE_YOU_READY = "ARE_YOU_READY";
     public static final String GET_ALL_MESSAGES = "messages";
-    private static final String CLEAR_MESSAGES = "clear-messages";
+
+    private static final Object CLEAR_MESSAGES = new ControlMessage() {
+        @Override
+        public String toString() {
+            return "clear-messages";
+        }
+    };
 
     private final List<Object> messages = new ArrayList<>();
 
-    @Override public void onReceive(Object message) throws Exception {
+    @Override public void onReceive(final Object message) throws Exception {
         if (ARE_YOU_READY.equals(message)) {
             getSender().tell("yes", getSelf());
         } else if (GET_ALL_MESSAGES.equals(message)) {
             getSender().tell(new ArrayList<>(messages), getSelf());
         } else if (CLEAR_MESSAGES.equals(message)) {
-            clear();
+            messages.clear();
         } else if (message != null) {
             messages.add(message);
         }
     }
 
-    public void clear() {
-        messages.clear();
-    }
-
-    @SuppressWarnings("unchecked")
-    private static List<Object> getAllMessages(ActorRef actor) throws Exception {
-        FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
+    @SuppressWarnings({"unchecked", "checkstyle:illegalCatch"})
+    public static List<Object> getAllMessages(final ActorRef actor) {
+        FiniteDuration operationDuration = FiniteDuration.create(5, TimeUnit.SECONDS);
         Timeout operationTimeout = new Timeout(operationDuration);
         Future<Object> future = Patterns.ask(actor, GET_ALL_MESSAGES, operationTimeout);
 
-        return (List<Object>) Await.result(future, operationDuration);
+        try {
+            return (List<Object>) Await.result(future, operationDuration);
+        } catch (RuntimeException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
     }
 
-    public static void clearMessages(ActorRef actor) {
+    public static void clearMessages(final ActorRef actor) {
         actor.tell(CLEAR_MESSAGES, ActorRef.noSender());
     }
 
@@ -73,7 +79,7 @@ public class MessageCollectorActor extends UntypedActor {
      * @param clazz the class to match
      * @return the first matching message
      */
-    public static <T> T getFirstMatching(ActorRef actor, Class<T> clazz) throws Exception {
+    public static <T> T getFirstMatching(final ActorRef actor, final Class<T> clazz) {
         List<Object> allMessages = getAllMessages(actor);
 
         for (Object message : allMessages) {
@@ -86,13 +92,13 @@ public class MessageCollectorActor extends UntypedActor {
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
-    public static <T> List<T> expectMatching(ActorRef actor, Class<T> clazz, int count) {
+    public static <T> List<T> expectMatching(final ActorRef actor, final Class<T> clazz, final int count) {
         return expectMatching(actor, clazz, count, msg -> true);
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
-    public static <T> List<T> expectMatching(ActorRef actor, Class<T> clazz, int count,
-            Predicate<T> matcher) {
+    public static <T> List<T> expectMatching(final ActorRef actor, final Class<T> clazz, final int count,
+            final Predicate<T> matcher) {
         int timeout = 5000;
         Exception lastEx = null;
         List<T> messages = Collections.emptyList();
@@ -116,12 +122,12 @@ public class MessageCollectorActor extends UntypedActor {
                 clazz, messages.size(), messages), lastEx);
     }
 
-    public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz) {
+    public static <T> T expectFirstMatching(final ActorRef actor, final Class<T> clazz) {
         return expectFirstMatching(actor, clazz, 5000);
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
-    public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz, long timeout) {
+    public static <T> T expectFirstMatching(final ActorRef actor, final Class<T> clazz, final long timeout) {
         Exception lastEx = null;
         int count = (int) (timeout / 50);
         for (int i = 0; i < count; i++) {
@@ -139,11 +145,12 @@ public class MessageCollectorActor extends UntypedActor {
             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
         }
 
-        throw new AssertionError("Did not receive message of type " + clazz, lastEx);
+        throw new AssertionError(actor + ": Did not receive message of type " + clazz + ", Actual received was "
+            + getAllMessages(actor), lastEx);
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
-    public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz, Predicate<T> matcher) {
+    public static <T> T expectFirstMatching(final ActorRef actor, final Class<T> clazz, final Predicate<T> matcher) {
         int timeout = 5000;
         Exception lastEx = null;
         T lastMessage = null;
@@ -170,12 +177,12 @@ public class MessageCollectorActor extends UntypedActor {
                 clazz, lastMessage), lastEx);
     }
 
-    public static <T> void assertNoneMatching(ActorRef actor, Class<T> clazz) {
+    public static <T> void assertNoneMatching(final ActorRef actor, final Class<T> clazz) {
         assertNoneMatching(actor, clazz, 5000);
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
-    public static <T> void assertNoneMatching(ActorRef actor, Class<T> clazz, long timeout) {
+    public static <T> void assertNoneMatching(final ActorRef actor, final Class<T> clazz, final long timeout) {
         Exception lastEx = null;
         int count = (int) (timeout / 50);
         for (int i = 0; i < count; i++) {
@@ -195,17 +202,18 @@ public class MessageCollectorActor extends UntypedActor {
         }
 
         if (lastEx != null) {
-            Throwables.propagate(lastEx);
+            Throwables.throwIfUnchecked(lastEx);
+            throw new RuntimeException(lastEx);
         }
 
         return;
     }
 
 
-    public static <T> List<T> getAllMatching(ActorRef actor, Class<T> clazz) throws Exception {
+    public static <T> List<T> getAllMatching(final ActorRef actor, final Class<T> clazz) {
         List<Object> allMessages = getAllMessages(actor);
 
-        List<T> output = Lists.newArrayList();
+        List<T> output = new ArrayList<>();
 
         for (Object message : allMessages) {
             if (message.getClass().equals(clazz)) {
@@ -216,9 +224,9 @@ public class MessageCollectorActor extends UntypedActor {
         return output;
     }
 
-    public static void waitUntilReady(ActorRef actor) throws Exception {
+    public static void waitUntilReady(final ActorRef actor) throws TimeoutException, InterruptedException {
         long timeout = 500;
-        FiniteDuration duration = Duration.create(timeout, TimeUnit.MILLISECONDS);
+        FiniteDuration duration = FiniteDuration.create(timeout, TimeUnit.MILLISECONDS);
         for (int i = 0; i < 10; i++) {
             try {
                 Await.ready(Patterns.ask(actor, ARE_YOU_READY, timeout), duration);