|
| 1 | +# pylint: skip-file |
| 2 | +from __future__ import absolute_import, division |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from kafka.errors import IllegalStateError, KafkaError |
| 7 | +from kafka.producer.future import FutureRecordMetadata, RecordMetadata |
| 8 | +from kafka.producer.producer_batch import ProducerBatch |
| 9 | +from kafka.record.memory_records import MemoryRecordsBuilder |
| 10 | +from kafka.structs import TopicPartition |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def tp(): |
| 15 | + return TopicPartition('foo', 0) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def memory_records_builder(): |
| 20 | + return MemoryRecordsBuilder(magic=2, compression_type=0, batch_size=100000) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def batch(tp, memory_records_builder): |
| 25 | + return ProducerBatch(tp, memory_records_builder) |
| 26 | + |
| 27 | + |
| 28 | +def test_producer_batch_producer_id(tp, memory_records_builder): |
| 29 | + batch = ProducerBatch(tp, memory_records_builder) |
| 30 | + assert batch.producer_id == -1 |
| 31 | + batch.records.set_producer_state(123, 456, 789, False) |
| 32 | + assert batch.producer_id == 123 |
| 33 | + memory_records_builder.close() |
| 34 | + assert batch.producer_id == 123 |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize("magic", [0, 1, 2]) |
| 38 | +def test_producer_batch_try_append(magic): |
| 39 | + tp = TopicPartition('foo', 0) |
| 40 | + records = MemoryRecordsBuilder( |
| 41 | + magic=magic, compression_type=0, batch_size=100000) |
| 42 | + batch = ProducerBatch(tp, records) |
| 43 | + assert batch.record_count == 0 |
| 44 | + future = batch.try_append(0, b'key', b'value', []) |
| 45 | + assert isinstance(future, FutureRecordMetadata) |
| 46 | + assert not future.is_done |
| 47 | + batch.complete(123, 456) |
| 48 | + assert future.is_done |
| 49 | + # record-level checksum only provided in v0/v1 formats; payload includes magic-byte |
| 50 | + if magic == 0: |
| 51 | + checksum = 592888119 |
| 52 | + elif magic == 1: |
| 53 | + checksum = 213653215 |
| 54 | + else: |
| 55 | + checksum = None |
| 56 | + |
| 57 | + expected_metadata = RecordMetadata( |
| 58 | + topic=tp[0], partition=tp[1], topic_partition=tp, |
| 59 | + offset=123, timestamp=456, checksum=checksum, |
| 60 | + serialized_key_size=3, serialized_value_size=5, serialized_header_size=-1) |
| 61 | + assert future.value == expected_metadata |
| 62 | + |
| 63 | + |
| 64 | +def test_producer_batch_retry(batch): |
| 65 | + assert not batch.in_retry() |
| 66 | + batch.retry() |
| 67 | + assert batch.in_retry() |
| 68 | + |
| 69 | + |
| 70 | +def test_batch_abort(batch): |
| 71 | + future = batch.try_append(123, None, b'msg', []) |
| 72 | + batch.abort(KafkaError()) |
| 73 | + assert future.is_done |
| 74 | + |
| 75 | + # subsequent completion should be ignored |
| 76 | + assert not batch.complete(500, 2342342341) |
| 77 | + assert not batch.complete_exceptionally(KafkaError('top_level'), lambda _: KafkaError('record')) |
| 78 | + |
| 79 | + assert future.is_done |
| 80 | + with pytest.raises(KafkaError): |
| 81 | + future.get() |
| 82 | + |
| 83 | + |
| 84 | +def test_batch_cannot_abort_twice(batch): |
| 85 | + future = batch.try_append(123, None, b'msg', []) |
| 86 | + batch.abort(KafkaError()) |
| 87 | + with pytest.raises(IllegalStateError): |
| 88 | + batch.abort(KafkaError()) |
| 89 | + assert future.is_done |
| 90 | + with pytest.raises(KafkaError): |
| 91 | + future.get() |
| 92 | + |
| 93 | + |
| 94 | +def test_batch_cannot_complete_twice(batch): |
| 95 | + future = batch.try_append(123, None, b'msg', []) |
| 96 | + batch.complete(500, 10) |
| 97 | + with pytest.raises(IllegalStateError): |
| 98 | + batch.complete(1000, 20) |
| 99 | + record_metadata = future.get() |
| 100 | + assert record_metadata.offset == 500 |
| 101 | + assert record_metadata.timestamp == 10 |
| 102 | + |
| 103 | + |
| 104 | +def _test_complete_exceptionally(batch, record_count, top_level_exception, record_exceptions_fn): |
| 105 | + futures = [] |
| 106 | + for i in range(record_count): |
| 107 | + futures.append(batch.try_append(0, b'key', b'value', [])) |
| 108 | + |
| 109 | + assert record_count == batch.record_count |
| 110 | + |
| 111 | + batch.complete_exceptionally(top_level_exception, record_exceptions_fn) |
| 112 | + assert batch.is_done |
| 113 | + |
| 114 | + for i, future in enumerate(futures): |
| 115 | + assert future.is_done |
| 116 | + assert future.failed() |
| 117 | + assert isinstance(future.exception, RuntimeError) |
| 118 | + assert record_exceptions_fn(i) == future.exception |
| 119 | + |
| 120 | + |
| 121 | +def test_complete_exceptionally_with_record_errors(batch): |
| 122 | + record_count = 5 |
| 123 | + top_level_exception = RuntimeError() |
| 124 | + |
| 125 | + record_exceptions_map = {0: RuntimeError(), 3: RuntimeError()} |
| 126 | + record_exceptions_fn = lambda i: record_exceptions_map.get(i, top_level_exception) |
| 127 | + |
| 128 | + _test_complete_exceptionally(batch, record_count, top_level_exception, record_exceptions_fn) |
| 129 | + |
| 130 | + |
| 131 | +def test_complete_exceptionally_with_null_record_errors(batch): |
| 132 | + record_count = 5 |
| 133 | + top_level_exception = RuntimeError() |
| 134 | + |
| 135 | + with pytest.raises(AssertionError): |
| 136 | + _test_complete_exceptionally(batch, record_count, top_level_exception, None) |
0 commit comments