def test_learner_sync_messages(self): for i in range(Learner.SYNC_SIZE + 1): self.ledger.append(LedgerEntry(number=i, value="value_{0}".format(i))) self.role.receive(Sync.create(proposal=Proposal('A', 1), sender='A'), self.channel) message = self.channel.unicast_messages[-1] self.assertEqual(Learner.SYNC_SIZE, len(message.proposal)) self.assertTrue(not message.finished)
def test_learner_receives_sync(self): self.ledger.append(LedgerEntry(number=1, value="value_1")) self.ledger.append(LedgerEntry(number=2, value="value_2")) self.ledger.append(LedgerEntry(number=3, value="value_3")) self.role.receive(Sync.create(proposal=Proposal('A', 1), sender='A'), self.channel) message = self.channel.unicast_messages[-1] self.assert_equal(message.proposal[0], LedgerEntry(number=1, value="value_1")) self.assert_equal(message.proposal[1], LedgerEntry(number=2, value="value_2")) self.assert_equal(message.proposal[2], LedgerEntry(number=3, value="value_3"))
def wrapper(self, message, channel, **kw): latest_proposal = self.state.read(Role.PROPOSED) this_proposal = message.proposal if this_proposal.number == latest_proposal.number + 1: self.state.write(Role.PROPOSED, this_proposal.next()) if this_proposal.number > latest_proposal.number + 1: # we're behind and need to catch up channel.unicast(Sync.create(receiver=message.sender, sender=message.receiver, proposal=latest_proposal)) self.resume = lambda: func(self, message, channel, **kw) else: func(self, message, channel, **kw)
def _(self, message, channel): """Handle response for updating this replica""" LOG.debug("RECEIVED message {0}".format(message)) synced_proposals = message.proposal self._ledger.extend(synced_proposals) current_proposal = Proposal("sync", synced_proposals[-1].number) next_proposal = Proposal("sync", synced_proposals[-1].number + 1) self.state.write(Role.PROPOSED, current_proposal) self.state.write(Role.PROMISED, current_proposal) self.state.write(Role.ACCEPTED, current_proposal) if not message.finished: channel.unicast(Sync.create( receiver=message.sender, sender=message.receiver, proposal=self.state.read(Role.ACCEPTED))) else: self.state.write(Role.PROPOSED, next_proposal) self.resume()