@Test
public void testMultipleReplicateWithReplyShouldResultInAppendEntries() {

    logStart("testMultipleReplicateWithReplyShouldResultInAppendEntries");
    MockRaftActorContext actorContext = createActorContextWithFollower();
    actorContext.setConfigParams(new DefaultConfigParamsImpl() {
        @Override
        public FiniteDuration getHeartBeatInterval() {
            return FiniteDuration.apply(5, TimeUnit.SECONDS);
        }
    });
    long term = 1;
    actorContext.getTermInformation().update(term, "");
    leader = new Leader(actorContext);
    // Leader will send an immediate heartbeat - ignore it.
    MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    // The follower would normally reply - simulate that explicitly here.
    long lastIndex = actorContext.getReplicatedLog().lastIndex();
    leader.handleMessage(followerActor, new AppendEntriesReply(
            FOLLOWER_ID, term, true, lastIndex, term, (short)0));
    assertEquals("isFollowerActive", true, leader.getFollower(FOLLOWER_ID).isFollowerActive());
    followerActor.underlyingActor().clear();
    for (int i = 0; i < 3; i++) {
        sendReplicate(actorContext, lastIndex + i + 1);
        leader.handleMessage(followerActor, new AppendEntriesReply(
                FOLLOWER_ID, term, true, lastIndex + i + 1, term, (short)0));
    }
    // We are expecting six messages here -- a request to replicate and a consensus-reached message
    List<AppendEntries> allMessages = MessageCollectorActor.getAllMatching(followerActor, AppendEntries.class);
    assertEquals("The number of request/consensus appends collected", 6, allMessages.size());
    for (int i = 0; i < 3; i++) {
        assertRequestEntry(lastIndex, allMessages, i);
        assertCommitEntry(lastIndex, allMessages, i);
    }
    // Now perform another commit, eliciting a request to persist
    sendReplicate(actorContext, lastIndex + 3 + 1);
    allMessages = MessageCollectorActor.getAllMatching(followerActor, AppendEntries.class);
    // This elicits another message for request to replicate
    assertEquals("The number of request entries collected", 7, allMessages.size());
    assertRequestEntry(lastIndex, allMessages, 3);
    sendReplicate(actorContext, lastIndex + 4 + 1);
    allMessages = MessageCollectorActor.getAllMatching(followerActor, AppendEntries.class);
    assertEquals("The number of request entries collected", 7, allMessages.size());
}