Skip to content
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

feat: delayed broker setting #1931

Draft
wants to merge 4 commits into
base: 0.6.0
Choose a base branch
from
Draft
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
29 changes: 24 additions & 5 deletions faststream/_internal/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
fake_context,
to_async,
)
from faststream.exceptions import SetupError

if TYPE_CHECKING:
from fast_depends.library.serializer import SerializerProto
Expand Down Expand Up @@ -78,7 +79,7 @@ async def catch_startup_validation_error() -> AsyncIterator[None]:
class StartAbleApplication:
def __init__(
self,
broker: "BrokerUsecase[Any, Any]",
broker: Optional["BrokerUsecase[Any, Any]"] = None,
/,
provider: Optional["Provider"] = None,
serializer: Optional["SerializerProto"] = EMPTY,
Expand All @@ -91,7 +92,7 @@ def __init__(

def _init_setupable_( # noqa: PLW3201
self,
broker: "BrokerUsecase[Any, Any]",
broker: Optional["BrokerUsecase[Any, Any]"] = None,
/,
provider: Optional["Provider"] = None,
serializer: Optional["SerializerProto"] = EMPTY,
Expand All @@ -115,21 +116,39 @@ def _init_setupable_( # noqa: PLW3201
)
)

self.broker = broker
self.brokers = [broker] if broker else []

self._setup()

def _setup(self) -> None:
self.broker._setup(OuterBrokerState(di_state=self._state.di_state))
if self.broker:
self.broker._setup(OuterBrokerState(di_state=self._state.di_state))

async def _start_broker(self) -> None:
assert self.broker, "You should setup a broker"
await self.broker.start()

@property
def broker(self) -> Optional["BrokerUsecase[Any, Any]"]:
return self.brokers[0] if self.brokers else None

def set_broker(self, broker: "BrokerUsecase[Any, Any]") -> None:
"""Set already existed App object broker.

Useful then you create/init broker in `on_startup` hook.
"""
if self.brokers:
msg = f"`{self}` already has a broker. You can't use multiple brokers until 1.0.0 release."
raise SetupError(msg)

self.brokers.append(broker)
self._setup()


class Application(StartAbleApplication):
def __init__(
self,
broker: "BrokerUsecase[Any, Any]",
broker: Optional["BrokerUsecase[Any, Any]"] = None,
/,
logger: Optional["LoggerProto"] = logger,
provider: Optional["Provider"] = None,
Expand Down
2 changes: 1 addition & 1 deletion faststream/asgi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class AsgiFastStream(Application):

def __init__(
self,
broker: "BrokerUsecase[Any, Any]",
broker: Optional["BrokerUsecase[Any, Any]"] = None,
/,
asgi_routes: Sequence[tuple[str, "ASGIApp"]] = (),
# regular broker args
Expand Down
Empty file added tests/application/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions tests/application/test_delayed_broker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest

from faststream._internal.application import StartAbleApplication
from faststream.rabbit import RabbitBroker


def test_set_broker() -> None:
app = StartAbleApplication()

assert app.broker is None

broker = RabbitBroker()
app.set_broker(broker)

assert app.broker is broker


@pytest.mark.asyncio()
async def test_start_not_setup_broker() -> None:
app = StartAbleApplication()

with pytest.raises(AssertionError, match="You should setup a broker"):
await app._start_broker()
Loading