def test_candidate_server_wins_election(self): state = Follower() server0 = Server(0, state, [], []) state = Follower() server3 = Server(3, state, [], []) state = Follower() oserver = Server(1, state, [], []) state = Candidate() server = Server(2, state, [], [oserver, server0, server3]) server0._neighbors.append(server) oserver._neighbors.append(server) server3._neighbors.append(server) oserver.on_message(oserver.get_message()) server0.on_message(server0.get_message()) server3.on_message(server3.get_message()) server._total_nodes = 4 server.on_message(server.get_message()) server.on_message(server.get_message()) self.assertEquals(type(server._state), Leader)
def on_message(self, message): """This method is called when a message is received, and calls one of the other corrosponding methods that this state reacts to. """ _type = message.type if (message.term > self._server._currentTerm): self._server._currentTerm = message.term # Is the messages.term < ours? If so we need to tell # them this so they don't get left behind. # elif (message.term < self._server._currentTerm): # self._send_response_message(message, yes=False) # return self, None if (_type == BaseMessage.AppendEntries): state = Follower() self._server._state = state # elif (_type == BaseMessage.RequestVote): # a = self.on_vote_request(message) # return a if (_type == BaseMessage.RequestVoteResponse): a = self.on_vote_received(message) # print("RequestVoteResponse", a._server._name) return a
def setUp(self): state = Follower() self.oserver = Server(0, state, [], []) state = Candidate() self.server = Server(1, state, [], [self.oserver]) self.oserver._neighbors.append(self.server)
def setUp(self): followers = [] for i in range(1, 4): state = Follower() followers.append(Server(i, state, [], [])) state = Leader() self.leader = Server(0, state, [], followers) for i in followers: i._neighbors.append(self.leader)
if type(followers[t]._state) == Leader: message_data = "the number now is" + str(i) followers[1].send_data(message_data) print("the message has been sent") time.sleep(1) print("\n\nCreating four servers with names ranging from 0-3") # Create Servers followers = [] for name in range(4): ## Each server has a state out of (Candidate, Follower, Leader) ## Initially each candidate starts with follower state state = Follower() server = Server(name, state, [], []) ## Appending all neighbours to the current server for follower in followers: follower._neighbors.append(server) server._neighbors.append(follower) ## Attaching a thread to the server followers.append(server) thread = Thread(target=serverFunction, args=(name, )) thread.start() print("Started server with name ", name) print("\nWait until first timer timesout") thread = Thread(target=checkMesages, args=())
def setUp(self): state = Follower() self.oserver = Server(0, state, [], []) state = Follower() self.server = Server(1, state, [], [self.oserver])