Skip to content

Commit

Permalink
Test combinations of rotation, skipping and pruning.
Browse files Browse the repository at this point in the history
  • Loading branch information
apcj committed Jul 14, 2016
1 parent 6a64f6a commit 82c5f16
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 6 deletions.
Expand Up @@ -19,10 +19,10 @@
*/
package org.neo4j.coreedge.raft.log;

import org.junit.Test;

import java.io.IOException;

import org.junit.Test;

import org.neo4j.coreedge.raft.ReplicatedString;

import static org.hamcrest.CoreMatchers.equalTo;
Expand All @@ -32,6 +32,7 @@
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import static org.neo4j.coreedge.raft.ReplicatedInteger.valueOf;
import static org.neo4j.coreedge.raft.log.RaftLogHelper.hasNoContent;
import static org.neo4j.coreedge.raft.log.RaftLogHelper.readLogEntry;
Expand Down Expand Up @@ -356,6 +357,30 @@ public void shouldBeAbleToAppendAfterSkipping() throws Exception
assertThat( readLogEntry( log, newEntryIndex ).content(), is( valueOf( newContentValue ) ) );
}

@Test
public void pruneShouldNotChangePrevIndexAfterSkipping() throws Exception
{
// given
RaftLog log = createRaftLog();


long term = 0;
for ( int i = 0; i < 2000; i++ )
{
log.append( new RaftLogEntry( term, valueOf( i ) ) );
}

long skipIndex = 3000;
log.skip( skipIndex, term );
assertEquals( skipIndex, log.prevIndex() );

// when
log.prune( skipIndex );

// then
assertEquals( skipIndex, log.prevIndex() );
}

@Test
public void shouldProperlyReportExistenceOfIndexesAfterSkipping() throws Exception
{
Expand Down Expand Up @@ -411,13 +436,14 @@ public void shouldThrowExceptionWhenReadingAnEntryWhichHasBeenPruned() throws Ex
assertEquals( -1L, term );
}

private ReplicatedString string(int numberOfCharacters) {
StringBuffer s = new StringBuffer( );
private ReplicatedString string( int numberOfCharacters )
{
StringBuilder builder = new StringBuilder();
for ( int i = 0; i < numberOfCharacters; i++ )
{
s.append( String.valueOf( i ) );
builder.append( String.valueOf( i ) );
}
return ReplicatedString.valueOf( s.toString() ) ;
return ReplicatedString.valueOf( builder.toString() );
}

// TODO: Test what happens when the log has rotated, *not* pruned and then skipping happens which causes
Expand Down
@@ -0,0 +1,87 @@
package org.neo4j.coreedge.raft.log.segmented;

import java.io.File;

import org.junit.Test;

import org.neo4j.coreedge.raft.log.DummyRaftableContentSerializer;
import org.neo4j.coreedge.raft.log.RaftLog;
import org.neo4j.coreedge.raft.log.RaftLogEntry;
import org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.time.FakeClock;

import static org.junit.Assert.assertEquals;

import static org.neo4j.coreedge.raft.ReplicatedInteger.valueOf;
import static org.neo4j.coreedge.raft.log.segmented.SegmentedRaftLog.SEGMENTED_LOG_DIRECTORY_NAME;

public class SegmentedRaftLogRotationTruncationPruneTest
{
@Test
public void shouldPruneAwaySingleEntriesIfRotationHappenedEveryEntry() throws Exception
{
// given
RaftLog log = createRaftLog();

long term = 0;
for ( int i = 0; i < 10; i++ )
{
log.append( new RaftLogEntry( term, valueOf( i ) ) );
}

assertEquals( -1, log.prevIndex() );
for ( int i = 0; i < 9; i++ )
{
log.prune( i );
assertEquals( i, log.prevIndex() );
}
log.prune( 9 );
assertEquals( 8, log.prevIndex() );
}

@Test
public void shouldPruneAwaySingleEntriesAfterTruncationIfRotationHappenedEveryEntry() throws Exception
{
// given
RaftLog log = createRaftLog();

long term = 0;
for ( int i = 0; i < 10; i++ )
{
log.append( new RaftLogEntry( term, valueOf( i ) ) );
}

log.truncate( 9 );
log.truncate( 8 );
log.truncate( 7 );

assertEquals( -1, log.prevIndex() );
for ( int i = 0; i <= 5; i++ )
{
log.prune( i );
assertEquals( i, log.prevIndex() );
}
for ( int i = 5; i < 10; i++ )
{
log.prune( i );
assertEquals( 5, log.prevIndex() );
}
}

private RaftLog createRaftLog() throws Exception
{
FileSystemAbstraction fileSystem = new EphemeralFileSystemAbstraction();

File directory = new File( SEGMENTED_LOG_DIRECTORY_NAME );
fileSystem.mkdir( directory );

SegmentedRaftLog newRaftLog = new SegmentedRaftLog( fileSystem, directory, 1,
new DummyRaftableContentSerializer(),
NullLogProvider.getInstance(), "1 entries", 8, new FakeClock() );

newRaftLog.start();
return newRaftLog;
}
}

0 comments on commit 82c5f16

Please sign in to comment.