Skip to content

FakeProducer

faststream.kafka.testing.FakeProducer #

FakeProducer(
    broker: KafkaBroker, brokers: Sequence[KafkaBroker]
)

Bases: AioKafkaFastProducer

A fake Kafka producer for testing purposes.

This class extends AioKafkaFastProducer and is used to simulate Kafka message publishing during tests.

Source code in faststream/kafka/testing.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
def __init__(
    self,
    broker: KafkaBroker,
    brokers: Sequence[KafkaBroker],
) -> None:
    self.broker = broker
    self.brokers = brokers

    default = AioKafkaParser(
        msg_class=KafkaMessage,
        regex=None,
    )

    self._parser = ParserComposition(broker._parser, default.parse_message)
    self._decoder = ParserComposition(broker._decoder, default.decode_message)
    self.codec = broker.config.broker_codec or DefaultCodec()

broker instance-attribute #

broker = broker

brokers instance-attribute #

brokers = brokers

codec instance-attribute #

codec = broker.config.broker_codec or DefaultCodec()

subscribers property #

subscribers: Iterable[LogicSubscriber[Any]]

closed property #

closed: bool

publish async #

publish(cmd: KafkaPublishCommand) -> None

Publish a message to the Kafka broker.

Source code in faststream/kafka/testing.py
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
@override
async def publish(self, cmd: "KafkaPublishCommand") -> None:
    """Publish a message to the Kafka broker."""
    incoming = await build_message(
        message=cmd.body,
        topic=cmd.destination,
        key=cmd.key,
        partition=cmd.partition,
        timestamp_ms=cmd.timestamp_ms,
        headers=cmd.headers,
        correlation_id=cmd.correlation_id,
        reply_to=cmd.reply_to,
        serializer=self.broker.config.fd_config._serializer,
        codec=self.codec,
        id_generator=self.broker.config.id_generator,
    )

    for handler in _find_handler(
        self.subscribers,
        cmd.destination,
        cmd.partition,
    ):
        msg_to_send = (
            (incoming,) if isinstance(handler, BatchSubscriber) else incoming
        )

        await self._execute_handler(msg_to_send, cmd.destination, handler)

request async #

Source code in faststream/kafka/testing.py
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
@override
async def request(self, cmd: "KafkaPublishCommand") -> "ConsumerRecord":
    incoming = await build_message(
        message=cmd.body,
        topic=cmd.destination,
        key=cmd.key,
        partition=cmd.partition,
        timestamp_ms=cmd.timestamp_ms,
        headers=cmd.headers,
        correlation_id=cmd.correlation_id,
        serializer=self.broker.config.fd_config._serializer,
        codec=self.codec,
        id_generator=self.broker.config.id_generator,
    )

    for handler in _find_handler(
        self.subscribers,
        cmd.destination,
        cmd.partition,
    ):
        msg_to_send = (
            (incoming,) if isinstance(handler, BatchSubscriber) else incoming
        )

        with anyio.fail_after(cmd.timeout):
            return await self._execute_handler(
                msg_to_send,
                cmd.destination,
                handler,
            )

    raise SubscriberNotFound

publish_batch async #

publish_batch(cmd: KafkaPublishCommand) -> None

Publish a batch of messages to the Kafka broker.

Source code in faststream/kafka/testing.py
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
@override
async def publish_batch(
    self,
    cmd: "KafkaPublishCommand",
) -> None:
    """Publish a batch of messages to the Kafka broker."""
    serializer = self.broker.config.fd_config._serializer

    if isinstance(self.codec, BatchCodecProto):
        encoded = await self.codec.encode_batch(cmd.batch_bodies, serializer)
    else:
        encoded = [
            await self.codec.encode(body, serializer) for body in cmd.batch_bodies
        ]

    for handler in _find_handler(
        self.subscribers,
        cmd.destination,
        cmd.partition,
    ):
        messages = [
            _build_record(
                body=body,
                content_type=content_type,
                topic=cmd.destination,
                partition=cmd.partition,
                timestamp_ms=cmd.timestamp_ms,
                key=cmd.key_for(message_position),
                headers=cmd.headers,
                correlation_id=cmd.correlation_id,
                reply_to=cmd.reply_to,
                id_generator=self.broker.config.id_generator,
            )
            for message_position, (body, content_type) in enumerate(encoded)
        ]

        if isinstance(handler, BatchSubscriber):
            await self._execute_handler(tuple(messages), cmd.destination, handler)

        else:
            for m in messages:
                await self._execute_handler(m, cmd.destination, handler)

connect async #

connect(
    producer: AIOKafkaProducer,
    serializer: Optional[SerializerProto],
    codec: Optional[CodecProto] = None,
) -> None
Source code in faststream/kafka/publisher/producer.py
32
33
34
35
36
37
async def connect(
    self,
    producer: "AIOKafkaProducer",
    serializer: Optional["SerializerProto"],
    codec: Optional["CodecProto"] = None,
) -> None: ...

disconnect async #

disconnect() -> None
Source code in faststream/kafka/publisher/producer.py
39
async def disconnect(self) -> None: ...

flush async #

flush() -> None
Source code in faststream/kafka/publisher/producer.py
48
49
async def flush(self) -> None:  # ruff: ignore[no-self-use]
    return None