### Question:
TruncateLogCommand implements RaftLogCommand { @Override public void applyTo( RaftLog raftLog, Log log ) throws IOException { raftLog.truncate( fromIndex ); } TruncateLogCommand( long fromIndex ); @Override void dispatch( Handler handler ); @Override void applyTo( RaftLog raftLog, Log log ); @Override void applyTo( InFlightCache inFlightCache, Log log ); @Override boolean equals( Object o ); @Override int hashCode(); @Override String toString(); final long fromIndex; }### Answer:
@Test public void applyTo() { AssertableLogProvider logProvider = new AssertableLogProvider(); Log log = logProvider.getLog( getClass() ); long fromIndex = 2L; TruncateLogCommand truncateLogCommand = new TruncateLogCommand( fromIndex ); InFlightCache inFlightCache = new ConsecutiveInFlightCache(); inFlightCache.put( 0L, new RaftLogEntry( 0L, valueOf( 0 ) ) ); inFlightCache.put( 1L, new RaftLogEntry( 1L, valueOf( 1 ) ) ); inFlightCache.put( 2L, new RaftLogEntry( 2L, valueOf( 2 ) ) ); inFlightCache.put( 3L, new RaftLogEntry( 3L, valueOf( 3 ) ) ); truncateLogCommand.applyTo( inFlightCache, log ); assertNotNull( inFlightCache.get( 0L ) ); assertNotNull( inFlightCache.get( 1L ) ); assertNull( inFlightCache.get( 2L ) ); assertNull( inFlightCache.get( 3L ) ); logProvider.assertAtLeastOnce( inLog( getClass() ) .debug( "Start truncating in-flight-map from index %d. Current map:%n%s", fromIndex, inFlightCache ) ); }
@Test public void shouldTruncateWithGaps() { long fromIndex = 1L; TruncateLogCommand truncateLogCommand = new TruncateLogCommand( fromIndex ); InFlightCache inFlightCache = new ConsecutiveInFlightCache(); inFlightCache.put( 0L, new RaftLogEntry( 0L, valueOf( 0 ) ) ); inFlightCache.put( 2L, new RaftLogEntry( 1L, valueOf( 1 ) ) ); inFlightCache.put( 4L, new RaftLogEntry( 2L, valueOf( 2 ) ) ); truncateLogCommand.applyTo( inFlightCache, NullLog.getInstance() ); inFlightCache.put( 1L, new RaftLogEntry( 3L, valueOf( 1 ) ) ); inFlightCache.put( 2L, new RaftLogEntry( 4L, valueOf( 2 ) ) ); assertNotNull( inFlightCache.get( 1L ) ); assertNotNull( inFlightCache.get( 2L ) ); }