Beispiel #1
0
def test_handle_append_entries_candidate_to_candidate_msg_term_too_small():
    controller = mock.MagicMock()
    servers = [(), ()]  # Need two items in list for id = 1 to be valid
    id_ = 1
    m = Machine(id_, controller, servers)
    m.current_term = 5

    m._state = constants.State.CANDIDATE
    m.voted_for = id_

    msg = messages.AppendEntriesMessage(
        src=2,
        dst=id_,
        term=3,  # Old term
        leader_id=2,
        prev_log_index=0,
        prev_log_term=0,
        entries=(),
        leader_commit=0,
    )

    m.handle_AppendEntriesMessage(msg)

    assert m._state == constants.State.CANDIDATE
    assert m.voted_for == id_
Beispiel #2
0
def test_handle_append_entries_leader_to_follower():
    controller = mock.MagicMock()
    servers = [(), ()]  # Need two items in list for id = 1 to be valid
    id_ = 1
    m = Machine(id_, controller, servers)
    m.current_term = 5

    m._state = constants.State.LEADER

    msg = messages.AppendEntriesMessage(
        src=2,
        dst=id_,
        term=7,  # More recent term
        leader_id=2,
        prev_log_index=0,
        prev_log_term=0,
        entries=(),
        leader_commit=0,
    )

    m.handle_AppendEntriesMessage(msg)

    assert m._state == constants.State.FOLLOWER
    assert m.voted_for == 2
Beispiel #3
0
def test_handle_append_entries_candidate_with_log_truncation():
    controller = mock.MagicMock()
    servers = [(), ()]  # Need two items in list for id = 1 to be valid
    id_ = 1
    m = Machine(id_, controller, servers)
    m.current_term = 2
    m.log = [
        log.LogEntry(2, 'a'),
        log.LogEntry(2, 'b'),
        log.LogEntry(2, 'c'),
    ]

    m._state = constants.State.FOLLOWER
    m.voted_for = id_

    msg = messages.AppendEntriesMessage(
        src=2,
        dst=id_,
        term=2,
        leader_id=2,
        prev_log_index=1,
        prev_log_term=2,
        entries=[
            log.LogEntry(2, 'd'),
            log.LogEntry(2, 'e'),
        ],
        leader_commit=4,
    )
    m.handle_AppendEntriesMessage(msg)

    assert m.log == [
        log.LogEntry(2, 'a'),
        log.LogEntry(2, 'b'),
        log.LogEntry(2, 'd'),
        log.LogEntry(2, 'e'),
    ]