Beispiel #1
0
    def test_decode_fetch_response(self):
        t1 = b"topic1"
        t2 = b"topic2"
        msgs = [create_message(msg)
                for msg in [b"message1", b"hi", b"boo", b"foo", b"so fun!"]]
        ms1 = KafkaProtocol._encode_message_set([msgs[0], msgs[1]])
        ms2 = KafkaProtocol._encode_message_set([msgs[2]])
        ms3 = KafkaProtocol._encode_message_set([msgs[3], msgs[4]])

        encoded = struct.pack('>iih%dsiihqi%dsihqi%dsh%dsiihqi%ds' %
                              (len(t1), len(ms1), len(ms2), len(t2), len(ms3)),
                              4, 2, len(t1), t1, 2, 0, 0, 10, len(ms1), ms1, 1,
                              1, 20, len(ms2), ms2, len(t2), t2, 1, 0, 0, 30,
                              len(ms3), ms3)

        responses = list(KafkaProtocol.decode_fetch_response(encoded))
        def expand_messages(response):
            return FetchResponsePayload(response.topic, response.partition,
                                        response.error, response.highwaterMark,
                                        list(response.messages))

        expanded_responses = list(map(expand_messages, responses))
        expect = [FetchResponsePayload(t1, 0, 0, 10, [OffsetAndMessage(0, msgs[0]),
                                                      OffsetAndMessage(0, msgs[1])]),
                  FetchResponsePayload(t1, 1, 1, 20, [OffsetAndMessage(0, msgs[2])]),
                  FetchResponsePayload(t2, 0, 0, 30, [OffsetAndMessage(0, msgs[3]),
                                                      OffsetAndMessage(0, msgs[4])])]
        self.assertEqual(expanded_responses, expect)
Beispiel #2
0
 def fail_requests(payloads, **kwargs):
     responses = [
         FetchResponsePayload(payloads[0].topic, payloads[0].partition, 0, 0,
                       [OffsetAndMessage(
                           payloads[0].offset + i,
                           "msg %d" % (payloads[0].offset + i))
                        for i in range(10)]),
     ]
     for failure in payloads[1:]:
         responses.append(error_factory(failure))
     return responses
 def not_leader(request):
     return FetchResponsePayload(request.topic, request.partition,
                                 NotLeaderForPartitionError.errno, -1,
                                 ())
 def unknown_topic_partition(request):
     return FetchResponsePayload(request.topic, request.partition,
                                 UnknownTopicOrPartitionError.errno, -1,
                                 ())
Beispiel #5
0
 def expand_messages(response):
     return FetchResponsePayload(response.topic, response.partition,
                                 response.error, response.highwaterMark,
                                 list(response.messages))