package org.opendaylight.controller.cluster.raft.client.messages;
+import com.google.common.annotations.VisibleForTesting;
import java.io.Serializable;
+import java.util.Optional;
+import javax.annotation.Nullable;
-public class FindLeaderReply implements Serializable {
+/**
+ * Reply to {@link FindLeader} message, containing the address of the leader actor, as known to the raft actor which
+ * sent the message. If the responding actor does not have knowledge of the leader, {@link #getLeaderActor()} will
+ * return {@link Optional#empty()}.
+ *
+ * This message is intended for testing purposes only.
+ */
+@VisibleForTesting
+public final class FindLeaderReply implements Serializable {
private static final long serialVersionUID = 1L;
private final String leaderActor;
- public FindLeaderReply(String leaderActor) {
+ public FindLeaderReply(@Nullable final String leaderActor) {
this.leaderActor = leaderActor;
}
- public String getLeaderActor() {
- return leaderActor;
+ public Optional<String> getLeaderActor() {
+ return Optional.ofNullable(leaderActor);
}
}
import akka.testkit.JavaTestKit;
import akka.util.Timeout;
import com.google.common.util.concurrent.Uninterruptibles;
+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 {
+ private static final Logger LOG = LoggerFactory.getLogger(RaftActorTestKit.class);
private final ActorRef raftActor;
public RaftActorTestKit(ActorSystem actorSystem, String actorName) {
for(int i = 0; i < 20 * 5; i++) {
Future<Object> future = Patterns.ask(actorRef, FindLeader.INSTANCE, new Timeout(duration));
try {
- FindLeaderReply resp = (FindLeaderReply) Await.result(future, duration);
- if(resp.getLeaderActor() != null) {
+ final Optional<String> 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();
+ LOG.error("FindLeader failed", e);
}
-
Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
shard.tell(FindLeader.INSTANCE, getRef());
final FindLeaderReply findLeadeReply =
expectMsgClass(duration("5 seconds"), FindLeaderReply.class);
- assertNull("Expected the shard not to be the leader", findLeadeReply.getLeaderActor());
+ assertFalse("Expected the shard not to be the leader", findLeadeReply.getLeaderActor().isPresent());
// Signal the onChangeListenerRegistered latch to tell the thread above to proceed
// with the election process.
shard.tell(FindLeader.INSTANCE, getRef());
final FindLeaderReply findLeadeReply =
expectMsgClass(duration("5 seconds"), FindLeaderReply.class);
- assertNull("Expected the shard not to be the leader", findLeadeReply.getLeaderActor());
+ assertFalse("Expected the shard not to be the leader", findLeadeReply.getLeaderActor().isPresent());
writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
import akka.testkit.JavaTestKit;
import akka.util.Timeout;
import com.google.common.util.concurrent.Uninterruptibles;
+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 ShardTestKit extends JavaTestKit {
+ private static final Logger LOG = LoggerFactory.getLogger(ShardTestKit.class);
public ShardTestKit(ActorSystem actorSystem) {
super(actorSystem);
for(int i = 0; i < 20 * 5; i++) {
Future<Object> future = Patterns.ask(shard, FindLeader.INSTANCE, new Timeout(duration));
try {
- FindLeaderReply resp = (FindLeaderReply)Await.result(future, duration);
- if(resp.getLeaderActor() != null) {
- return resp.getLeaderActor();
+ final Optional<String> maybeLeader = ((FindLeaderReply)Await.result(future, duration)).getLeaderActor();
+ if (maybeLeader.isPresent()) {
+ return maybeLeader.get();
}
} catch(TimeoutException e) {
+ LOG.trace("FindLeader timed out", e);
} catch(Exception e) {
- System.err.println("FindLeader threw ex");
- e.printStackTrace();
+ LOG.error("FindLeader failed", e);
}
-
Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
}
for(int i = 0; i < 20 * 5; i++) {
Future<Object> future = Patterns.ask(shard, FindLeader.INSTANCE, new Timeout(duration));
try {
- FindLeaderReply resp = (FindLeaderReply)Await.result(future, duration);
- if(resp.getLeaderActor() == null) {
+ final Optional<String> maybeLeader = ((FindLeaderReply)Await.result(future, duration)).getLeaderActor();
+ if (!maybeLeader.isPresent()) {
return;
}
- lastResponse = resp.getLeaderActor();
+ lastResponse = maybeLeader.get();
} catch(TimeoutException e) {
lastResponse = e;
} catch(Exception e) {
- System.err.println("FindLeader threw ex");
- e.printStackTrace();
+ LOG.error("FindLeader failed", e);
lastResponse = e;
}