|
| 1 | +# Batch Codec Testing Gap |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +`BatchCodecTestcase` tests the codec in isolation (protocol conformance, `isinstance` dispatch, fallback). It cannot test the full publish→subscribe round trip like `CodecTestcase` does for single messages. |
| 6 | + |
| 7 | +The FakeProducer's `publish_batch` path fails when a custom codec is set because the subscriber pipeline calls `decode_message()` on a `StreamMessage` with `body=list[bytes]`, and `decode_message()` expects `bytes`, not a list. |
| 8 | + |
| 9 | +## Root Cause |
| 10 | + |
| 11 | +Three things are wrong in the batch path: |
| 12 | + |
| 13 | +### 1. `_get_parser_and_decoder()` is not batch-aware |
| 14 | + |
| 15 | +When `codec=` is set, the subscriber wiring always uses `codec.decode` as the decoder — even for batch subscribers. Batch subscribers need `codec.decode_batch` (if the codec implements `BatchCodecProto`). |
| 16 | + |
| 17 | +```python |
| 18 | +# Current (usecase.py _get_parser_and_decoder): |
| 19 | +if codec: |
| 20 | + async_decoder = codec.decode # always single-message decoder |
| 21 | + |
| 22 | +# Needed: |
| 23 | +if codec and is_batch_subscriber and isinstance(codec, BatchCodecProto): |
| 24 | + async_decoder = codec.decode_batch |
| 25 | +elif codec: |
| 26 | + async_decoder = codec.decode |
| 27 | +``` |
| 28 | + |
| 29 | +This requires `_get_parser_and_decoder` to know whether the current subscriber is a batch subscriber. Currently it doesn't — it only sees parser/decoder/codec options, not the subscriber type. |
| 30 | + |
| 31 | +### 2. FakeProducer `publish_batch` builds StreamMessages eagerly |
| 32 | + |
| 33 | +```python |
| 34 | +# Current (kafka/testing.py FakeProducer.publish_batch): |
| 35 | +messages = [await build_message(m, ...) for m in cmd.batch_bodies] |
| 36 | +await self._execute_handler(list(messages), topic, handler) |
| 37 | +``` |
| 38 | + |
| 39 | +It builds individual `ConsumerRecord`s via `build_message()`, then passes the list to the handler. The batch parser (`AioKafkaBatchParser.parse_batch`) expects raw `ConsumerRecord`s, not pre-parsed messages. The FakeProducer should pass raw records and let the batch parser handle the batching. |
| 40 | + |
| 41 | +### 3. `call_item.is_suitable()` always sets single decoder |
| 42 | + |
| 43 | +```python |
| 44 | +# Current (call_item.py): |
| 45 | +message.set_decoder(decoder) # always the single-message decoder |
| 46 | +``` |
| 47 | + |
| 48 | +For batch subscribers, this should set the batch decoder when available. |
| 49 | + |
| 50 | +## Fix (3 touch points) |
| 51 | + |
| 52 | +### Touch 1: `_get_parser_and_decoder()` batch detection |
| 53 | + |
| 54 | +Add batch subscriber detection. Options: |
| 55 | +- Pass a `is_batch: bool` flag from `_build_fastdepends_model` |
| 56 | +- Check if the parser is a batch parser type |
| 57 | +- Add a flag to `_CallOptions` |
| 58 | + |
| 59 | +Then dispatch: |
| 60 | +```python |
| 61 | +if codec and is_batch and isinstance(codec, BatchCodecProto): |
| 62 | + async_decoder = codec.decode_batch |
| 63 | +elif codec: |
| 64 | + async_decoder = codec.decode |
| 65 | +``` |
| 66 | + |
| 67 | +### Touch 2: FakeProducer batch path |
| 68 | + |
| 69 | +Pass raw `ConsumerRecord` tuples to the subscriber pipeline instead of pre-built `StreamMessage`s. The batch parser will handle batching + decoding correctly. |
| 70 | + |
| 71 | +### Touch 3: `call_item.is_suitable()` or `set_decoder` |
| 72 | + |
| 73 | +No change needed if Touch 1 correctly sets the batch decoder — `set_decoder` already stores whatever callable it receives. |
| 74 | + |
| 75 | +## Effort |
| 76 | + |
| 77 | +Small-medium. Three files, ~20 lines of logic. The main complexity is plumbing `is_batch` through to `_get_parser_and_decoder` without polluting the interface. |
| 78 | + |
| 79 | +## When |
| 80 | + |
| 81 | +Separate PR after the current codec encode wiring merges. Not blocking — `BatchCodecTestcase` unit tests verify the protocol dispatch correctly, and the full round-trip for single messages is already covered by `CodecTestcase`. |
0 commit comments