Skip to content

Support scenario when broker.connect() is called manually (for example, in lifespan hook) #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/faststream-stomp/faststream_stomp/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def __init__(
name="stomp", default_context={"channel": ""}, message_id_ln=self.__max_msg_id_ln
),
)
self._attempted_to_connect = False

async def start(self) -> None:
await super().start()
Expand All @@ -91,6 +92,9 @@ async def start(self) -> None:
await handler.start()

async def _connect(self, client: stompman.Client) -> stompman.Client: # type: ignore[override]
if self._attempted_to_connect:
return client
self._attempted_to_connect = True
self._producer = StompProducer(client)
return await client.__aenter__()

Expand Down
2 changes: 1 addition & 1 deletion packages/faststream-stomp/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"

[dependency-groups]
dev = ["faststream[otel,prometheus]~=0.5"]
dev = ["faststream[otel,prometheus]~=0.5", "asgi-lifespan"]

[tool.hatch.version]
source = "vcs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import faststream_stomp
import pytest
import stompman
from asgi_lifespan import LifespanManager
from faststream import BaseMiddleware, Context, FastStream
from faststream.asgi import AsgiFastStream
from faststream.broker.message import gen_cor_id
from faststream.broker.middlewares.logging import CriticalLogMiddleware
from faststream.exceptions import AckMessage, NackMessage, RejectMessage
Expand Down Expand Up @@ -217,3 +219,9 @@ def some_handler(message_frame: Annotated[stompman.MessageFrame, Context("messag
mock.call(logging.ERROR, "MyError: ", extra=extra, exc_info=MyError()),
mock.call(logging.INFO, "Processed", extra=extra),
]


async def test_broker_connect_twice(broker: faststream_stomp.StompBroker) -> None:
app = AsgiFastStream(broker, on_startup=[broker.connect])
async with LifespanManager(app):
pass