X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FAbstractReplicatedLogImplTest.java;h=ffd8edfbe15fa3aad7a9f237a053b23017946337;hp=913665861d6f694dbb01c51d96978c05d6e9c5ea;hb=e3918142bf119759d092af5c7fa72807b3a467e0;hpb=574102cee2e4d0f98042dcd6ca49e6574313160c diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractReplicatedLogImplTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractReplicatedLogImplTest.java index 913665861d..ffd8edfbe1 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractReplicatedLogImplTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractReplicatedLogImplTest.java @@ -7,21 +7,19 @@ */ package org.opendaylight.controller.cluster.raft; -import junit.framework.Assert; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload; -import static org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockReplicatedLogEntry; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload; +import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockReplicatedLogEntry; /** * */ @@ -65,7 +63,7 @@ public class AbstractReplicatedLogImplTest { // now create a snapshot of 3 entries, with 1 unapplied entry left in the log // It removes the entries which have made it to snapshot // and updates the snapshot index and term - Map state = takeSnapshot(3); + Map state = takeSnapshot(3); // check the values after the snapshot. // each index value passed in the test is the logical index (log entry index) @@ -130,9 +128,36 @@ public class AbstractReplicatedLogImplTest { } + @Test + public void testSnapshotPreCommit() { + replicatedLogImpl.append(new MockReplicatedLogEntry(2, 4, new MockPayload("E"))); + replicatedLogImpl.append(new MockReplicatedLogEntry(2, 5, new MockPayload("F"))); + replicatedLogImpl.append(new MockReplicatedLogEntry(3, 6, new MockPayload("G"))); + replicatedLogImpl.append(new MockReplicatedLogEntry(3, 7, new MockPayload("H"))); + + replicatedLogImpl.snapshotPreCommit(4, 3); + assertEquals(3, replicatedLogImpl.size()); + assertEquals(4, replicatedLogImpl.getSnapshotIndex()); + + replicatedLogImpl.snapshotPreCommit(6, 3); + assertEquals(1, replicatedLogImpl.size()); + assertEquals(6, replicatedLogImpl.getSnapshotIndex()); + + replicatedLogImpl.snapshotPreCommit(7, 3); + assertEquals(0, replicatedLogImpl.size()); + assertEquals(7, replicatedLogImpl.getSnapshotIndex()); + + //running it again on an empty list should not throw exception + replicatedLogImpl.snapshotPreCommit(7, 3); + assertEquals(0, replicatedLogImpl.size()); + assertEquals(7, replicatedLogImpl.getSnapshotIndex()); + + + } + // create a snapshot for test - public Map takeSnapshot(int numEntries) { - Map map = new HashMap(numEntries); + public Map takeSnapshot(final int numEntries) { + Map map = new HashMap<>(numEntries); List entries = replicatedLogImpl.getEntriesTill(numEntries); for (ReplicatedLogEntry entry : entries) { map.put(entry.getIndex(), entry.getData().toString()); @@ -149,22 +174,19 @@ public class AbstractReplicatedLogImplTest { } class MockAbstractReplicatedLogImpl extends AbstractReplicatedLogImpl { @Override - public void appendAndPersist(ReplicatedLogEntry replicatedLogEntry) { + public void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry) { } @Override - public void removeFromAndPersist(long index) { - } - - public void setSnapshotIndex(long snapshotIndex) { - this.snapshotIndex = snapshotIndex; + public void removeFromAndPersist(final long index) { } - public void setSnapshotTerm(long snapshotTerm) { - this.snapshotTerm = snapshotTerm; + @Override + public int dataSize() { + return -1; } - public List getEntriesTill(int index) { + public List getEntriesTill(final int index) { return journal.subList(0, index); } }