@Test
public void shouldNotCommitEntriesUnlessAnEntryFromTheCurrentTermIsCommitted() throws StorageException, RPCException {

    insertIntoLog(
            NOOP(1, 1),
            NOOP(2, 1),
            NOOP(3, 2)
    );
    store.setCommitIndex(1);
    becomeLeaderInTerm(3, false);
    Collection<AppendEntries> appendEntriesRequests;
    // after we become a leader we should automatically
    // generate a NOOP in this term and send it out in our first
    // "I am leader" message
    assertThatLogContains(
            SENTINEL(),
            NOOP(1, 1),
            NOOP(2, 1),
            NOOP(3, 2),
            NOOP(4, 3)
    );
    // check the "I am leader messages)
    appendEntriesRequests = getRPCs(4, AppendEntries.class);
    assertThatAppendEntriesHaveValues(appendEntriesRequests, 3, 1, 3, 2, NOOP(4, 3));
    assertThatNoMoreRPCsWereSent();
    // apparently no one has the same log you do
    algorithm.onAppendEntriesReply(S_03, 3, 3, 1, false);
    algorithm.onAppendEntriesReply(S_01, 3, 3, 1, false);
    algorithm.onAppendEntriesReply(S_04, 3, 3, 1, false);
    algorithm.onAppendEntriesReply(S_02, 3, 3, 1, false);
    // move to the heartbeat timeout
    timer.fastForward();
    // again, heartbeat, but with more entries (to try overwrite their prefix)
    appendEntriesRequests = getRPCs(4, AppendEntries.class);
    assertThatAppendEntriesHaveValues(
            appendEntriesRequests,
            3, 1, 2, 1,
            NOOP(3, 2),
            NOOP(4, 3));
    assertThatNoMoreRPCsWereSent();
    // OK, this is broken (i.e. the real followers won't do this), but I'm
    // doing this just to check the commit logic
    // everyone's happy, but they only want to commit one entry, the one from the previous term
    algorithm.onAppendEntriesReply(S_03, 3, 2, 1, true);
    algorithm.onAppendEntriesReply(S_01, 3, 2, 1, true);
    algorithm.onAppendEntriesReply(S_04, 3, 2, 1, true);
    algorithm.onAppendEntriesReply(S_02, 3, 2, 1, true);
    // it's nice that we have quorum for an entry in the older term,
    // but unless they also accept an entry from this term I can't consider
    // it committed
    // so, the listener shouldn't be notified and the commit index shouldn't be changed
    verifyNoMoreInteractions(listener);
    assertThatLogContains(
            SENTINEL(),
            NOOP(1, 1),
            NOOP(2, 1),
            NOOP(3, 2),
            NOOP(4, 3)
    );
    assertThatTermAndCommitIndexHaveValues(3, 1);
}