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=a47cf118963e1698c68ef2c2706a44c2baaeeb59;hb=f12d62d2dc28a883c1f1b38df7d72a9142c2abfb;hp=45fd26c930736dd0d17ff706154b6252246bdd0e;hpb=254381e31fb8a30db6428e8ebebdbb4416aa1c1a;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 45fd26c930..a47cf11896 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; @@ -52,7 +54,6 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest 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) { @@ -112,20 +113,14 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest } @Override - protected void createSnapshot() { - if(snapshot != null) { - getSelf().tell(new CaptureSnapshotReply(snapshot), ActorRef.noSender()); + public void createSnapshot(ActorRef actorRef) { + 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 +154,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 +181,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 +189,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 +225,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) {