def test_get_last_log_data(): controller = mock.MagicMock() id_ = 1 m = Machine(id_, controller) m._controller.enqueue.reset_mock() m.log = [log.LogEntry(m.current_term, '')] last_log_term, last_log_index = m.get_last_log_data() assert last_log_term == 0 assert last_log_index == 0
def test_handle_no_comm_election_timeout_candidate_to_candidate(): controller = mock.MagicMock() id_ = 1 m = Machine(id_, controller) m.get_last_log_data = lambda: (5, 17) m._controller.enqueue.reset_mock() m.current_term = 5 m._state = constants.State.CANDIDATE m.voted_for = None m.last_applied = 17 msg = messages.ElectionTimeoutMessage( src=2, dst=id_, term=m.current_term, time=utils.now() ) m.handle_ElectionTimeoutMesssage(msg) assert m.current_term == 6 assert m._state == constants.State.CANDIDATE assert m.voted_for == id_ # Assert messages sent to all other servers enqueue_calls = m._controller.enqueue.mock_calls assert len(enqueue_calls) == len(m._servers) - 1 dsts = set() for mock_call in enqueue_calls: name, args, kwargs = mock_call msg = args[0] assert isinstance(msg, messages.RequestVoteMessage) assert msg.src == id_ dsts.add(msg.dst) assert msg.term == 6 assert msg.candidate_id == id_ assert msg.last_log_index == 17 assert msg.last_log_term == 5 assert set(range(len(m._servers))) - {id_} == dsts