def test_remove_incorrect(self): clock = Clock() leader_store = MockStoreDontUse( entries={ 1: Entry(term=1, payload=1), 2: Entry(term=2, payload=2), 3: Entry(term=2, payload=3), }) leader_store.setCurrentTerm(2) leader_rpc = MockRPC() leader = RaftNode(1, leader_store, leader_rpc, clock=clock) follower_store = MockStoreDontUse({ 1: Entry(term=1, payload=1), 2: Entry(term=5, payload=1) }) follower_rpc = MockRPC() follower = RaftNode(2, follower_store, follower_rpc, clock=clock) leader_rpc.simpleAddNode(follower) follower_rpc.simpleAddNode(leader) d = leader._callAppendEntries(follower.id, {}) res = self.successResultOf(d) self.assertEqual(leader_store.log, follower_store.log)
def test_respond_requestVote_oldLog(self): store = MockStoreDontUse(entries={ 2: Entry(term=2, payload=1), 3: Entry(term=3, payload=2) }) store.setCurrentTerm(3) rpc = MockRPC() clock = Clock() node = RaftNode(1, store, rpc, clock=clock) resp = node.respond_requestVote( RequestVotesCommand(term=4, candidateId='id', lastLogIndex=2, lastLogTerm=2)) term, result = self.successResultOf(resp) self.assertFalse(result) resp = node.respond_requestVote( RequestVotesCommand(term=4, candidateId='id', lastLogIndex=4, lastLogTerm=2)) term, result = self.successResultOf(resp) self.assertFalse(result) resp = node.respond_requestVote( RequestVotesCommand(term=4, candidateId='id', lastLogIndex=2, lastLogTerm=3)) term, result = self.successResultOf(resp) self.assertFalse(result)
def test_cluster(self): nodes = [] for num in range(5): clock = Clock() rpc = MockRPC() store = MockStoreDontUse() node = RaftNode(num, store, rpc, clock=clock, electionTimeout=1) nodes.append((node, rpc, store, clock)) for node1, rpc, _, _ in nodes: for node2, _, _, _ in nodes: if node1 is node2: continue rpc.simpleAddNode(node2) for node, rpc, store, clock in nodes: clock.advance(1.0)
def test_three_up(self): store1 = MockStoreDontUse() store2 = MockStoreDontUse() store3 = MockStoreDontUse() rpc1 = MockRPC() rpc2 = MockRPC() rpc3 = MockRPC() clock1 = Clock() clock2 = Clock() clock3 = Clock() node1 = RaftNode(1, store1, rpc1, clock=clock1) node2 = RaftNode(2, store2, rpc2, clock=clock2) node3 = RaftNode(3, store3, rpc3, clock=clock3) for rpc in [rpc1, rpc2, rpc3]: for node in [node1, node2, node3]: rpc.simpleAddNode(node) clock1.advance(0.4) self.assertIs(node1._state, STATE.LEADER)
def test_respond_requestVote_lowerTerm(self): store = MockStoreDontUse() store.setCurrentTerm(3) rpc = MockRPC() clock = Clock() node = RaftNode(1, store, rpc, clock=clock) resp = node.respond_requestVote( RequestVotesCommand(term=2, candidateId='id', lastLogIndex=4, lastLogTerm=4)) term, result = self.successResultOf(resp) self.assertFalse(result)
def test_respond_requestVote(self): store = MockStoreDontUse() rpc = MockRPC() clock = Clock() node = RaftNode(1, store, rpc, clock=clock) resp = node.respond_requestVote( RequestVotesCommand(term=4, candidateId=2, lastLogIndex=4, lastLogTerm=4)) term, result = self.successResultOf(resp) self.assertTrue(result) votedFor = self.successResultOf(store.getVotedFor()) self.assertEqual(votedFor, 2)
def test_respond_appendEntries_empty(self): store = MockStoreDontUse() rpc = MockRPC() clock = Clock() node = RaftNode(1, store, rpc, clock=clock) newentry = Entry(term=0, payload=1) resp = node.respond_appendEntries( AppendEntriesCommand(term=0, leaderId=2, prevLogIndex=0, prevLogTerm=0, entries={}, leaderCommit=1)) term, result = self.successResultOf(resp) self.assertEqual(term, 0) self.assertTrue(result)
def test_respond_requestVote_alreadyVoted(self): store = MockStoreDontUse() store.setVotedFor(3) rpc = MockRPC() clock = Clock() node = RaftNode(1, store, rpc, clock=clock) resp = node.respond_requestVote( RequestVotesCommand(term=4, candidateId=2, lastLogIndex=4, lastLogTerm=4)) term, result = self.successResultOf(resp) self.assertFalse(result) resp = node.respond_requestVote( RequestVotesCommand(term=4, candidateId=3, lastLogIndex=4, lastLogTerm=4)) term, result = self.successResultOf(resp) self.assertTrue(result)