async def asyncSetUp(self): self.oserver = Server(0, Follower()) self.server = Server(1, Follower()) self.server._neighbors.append(self.oserver) self.oserver._neighbors.append(self.server) candidate = Candidate() self.server._state = candidate await candidate.set_server(self.server)
async def asyncSetUp(self): board = MemoryBoard() state = Follower() self.oserver = Server(0, state, [], board, []) board = MemoryBoard() state = Follower() self.server = Server(1, state, [], board, [self.oserver]) asyncio.create_task(self.oserver.run()) asyncio.create_task(self.server.run())
async def test_two_candidates_tie(self): followers = [] for i in range(4): followers.append(Server(i, Follower())) c0 = Server(5, Candidate(), neighbors=followers[0:2]) c1 = Server(6, Candidate(), neighbors=followers[2:]) await c0._state._start_election() await c1._state._start_election() for i in range(2): followers[i]._neighbors.append(c0) await followers[i].on_message( await followers[i]._messageBoard.get_message()) for i in range(2, 4): followers[i]._neighbors.append(c1) await followers[i].on_message( await followers[i]._messageBoard.get_message()) c0._total_nodes = 6 c1._total_nodes = 6 for i in range(2): await c0.on_message(await c0._messageBoard.get_message()) await c1.on_message(await c1._messageBoard.get_message()) self.assertEqual(type(c0._state), Candidate) self.assertEqual(type(c1._state), Candidate)
async def test_two_candidates_one_wins(self): followers = [] for i in range(6): followers.append(Server(i, Follower())) board = MemoryBoard() state = Candidate() c0 = Server(7, Candidate(), neighbors=followers[0:2]) c1 = Server(8, Candidate(), neighbors=followers[2:]) await c0._state._start_election() await c1._state._start_election() for i in range(2): followers[i]._neighbors.append(c0) await followers[i].on_message( await followers[i]._messageBoard.get_message()) for i in range(2, 6): followers[i]._neighbors.append(c1) await followers[i].on_message( await followers[i]._messageBoard.get_message()) c0._total_nodes = 7 c1._total_nodes = 7 for i in range(2): await c0.on_message(await c0._messageBoard.get_message()) for i in range(4): await c1.on_message(await c1._messageBoard.get_message()) self.assertEqual(type(c0._state), Candidate) self.assertEqual(type(c1._state), Leader)
async def on_append_entries(self, message): self._timeoutTime = self._nextTimeout() self.timer.cancel() self.timer = self.restart_timer() if message.term < self._server._currentTerm: await self._send_response_message(message, yes=False) return self, None if "leaderId" in message.data and self.leader != message.data["leaderId"]: self.leader = message.data["leaderId"] logger.info(f"Accepted new leader: {self.leader}") from simpleRaft.states.follower import Follower # TODO: Fix circular import if not isinstance(self, Follower): follower = Follower() follower.leader = self.leader follower.set_server(self._server) return follower, None
async def test_candidate_server_wins_election(self): server0 = Server(0, Follower()) oserver = Server(1, Follower()) server = Server(2, Candidate()) server._neighbors.append(oserver) server._neighbors.append(server0) server0._neighbors.append(server) oserver._neighbors.append(server) await server._state._start_election() await oserver.on_message(await oserver._messageBoard.get_message()) await server0.on_message(await server0._messageBoard.get_message()) server._total_nodes = 3 await server.on_message(await server._messageBoard.get_message()) await server.on_message(await server._messageBoard.get_message()) self.assertEqual(type(server._state), Leader)
async def asyncSetUp(self): followers = [] for i in range(1, 4): board = MemoryBoard() state = Follower(timeout=1) followers.append(Server(i, state, [], board, [])) board = MemoryBoard() state = Leader() self.leader = Server(0, state, [], board, followers) for i in followers: i._neighbors.append(self.leader) # Consume the heart beat message sent from set_server() for i in self.leader._neighbors: await i.on_message(await i._messageBoard.get_message())
async def asyncSetUpClass(cls): cls.servers = [] for i in range(N): s = ZeroMQServer("S%d" % i, Follower(), port=6666 + i) cls.servers.append(s) for i in range(N): me = cls.servers[i] neighbors = [n for n in cls.servers if n != me] for n in neighbors: me.add_neighbor(n) server0 = cls.servers[0] server0._state = Candidate() for i in range(N): if isinstance(cls.servers[i]._state, Leader): cls.leader = cls.servers[i] break else: # Manually elect server-0 as the leader cls.servers[0]._state = leader = Leader() cls.leader = cls.servers[0] await leader.set_server(cls.leader)