Beispiel #1
0
class TestProduceAPI(unittest2.TestCase):
    maxDiff = None

    test_messages = [
        protocol.Message(b'this is a test message', partition_key=b'asdf'),
        protocol.Message(b'this is also a test message',
                         partition_key=b'test_key'),
        protocol.Message(b"this doesn't have a partition key"),
    ]

    def test_request(self):
        message = self.test_messages[0]
        req = protocol.ProduceRequest()
        req.add_message(message, b'test', 0)
        msg = req.get_bytes()
        self.assertEqual(
            msg,
            bytearray(
                b"\x00\x00\x00a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x07pykafka\x00\x01\x00\x00\'\x10\x00\x00\x00\x01\x00\x04test\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x004\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00(\x0e\x8a\x19O\x00\x00\x00\x00\x00\x04asdf\x00\x00\x00\x16this is a test message"
            ))

    def test_gzip_compression(self):
        req = protocol.ProduceRequest(compression_type=CompressionType.GZIP)
        [req.add_message(m, b'test_gzip', 0) for m in self.test_messages]
        msg = req.get_bytes()
        self.assertEqual(len(msg), 207)  # this isn't a good test

    def test_snappy_compression(self):
        req = protocol.ProduceRequest(compression_type=CompressionType.SNAPPY)
        [req.add_message(m, b'test_snappy', 0) for m in self.test_messages]
        msg = req.get_bytes()
        self.assertEqual(len(msg), 212)  # this isn't a good test

    def test_partition_error(self):
        # Response has a UnknownTopicOrPartition error for test/0
        response = protocol.ProduceResponse(
            buffer(
                b'\x00\x00\x00\x01\x00\x04test\x00\x00\x00\x01\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x02'
            ))
        self.assertEqual(response.topics[b'test'][0].err, 3)

    def test_response(self):
        response = protocol.ProduceResponse(
            buffer(
                b'\x00\x00\x00\x01\x00\x04test\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02'
            ))
        self.assertEqual(
            response.topics,
            {b'test': {
                0: protocol.ProducePartitionResponse(0, 2)
            }})
Beispiel #2
0
class TestProduceAPI(unittest2.TestCase):
    maxDiff = None

    test_messages = [
        protocol.Message(b'this is a test message', partition_key=b'asdf'),
        protocol.Message(b'this is also a test message',
                         partition_key=b'test_key'),
        protocol.Message(b"this doesn't have a partition key"),
    ]

    def test_request(self):
        message = self.test_messages[0]
        req = protocol.ProduceRequest()
        req.add_message(message, b'test', 0)
        msg = req.get_bytes()
        self.assertEqual(
            msg,
            bytearray(
                b"\x00\x00\x00a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x07pykafka"  # header
                b'\x00\x01'  # required acks
                b'\x00\x00\'\x10'  # timeout
                b'\x00\x00\x00\x01'  # len(topics)
                b'\x00\x04'  # len(topic name)
                b'test'  # topic name
                b'\x00\x00\x00\x01'  # len (partitions)
                b'\x00\x00\x00\x00'  # partition
                b'\x00\x00\x004'  # message set size
                b'\xff\xff\xff\xff\xff\xff\xff\xff'  # offset
                b'\x00\x00\x00('  # message size
                b'\x0e\x8a\x19O'  # crc
                b'\x00'  # magic byte
                b'\x00'  # attributes
                b'\x00\x00\x00\x04'  # len(key)
                b'asdf'  # key
                b'\x00\x00\x00\x16'  # len(value)
                b"this is a test message"  # value
            ))

    def test_gzip_compression(self):
        req = protocol.ProduceRequest(compression_type=CompressionType.GZIP)
        [req.add_message(m, b'test_gzip', 0) for m in self.test_messages]
        msg = req.get_bytes()
        self.assertEqual(len(msg), 207)  # this isn't a good test

    def test_snappy_compression(self):
        req = protocol.ProduceRequest(compression_type=CompressionType.SNAPPY)
        [req.add_message(m, b'test_snappy', 0) for m in self.test_messages]
        msg = req.get_bytes()
        self.assertEqual(len(msg), 212)  # this isn't a good test

    def test_partition_error(self):
        # Response has a UnknownTopicOrPartition error for test/0
        response = protocol.ProduceResponse(
            buffer(b'\x00\x00\x00\x01'  # len(topics)
                   b'\x00\x04'  # len(topic name)
                   b'test'  # topic name
                   b'\x00\x00\x00\x01'  # len(partitions)
                   b'\x00\x00\x00\x00'  # partition
                   b'\x00\x03'  # error code
                   b'\x00\x00\x00\x00\x00\x00\x00\x02'  # offset
                   ))
        self.assertEqual(response.topics[b'test'][0].err, 3)

    def test_response(self):
        response = protocol.ProduceResponse(
            buffer(b'\x00\x00\x00\x01'  # len(topics)
                   b'\x00\x04'  # len(topic name)
                   b'test'  # topic name
                   b'\x00\x00\x00\x01'  # len(partitions)
                   b'\x00\x00\x00\x00'  # partition
                   b'\x00\x00'  # error code
                   b'\x00\x00\x00\x00\x00\x00\x00\x02'  # offset
                   ))
        self.assertEqual(
            response.topics,
            {b'test': {
                0: protocol.ProducePartitionResponse(0, 2)
            }})