def test_handle_request_vote_reply_is_candidate_win_election(now): now.return_value = 123456789 controller = mock.MagicMock() id_ = 1 m = Machine(id_, controller) m._controller.enqueue.reset_mock() m.current_term = 5 m.voted_for = id_ m._state = constants.State.CANDIDATE m._votes = 2 m.log = [log.LogEntry(m.current_term, '')] msg = messages.RequestVoteResponseMessage( src=0, dst=1, term=m.current_term, ) m.handle_RequestVoteResponseMessage(msg) assert m._state == constants.State.LEADER # Assert messages sent to all other servers enqueue_calls = m._controller.enqueue.mock_calls[:-1] 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.AppendEntriesMessage) assert msg.src == id_ dsts.add(msg.dst) assert msg.term == m.current_term assert msg.leader_id == id_ assert msg.prev_log_term == 5 assert msg.prev_log_index == 0 assert msg.entries == () assert msg.leader_commit == 0 assert set(range(len(m._servers))) - {id_} == dsts call = m._controller.enqueue.mock_calls[-1] name, args, kwargs = call first_arg = args[0] assert isinstance(first_arg, messages.LeaderTimeoutMessage) assert first_arg.src == id_ assert first_arg.dst == id_ assert first_arg.term == m.current_term assert first_arg.time == 123456789 + m._leader_timeout
def test_handle_request_vote_reply_is_candidate_stale_term(): controller = mock.MagicMock() id_ = 1 m = Machine(id_, controller) m._controller.enqueue.reset_mock() m.current_term = 5 m.voted_for = id_ m._state = constants.State.CANDIDATE m._votes = 2 msg = messages.RequestVoteResponseMessage( src=0, dst=1, term=99, ) m.handle_RequestVoteResponseMessage(msg) assert m._state == constants.State.CANDIDATE m._controller.enqueue.assert_not_called()