Skip to content

FakeProducer

faststream.confluent.testing.FakeProducer #

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

Bases: AsyncConfluentFastProducer

A fake Kafka producer for testing purposes.

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

Source code in faststream/confluent/testing.py
148
149
150
151
152
153
154
155
156
157
158
159
def __init__(
    self,
    broker: KafkaBroker,
    brokers: Sequence[KafkaBroker],
) -> None:
    self.broker = broker
    self.brokers = brokers

    default = AsyncConfluentParser()
    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]]

ping async #

ping(timeout: float) -> bool
Source code in faststream/confluent/testing.py
170
171
172
@override
async def ping(self, timeout: float) -> bool:
    return True

publish async #

publish(cmd: KafkaPublishCommand) -> None

Publish a message to the Kafka broker.

Source code in faststream/confluent/testing.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
@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)

publish_batch async #

publish_batch(cmd: KafkaPublishCommand) -> None

Publish a batch of messages to the Kafka broker.

Source code in faststream/confluent/testing.py
202
203
204
205
206
207
208
209
210
211
212
213
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
@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_mock_message(
                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)

request async #

Source code in faststream/confluent/testing.py
242
243
244
245
246
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
@override
async def request(self, cmd: "KafkaPublishCommand") -> "MockConfluentMessage":
    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

connect #

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

disconnect async #

disconnect() -> None
Source code in faststream/confluent/publisher/producer.py
42
43
async def disconnect(self) -> None:  # ruff: ignore[no-self-use]
    return None

flush async #

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