### Question:
InFlightLogEntryReader implements AutoCloseable { public RaftLogEntry get( long logIndex ) throws IOException { RaftLogEntry entry = null; if ( useCache ) { entry = inFlightCache.get( logIndex ); } if ( entry == null ) { useCache = false; entry = getUsingCursor( logIndex ); } if ( pruneAfterRead ) { inFlightCache.prune( logIndex ); } return entry; } InFlightLogEntryReader( ReadableRaftLog raftLog, InFlightCache inFlightCache,
            boolean pruneAfterRead ); RaftLogEntry get( long logIndex ); @Override void close();  }### Answer:
@Test public void shouldUseTheCacheWhenTheIndexIsPresent() throws Exception { InFlightLogEntryReader reader = new InFlightLogEntryReader( raftLog, inFlightCache, clearCache ); startingFromIndexReturnEntries( inFlightCache, logIndex, entry ); startingFromIndexReturnEntries( raftLog, -1, null ); RaftLogEntry raftLogEntry = reader.get( logIndex ); assertEquals( entry, raftLogEntry ); verify( inFlightCache ).get( logIndex ); assertCacheIsUpdated( inFlightCache, logIndex ); verifyNoMoreInteractions( inFlightCache ); verifyZeroInteractions( raftLog ); }
@Test public void shouldUseTheRaftLogWhenTheIndexIsNotPresent() throws Exception { InFlightLogEntryReader reader = new InFlightLogEntryReader( raftLog, inFlightCache, clearCache ); startingFromIndexReturnEntries( inFlightCache, logIndex, null ); startingFromIndexReturnEntries( raftLog, logIndex, entry ); RaftLogEntry raftLogEntry = reader.get( logIndex ); assertEquals( entry, raftLogEntry ); verify( inFlightCache ).get( logIndex ); verify( raftLog ).getEntryCursor( logIndex ); assertCacheIsUpdated( inFlightCache, logIndex ); verifyNoMoreInteractions( inFlightCache ); verifyNoMoreInteractions( raftLog ); }
@Test public void shouldNeverUseMapAgainAfterHavingFallenBackToTheRaftLog() throws Exception { InFlightLogEntryReader reader = new InFlightLogEntryReader( raftLog, inFlightCache, clearCache ); startingFromIndexReturnEntries( inFlightCache, logIndex, entry, null, mock( RaftLogEntry.class ) ); RaftLogEntry[] entries = {entry, mock( RaftLogEntry.class ), mock( RaftLogEntry.class )}; startingFromIndexReturnEntries( raftLog, logIndex + 1, entries[1], entries[2] ); for ( int offset = 0; offset < 3; offset++ ) { RaftLogEntry raftLogEntry = reader.get( offset + logIndex ); assertEquals( entries[offset], raftLogEntry ); if ( offset <= 1 ) { verify( inFlightCache ).get( offset + logIndex ); } if ( offset == 1 ) { verify( raftLog ).getEntryCursor( offset + logIndex ); } assertCacheIsUpdated( inFlightCache, offset + logIndex ); } verifyNoMoreInteractions( inFlightCache ); verifyNoMoreInteractions( raftLog ); }