<commit_before>package digital.upgrade.replication.raft;

import digital.upgrade.replication.CommitReplicator;
import digital.upgrade.replication.Model.CommitMessage;
import digital.upgrade.replication.raft.Raft.AppendResult;
import digital.upgrade.replication.raft.Raft.Entry;
import digital.upgrade.replication.raft.Raft.Term;

import org.testng.annotations.Test;

import java.util.UUID;

import static org.testng.Assert.*;

public final class RaftReplicatorTest {

    @Test
    public void testCommitReturnsCommitState() {
        ClockSource clock = new SystemClock();
        CommitReplicator replicator = RaftReplicator.newBuilder()
                .setClockSource(clock)
                .setStateManager(new InMemoryStateManager(clock))
                .setCommitHandler(new InMemoryCommitHandler(clock))
                .build();
        assertNotNull(replicator.commit(CommitMessage.newBuilder().build()));
    }

    @Test
    public void testAppendReturnsResult() {
        RaftReplicator replicator = RaftReplicatorStateTest.startedReplicator();
        AppendResult result = replicator.append(zeroRequest().build());
        assertNotNull(result);
    }

    @Test
    public void testReturnsFalseIfTermLessCurrentTerm() {
        RaftReplicator replicator = RaftReplicatorStateTest.startedReplicator();
        Term current = replicator.getCurrentTerm();
        AppendResult result = replicator.append(zeroRequest()
                .setLeaderTerm(replicator.getCurrentTerm()
                        .toBuilder()
                        .setNumber(current.getNumber() - 1))
                .build());
        assertFalse(result.getSuccess());
    }

    @Test
    public void testReturnTrueIfTermEqualCurrentTerm() {
        RaftReplicator replicator = RaftReplicatorStateTest.startedReplicator();
        AppendResult result = replicator.append(zeroRequest()
                .setLeaderTerm(replicator.getCurrentTerm())
                .build());
        assertTrue(result.getSuccess());
    }

    @Test
    public void testReturnFalseIfPreviousLogTermMismatch() {
        RaftReplicator replicator = RaftReplicatorStateTest.startedReplicator();
        CommitIndex previousIndex = zeroIndex();
        AppendResult result = replicator.append(zeroRequest()
                .addEntries(newEntry(replicator, previousIndex))
                .setLeaderTerm(replicator.getCurrentTerm())
                .build());
        CommitIndex nextIndex = previousIndex.nextIndex();
        assertTrue(result.getSuccess());
        Term wrongTerm = Term.newBuilder()
                .setNumber(999)
                .build();
        result = replicator.append(zeroRequest()
                .addEntries(newEntry(replicator, nextIndex))
                .setLeaderTerm(replicator.getCurrentTerm())
                .setPreviousIndex(previousIndex.indexValue())
                .setPreviousTerm(wrongTerm)
                .build());
        assertFalse(result.getSuccess());
    }

    private Entry newEntry(RaftReplicator replicator, CommitIndex index) {
        return Entry.newBuilder()
                .setCommit(index.indexValue())
                .setTerm(replicator.getCurrentTerm())
                .setCommand(com.google.protobuf.ByteString.EMPTY)
                .build();
    }

    private CommitIndex zeroIndex() {
        return new CommitIndex(0);
    }


    private Raft.AppendRequest.Builder zeroRequest() {
        return Raft.AppendRequest.newBuilder()
                .setLeaderTerm(Term.newBuilder()
                        .setNumber(0)
                        .build())
                .setLeader(Raft.Peer.newBuilder()
                        .setUuid(UUID.randomUUID().toString())
                        .build())
                .setPreviousIndex(CommitIndex.ZERO)<commit_msg>Created failing testing for rule 5.3 that if a log entry is committed with a mismatched term then all following commits should be removed.<commit_after>package digital.upgrade.replication.raft;

import digital.upgrade.replication.CommitReplicator;
import digital.upgrade.replication.Model.CommitMessage;
import digital.upgrade.replication.raft.Raft.AppendRequest;
import digital.upgrade.replication.raft.Raft.AppendResult;
import digital.upgrade.replication.raft.Raft.Entry;
import digital.upgrade.replication.raft.Raft.Index;
import digital.upgrade.replication.raft.Raft.Peer;
import digital.upgrade.replication.raft.Raft.Term;

import com.google.protobuf.ByteString;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.UUID;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.AssertJUnit.assertFalse;

public final class RaftReplicatorTest {

    private static final Term TERM_777 = Term.newBuilder()
            .setNumber(777)
            .build();
    private static final Entry THIRD_ENTRY = Entry.newBuilder()
            .setCommit(Index.newBuilder()
                    .setMostSignificant(0)
                    .setLeastSignificant(2)
                    .build())
            .setTerm(TERM_777)
            .setCommand(ByteString.EMPTY)
            .build();
    private static final Entry[] SINGLE_TERM_ENTRIES = new Entry[]{
            THIRD_ENTRY
    };

    @Test
    public void testCommitReturnsCommitState() {
        ClockSource clock = new SystemClock();
        CommitReplicator replicator = RaftReplicator.newBuilder()
                .setClockSource(clock)
                .setStateManager(new InMemoryStateManager(clock))
                .setCommitHandler(new InMemoryCommitHandler(clock))
                .build();
        assertNotNull(replicator.commit(CommitMessage.newBuilder().build()));
    }

    @Test
    public void testAppendReturnsResult() {
        RaftReplicator replicator = RaftReplicatorStateTest.startedReplicator();
        AppendResult result = replicator.append(zeroRequest().build());
        assertNotNull(result);
    }

    @Test
    public void testReturnsFalseIfTermLessCurrentTerm() {
        RaftReplicator replicator = RaftReplicatorStateTest.startedReplicator();
        Term current = replicator.getCurrentTerm();
        AppendResult result = replicator.append(zeroRequest()
                .setLeaderTerm(replicator.getCurrentTerm()
                        .toBuilder()
                        .setNumber(current.getNumber() - 1))
                .build());
        assertFalse(result.getSuccess());
    }

    @Test
    public void testReturnTrueIfTermEqualCurrentTerm() {
        RaftReplicator replicator = RaftReplicatorStateTest.startedReplicator();
        AppendResult result = replicator.append(zeroRequest()
                .setLeaderTerm(replicator.getCurrentTerm())
                .build());
        assertTrue(result.getSuccess());
    }

    @Test
    public void testReturnFalseIfPreviousLogTermMismatch() {
        RaftReplicator replicator = RaftReplicatorStateTest.startedReplicator();
        CommitIndex previousIndex = zeroIndex();
        AppendResult result = replicator.append(zeroRequest()
                .addEntries(newEntry(replicator, previousIndex))
                .setLeaderTerm(replicator.getCurrentTerm())
                .build());
        CommitIndex nextIndex = previousIndex.nextIndex();
        assertTrue(result.getSuccess());
        Term wrongTerm = Term.newBuilder()
                .setNumber(999)
                .build();
        result = replicator.append(zeroRequest()
                .addEntries(newEntry(replicator, nextIndex))
                .setLeaderTerm(replicator.getCurrentTerm())
                .setPreviousIndex(previousIndex.indexValue())
                .setPreviousTerm(wrongTerm)
                .build());
        assertFalse(result.getSuccess());
    }

    @Test
    public void testOnConflictingTermDeleteEntryAndAllFollowing() {
        RaftReplicator replicator = RaftReplicatorStateTest.startedReplicator();
        replicator.append(zeroRequest()
                .addAllEntries(Arrays.asList(SINGLE_TERM_ENTRIES))
                .build());
        replicator.append(zeroRequest()
                .setPreviousTerm(replicator.getCurrentTerm())
                .setPreviousIndex(Index.newBuilder()
                        .setMostSignificant(0)
                        .setLeastSignificant(2)
                        .build())
                .setLeaderTerm(replicator.getCurrentTerm())
                .addEntries(THIRD_ENTRY)
                .build());
        assertEquals(replicator.getCommittedIndex().indexValue(), THIRD_ENTRY.getCommit());
    }

    private Entry newEntry(RaftReplicator replicator, CommitIndex index) {
        return Entry.newBuilder()
                .setCommit(index.indexValue())
                .setTerm(replicator.getCurrentTerm())
                .setCommand(com.google.protobuf.ByteString.EMPTY)
                .build();
    }

    private CommitIndex zeroIndex() {
        return new CommitIndex(0);
    }


    private AppendRequest.Builder zeroRequest() {
        return AppendRequest.newBuilder()
                .setLeaderTerm(Term.newBuilder()
                        .setNumber(0)
                        .build())
                .setLeader(Peer.newBuilder()
                        .setUuid(UUID.randomUUID().toString())
                        .build())
                .setPreviousIndex(CommitIndex.ZERO)
{'fraction_non_alphanumeric': 0.09520271704521333, 'fraction_numerical': 0.0028656336234345148, 'mean_word_length': 2.3786303334528505, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6890606', 'n_tokens_mistral': 2533, 'n_tokens_neox': 2422, 'n_words': 389}