def test_decode_fetch_response(self): t1 = "topic1" t2 = "topic2" msgs = map(create_message, ["message1", "hi", "boo", "foo", "so fun!"]) ms1 = KafkaCodec._encode_message_set([msgs[0], msgs[1]]) ms2 = KafkaCodec._encode_message_set([msgs[2]]) ms3 = KafkaCodec._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(KafkaCodec.decode_fetch_response(encoded)) def expand_messages(response): return FetchResponse(response.topic, response.partition, response.error, response.highwaterMark, list(response.messages)) expanded_responses = map(expand_messages, responses) expect = [ FetchResponse( t1, 0, 0, 10, [OffsetAndMessage(0, msgs[0]), OffsetAndMessage(0, msgs[1])]), FetchResponse(t1, 1, 1, 20, [OffsetAndMessage(0, msgs[2])]), FetchResponse( t2, 0, 0, 30, [OffsetAndMessage(0, msgs[3]), OffsetAndMessage(0, msgs[4])]) ] self.assertEqual(expanded_responses, expect)
def test_check_error_no_raise(self): for code, e in kafka_errors.items(): self.assertRaises(e, check_error, code) responses = [ (ProduceResponse("topic1", 5, 3, 9), UnknownTopicOrPartitionError), (FetchResponse("topic2", 3, 10, 8, []), MessageSizeTooLargeError), (OffsetResponse("topic3", 8, 1, []), OffsetOutOfRangeError), (OffsetCommitResponse("topic4", 10, 12), OffsetMetadataTooLargeError), (OffsetFetchResponse("topic5", 33, 12, "", 5), LeaderNotAvailableError), ] for resp, e in responses: self.assertTrue(isinstance(check_error(resp, False), e))
def test_check_error(self): for code, e in BrokerResponseError.errnos.items(): self.assertRaises(e, _check_error, code) responses = [ (ProduceResponse("topic1", 5, 3, 9), UnknownTopicOrPartitionError), (FetchResponse("topic2", 3, 10, 8, []), MessageSizeTooLargeError), (OffsetResponse("topic3", 8, 1, []), OffsetOutOfRangeError), (OffsetCommitResponse("topic4", 10, 12), OffsetMetadataTooLargeError), (OffsetFetchResponse("topic5", 33, 12, "", 5), LeaderNotAvailableError), (ConsumerMetadataResponse(15, -1, "", -1), CoordinatorNotAvailable), (OffsetFetchResponse("topic6", 23, -1, "", 14), CoordinatorLoadInProgress), (OffsetCommitResponse("topic7", 24, 16), NotCoordinator), ] for resp, e in responses: self.assertRaises(e, _check_error, resp)
def expand_messages(response): return FetchResponse(response.topic, response.partition, response.error, response.highwaterMark, list(response.messages))