def test_handle_no_comm_election_timeout_leader_to_leader(now): now.return_value = 123456789 controller = mock.MagicMock() id_ = 1 m = Machine(id_, controller) m._controller.enqueue.reset_mock() m.current_term = 5 m._state = constants.State.LEADER m.voted_for = id_ 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 == 5 assert m._state == constants.State.LEADER assert m.voted_for == id_ enqueue_calls = m._controller.enqueue.mock_calls call = enqueue_calls[0] name, args, kwargs = call first_arg = args[0] assert isinstance(first_arg, messages.ElectionTimeoutMessage) assert first_arg.src == id_ assert first_arg.dst == id_ assert first_arg.term == m.current_term assert first_arg.time == 123456789 + m._election_timeout
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