def test_read_message(self): response_format = "".join([ "!", "i", "i", "h%dsi" % len("broker01"), # array of brokers "i", "hh%ds" % len("example.foo"), # array of topics "i", # subarray of partitions "hii", "i", "ii", "i", "i", # partition 1 details "hii", "i", "ii", "i", "ii", # partition 2 details ]) raw_response = struct.pack( response_format, 1, # there is 1 broker 8, len("broker01"), b"broker01", 1234, # broker id,host,port 1, # there is 1 topic 0, len("example.foo"), b"example.foo", # topic name, no error 2, # there are 2 topics 0, 1, 1, # partition ID 1, leader is broker 1 2, 2, 3, 1, 2, # two replicas: on 2 & 3, one ISR: broker 2 0, 2, 3, # partition ID 2, leader is broker 3 2, 1, 2, 2, 2, 1, # two replicas: on 1 & 2, both are in ISR set ) raw_data = [ # size of full response (incl. correlation) struct.pack("!i", struct.calcsize(response_format) + 4), struct.pack("!i", 555), # correlation id raw_response ] def get_raw_data(*args): return self.future_value(raw_data.pop(0)) conn = Connection("localhost", 1234) conn.api_correlation = {555: "metadata"} conn.stream = Mock() conn.stream.read_bytes.side_effect = get_raw_data message = yield conn.read_message() expected = metadata.MetadataResponse( brokers=[ metadata.Broker(broker_id=8, host="broker01", port=1234) ], topics=[ metadata.TopicMetadata( error_code=0, name="example.foo", partitions=[ metadata.PartitionMetadata( error_code=0, partition_id=1, leader=1, replicas=[2, 3], isrs=[2] ), metadata.PartitionMetadata( error_code=0, partition_id=2, leader=3, replicas=[1, 2], isrs=[2, 1] ), ] ), ] ) self.assertEqual(message, expected)
def test_read_message(self): response_format = "".join([ "!", "i", "i", "h%dsi" % len("broker01"), # array of brokers "i", "hh%ds" % len("example.foo"), # array of topics "i", # subarray of partitions "hii", "i", "ii", "i", "i", # partition 1 details "hii", "i", "ii", "i", "ii", # partition 2 details ]) raw_response = struct.pack( response_format, 1, # there is 1 broker 8, len("broker01"), b"broker01", 1234, # broker id,host,port 1, # there is 1 topic 0, len("example.foo"), b"example.foo", # topic name, no error 2, # there are 2 topics 0, 1, 1, # partition ID 1, leader is broker 1 2, 2, 3, 1, 2, # two replicas: on 2 & 3, one ISR: broker 2 0, 2, 3, # partition ID 2, leader is broker 3 2, 1, 2, 2, 2, 1, # two replicas: on 1 & 2, both are in ISR set ) raw_data = [ # size of full response (incl. correlation) struct.pack("!i", struct.calcsize(response_format) + 4), struct.pack("!i", 555), # correlation id raw_response ] def get_raw_data(*args): return self.future_value(raw_data.pop(0)) conn = Connection("localhost", 1234) conn.api_correlation = {555: "metadata"} conn.stream = Mock() conn.stream.read_bytes.side_effect = get_raw_data message = yield conn.read_message() expected = metadata.MetadataResponse( brokers=[metadata.Broker(broker_id=8, host="broker01", port=1234)], topics=[ metadata.TopicMetadata( error_code=0, name="example.foo", partitions=[ metadata.PartitionMetadata(error_code=0, partition_id=1, leader=1, replicas=[2, 3], isrs=[2]), metadata.PartitionMetadata(error_code=0, partition_id=2, leader=3, replicas=[1, 2], isrs=[2, 1]), ]), ]) self.assertEqual(message, expected)