def datagram_received(self, data, addr): try: packet = parse(data) if packet.blockid in self.acked: self.send_ack(packet.blockid, addr) else: if packet.blockid in self.ack_next: self.acked.append(packet.blockid) self.send_ack(packet.blockid, addr) self.received.extend(packet.data) if len(packet.data) < 512: self._waiter.set_result(self.received) self.transport.close() else: if random.choice(["ack next", "don't"]) == "ack next": self.ack_next.append(packet.blockid) except Exception as exc: self._waiter.set_exception(exc) self.transport.close()
def test_error_packets(pkt): data = bytes(pkt) assert packet.parse(data) == pkt
def test_ack_packets(pkt): ack = bytes(pkt) assert packet.parse(ack) == pkt
def test_request_packets(pkt): data = bytes(pkt) assert packet.parse(data) == pkt
def test_parse_invalid_packet(buf): with pytest.raises(ValueError): packet.parse(buf)
def test_parse_valid_packet(buf): assert packet.parse(buf)