Skip to content

FakeProducer

faststream.nats.testing.FakeProducer #

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

Bases: NatsFastProducer

Source code in faststream/nats/testing.py
def __init__(
    self,
    broker: NatsBroker,
    brokers: Sequence[NatsBroker],
) -> None:
    self.broker = broker
    self.brokers = brokers

    default = NatsParser(pattern="", is_ack_disabled=True)
    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]]

publish async #

publish(cmd: NatsPublishCommand) -> None
Source code in faststream/nats/testing.py
@override
async def publish(self, cmd: "NatsPublishCommand") -> None:
    incoming = await build_message(
        message=cmd.body,
        subject=cmd.destination,
        headers=cmd.headers,
        correlation_id=cmd.correlation_id,
        reply_to=cmd.reply_to,
        serializer=self.broker.config.fd_config._serializer,
        codec=self.codec,
    )

    for handler in _find_handler(
        self.subscribers,
        cmd.destination,
        cmd.stream,
    ):
        msg: list[PatchedMessage] | PatchedMessage

        if (pull := getattr(handler, "pull_sub", None)) and pull.batch:
            msg = [incoming]
        else:
            msg = incoming

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

request async #

Source code in faststream/nats/testing.py
@override
async def request(self, cmd: "NatsPublishCommand") -> "PatchedMessage":
    incoming = await build_message(
        message=cmd.body,
        subject=cmd.destination,
        headers=cmd.headers,
        correlation_id=cmd.correlation_id,
        serializer=self.broker.config.fd_config._serializer,
        codec=self.codec,
    )

    for handler in _find_handler(
        self.subscribers,
        cmd.destination,
        cmd.stream,
    ):
        msg: list[PatchedMessage] | PatchedMessage

        if (pull := getattr(handler, "pull_sub", None)) and pull.batch:
            msg = [incoming]
        else:
            msg = incoming

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

    raise SubscriberNotFound

publish_batch async #

publish_batch(cmd: NatsPublishCommand) -> None
Source code in faststream/nats/publisher/producer.py
async def publish_batch(self, cmd: "NatsPublishCommand") -> None:
    msg = "NATS doesn't support publishing in batches."
    raise FeatureNotSupportedException(msg)

connect #

connect(
    connection: Any,
    serializer: Optional[SerializerProto],
    codec: Optional[CodecProto] = None,
) -> None
Source code in faststream/nats/publisher/producer.py
def connect(
    self,
    connection: Any,
    serializer: Optional["SerializerProto"],
    codec: Optional["CodecProto"] = None,
) -> None: ...

disconnect #

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