X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FRaftActorTestKit.java;h=6386d6c6ba1e7a9453161c3f67caa146e1dcd543;hb=f33beecf2a10955a9219757529ba3017079816cc;hp=3e747e387ee1f881ba0141f1042cd837430b1ae2;hpb=f9a9cd1ea40d2477ccb16b03c71a87595226595a;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorTestKit.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorTestKit.java index 3e747e387e..6386d6c6ba 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorTestKit.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorTestKit.java @@ -10,77 +10,61 @@ package org.opendaylight.controller.cluster.raft; import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.pattern.Patterns; -import akka.testkit.JavaTestKit; +import akka.testkit.javadsl.EventFilter; +import akka.testkit.javadsl.TestKit; import akka.util.Timeout; -import com.google.common.base.Optional; import com.google.common.util.concurrent.Uninterruptibles; -import java.util.Collections; +import java.util.Optional; import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import org.junit.Assert; import org.opendaylight.controller.cluster.raft.client.messages.FindLeader; import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import scala.concurrent.Await; import scala.concurrent.Future; -import scala.concurrent.duration.Duration; import scala.concurrent.duration.FiniteDuration; -public class RaftActorTestKit extends JavaTestKit { +public class RaftActorTestKit extends TestKit { + private static final Logger LOG = LoggerFactory.getLogger(RaftActorTestKit.class); private final ActorRef raftActor; - public RaftActorTestKit(ActorSystem actorSystem, String actorName) { + public RaftActorTestKit(final ActorSystem actorSystem, final String actorName) { super(actorSystem); - - raftActor = this.getSystem().actorOf(MockRaftActor.props(actorName, - Collections.emptyMap(), Optional.absent()), actorName); - + raftActor = this.getSystem().actorOf(MockRaftActor.builder().id(actorName).props(), actorName); } - public ActorRef getRaftActor() { return raftActor; } - public boolean waitForLogMessage(final Class logEventClass, String message){ + public boolean waitForLogMessage(final Class logEventClass, final String message) { // Wait for a specific log message to show up - return - new JavaTestKit.EventFilter(logEventClass - ) { - @Override - protected Boolean run() { - return true; - } - }.from(raftActor.path().toString()) - .message(message) - .occurrences(1).exec(); - - + return new EventFilter(logEventClass, getSystem()).from(raftActor.path().toString()).message(message) + .occurrences(1).intercept(() -> Boolean.TRUE); } - protected void waitUntilLeader(){ + protected void waitUntilLeader() { waitUntilLeader(raftActor); } - public static void waitUntilLeader(ActorRef actorRef) { - FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS); - for(int i = 0; i < 20 * 5; i++) { - Future future = Patterns.ask(actorRef, new FindLeader(), new Timeout(duration)); + @SuppressWarnings("checkstyle:IllegalCatch") + public static void waitUntilLeader(final ActorRef actorRef) { + FiniteDuration duration = FiniteDuration.create(100, TimeUnit.MILLISECONDS); + for (int i = 0; i < 20 * 5; i++) { + Future future = Patterns.ask(actorRef, FindLeader.INSTANCE, new Timeout(duration)); try { - FindLeaderReply resp = (FindLeaderReply) Await.result(future, duration); - if(resp.getLeaderActor() != null) { + final Optional maybeLeader = ((FindLeaderReply)Await.result(future, duration)).getLeaderActor(); + if (maybeLeader.isPresent()) { return; } - } catch(TimeoutException e) { - } catch(Exception e) { - System.err.println("FindLeader threw ex"); - e.printStackTrace(); + } catch (Exception e) { + LOG.error("FindLeader failed", e); } - Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS); } Assert.fail("Leader not found for actorRef " + actorRef.path()); } - -} \ No newline at end of file +}