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%2FAbstractRaftActorIntegrationTest.java;h=74440b5c24c37b7c22ba6ca8fb6ee1a3eed16c38;hb=refs%2Fchanges%2F85%2F22285%2F4;hp=3c6c8281fb734b0d378963c831c2f462fbcc3182;hpb=68fb550b416dddd0a50e0110add0a4ae9b706758;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractRaftActorIntegrationTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractRaftActorIntegrationTest.java index 3c6c8281fb..74440b5c24 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractRaftActorIntegrationTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractRaftActorIntegrationTest.java @@ -7,9 +7,10 @@ */ package org.opendaylight.controller.cluster.raft; -import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import akka.actor.ActorRef; +import akka.actor.InvalidActorNameException; import akka.actor.PoisonPill; import akka.actor.Props; import akka.actor.Terminated; @@ -20,6 +21,8 @@ import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.base.Supplier; import com.google.common.collect.ImmutableMap; +import com.google.common.util.concurrent.Uninterruptibles; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; @@ -27,7 +30,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import org.junit.After; import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload; -import org.opendaylight.controller.cluster.raft.RaftActorTest.MockRaftActor; import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries; import org.opendaylight.controller.cluster.raft.base.messages.ApplyState; import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply; @@ -48,11 +50,28 @@ import scala.concurrent.duration.FiniteDuration; */ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest { + public static class SetPeerAddress { + private final String peerId; + private final String peerAddress; + + public SetPeerAddress(String peerId, String peerAddress) { + this.peerId = peerId; + this.peerAddress = peerAddress; + } + + public String getPeerId() { + return peerId; + } + + public String getPeerAddress() { + return peerAddress; + } + } + public static class TestRaftActor extends MockRaftActor { private final TestActorRef collectorActor; private final Map, Boolean> dropMessages = new ConcurrentHashMap<>(); - private volatile byte[] snapshot; private TestRaftActor(String id, Map peerAddresses, ConfigParams config, TestActorRef collectorActor) { @@ -96,6 +115,12 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest return; } + if(message instanceof SetPeerAddress) { + setPeerAddress(((SetPeerAddress) message).getPeerId().toString(), + ((SetPeerAddress) message).getPeerAddress()); + return; + } + try { if(!dropMessages.containsKey(message.getClass())) { super.handleCommand(message); @@ -113,19 +138,13 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest @Override public void createSnapshot(ActorRef actorRef) { - if(snapshot != null) { - getSelf().tell(new CaptureSnapshotReply(snapshot), ActorRef.noSender()); + try { + actorRef.tell(new CaptureSnapshotReply(RaftActorTest.fromObject(getState()).toByteArray()), actorRef); + } catch (Exception e) { + e.printStackTrace(); } } - @Override - public void applyRecoverySnapshot(byte[] bytes) { - } - - void setSnapshot(byte[] snapshot) { - this.snapshot = snapshot; - } - public ActorRef collectorActor() { return collectorActor; } @@ -159,6 +178,8 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest protected long initialTerm = 5; protected long currentTerm; + protected List expSnapshotState = new ArrayList<>(); + @After public void tearDown() { InMemoryJournal.clear(); @@ -184,7 +205,7 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest } protected void waitUntilLeader(ActorRef actorRef) { - RaftActorTest.RaftActorTestKit.waitUntilLeader(actorRef); + RaftActorTestKit.waitUntilLeader(actorRef); } protected TestActorRef newTestRaftActor(String id, Map peerAddresses, @@ -192,9 +213,21 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest TestActorRef collectorActor = factory.createTestActor( MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), factory.generateActorId(id + "-collector")); - return factory.createTestActor(TestRaftActor.props(id, - peerAddresses != null ? peerAddresses : Collections.emptyMap(), - configParams, collectorActor), id); + + InvalidActorNameException lastEx = null; + for(int i = 0; i < 10; i++) { + try { + return factory.createTestActor(TestRaftActor.props(id, + peerAddresses != null ? peerAddresses : Collections.emptyMap(), + configParams, collectorActor), id); + } catch (InvalidActorNameException e) { + lastEx = e; + Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); + } + } + + assertNotNull(lastEx); + throw lastEx; } protected void killActor(TestActorRef leaderActor) { @@ -216,13 +249,20 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest }); } + @SuppressWarnings("unchecked") protected void verifySnapshot(String prefix, Snapshot snapshot, long lastAppliedTerm, - int lastAppliedIndex, long lastTerm, long lastIndex, byte[] data) { + int lastAppliedIndex, long lastTerm, long lastIndex) + throws Exception { assertEquals(prefix + " Snapshot getLastAppliedTerm", lastAppliedTerm, snapshot.getLastAppliedTerm()); assertEquals(prefix + " Snapshot getLastAppliedIndex", lastAppliedIndex, snapshot.getLastAppliedIndex()); assertEquals(prefix + " Snapshot getLastTerm", lastTerm, snapshot.getLastTerm()); assertEquals(prefix + " Snapshot getLastIndex", lastIndex, snapshot.getLastIndex()); - assertArrayEquals(prefix + " Snapshot getState", data, snapshot.getState()); + + List actualState = (List)MockRaftActor.toObject(snapshot.getState()); + assertEquals(prefix + " Snapshot getState size", expSnapshotState.size(), actualState.size()); + for(int i = 0; i < expSnapshotState.size(); i++) { + assertEquals(prefix + " Snapshot state " + i, expSnapshotState.get(i), actualState.get(i)); + } } protected void verifyPersistedJournal(String persistenceId, List expJournal) {