-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add FastStream framework integration (#133)
* tests: make FastAPI & Litestar tests optional * docs: add FastStream integration
- Loading branch information
Showing
7 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Usage with `FastStream` | ||
|
||
```python | ||
import datetime | ||
import contextlib | ||
import typing | ||
|
||
from faststream import FastStream, Depends, Logger | ||
from faststream.rabbit import RabbitBroker | ||
|
||
from tests import container | ||
|
||
|
||
@contextlib.asynccontextmanager | ||
async def lifespan_manager() -> typing.AsyncIterator[None]: | ||
try: | ||
yield | ||
finally: | ||
await container.DIContainer.tear_down() | ||
|
||
|
||
broker = RabbitBroker() | ||
app = FastStream(broker, lifespan=lifespan_manager) | ||
|
||
|
||
@broker.subscriber("in") | ||
async def read_root( | ||
logger: Logger, | ||
some_dependency: typing.Annotated[ | ||
container.DependentFactory, | ||
Depends(container.DIContainer.dependent_factory) | ||
], | ||
) -> datetime.datetime: | ||
startup_time = some_dependency.async_resource | ||
logger.info(startup_time) | ||
return startup_time | ||
|
||
|
||
@app.after_startup | ||
async def t() -> None: | ||
await broker.publish(None, "in") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import pytest | ||
|
||
|
||
pytest.importorskip("faststream") | ||
pytest.importorskip("nats") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import datetime | ||
import typing | ||
|
||
from faststream import Depends | ||
from faststream.nats import NatsBroker, TestNatsBroker | ||
|
||
from tests import container | ||
|
||
|
||
broker = NatsBroker() | ||
|
||
TEST_SUBJECT = "test" | ||
|
||
|
||
@broker.subscriber(TEST_SUBJECT) | ||
async def index_subscruber( | ||
dependency: typing.Annotated[ | ||
container.DependentFactory, | ||
Depends(container.DIContainer.dependent_factory), | ||
], | ||
free_dependency: typing.Annotated[ | ||
container.FreeFactory, | ||
Depends(container.DIContainer.resolver(container.FreeFactory)), | ||
], | ||
singleton: typing.Annotated[ | ||
container.SingletonFactory, | ||
Depends(container.DIContainer.singleton), | ||
], | ||
singleton_attribute: typing.Annotated[bool, Depends(container.DIContainer.singleton.dep1)], | ||
) -> datetime.datetime: | ||
assert dependency.sync_resource == free_dependency.dependent_factory.sync_resource | ||
assert dependency.async_resource == free_dependency.dependent_factory.async_resource | ||
assert singleton.dep1 is True | ||
assert singleton_attribute is True | ||
return dependency.async_resource | ||
|
||
|
||
async def test_read_main() -> None: | ||
async with TestNatsBroker(broker) as br: | ||
result = await br.request(None, TEST_SUBJECT) | ||
|
||
result_str = typing.cast(str, await result.decode()) | ||
assert ( | ||
datetime.datetime.fromisoformat(result_str.replace("Z", "+00:00")) | ||
== await container.DIContainer.async_resource() | ||
) |
50 changes: 50 additions & 0 deletions
50
tests/integrations/faststream/test_faststream_di_pass_message.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import typing | ||
|
||
from faststream import BaseMiddleware, Context, Depends | ||
from faststream.broker.message import StreamMessage | ||
from faststream.nats import NatsBroker, TestNatsBroker | ||
from faststream.nats.message import NatsMessage | ||
|
||
from that_depends import BaseContainer, container_context, fetch_context_item, providers | ||
|
||
|
||
class ContextMiddleware(BaseMiddleware): | ||
async def consume_scope( | ||
self, | ||
call_next: typing.Callable[..., typing.Awaitable[typing.Any]], | ||
msg: StreamMessage[typing.Any], | ||
) -> typing.Any: # noqa: ANN401 | ||
async with container_context({"request": msg}): | ||
return await call_next(msg) | ||
|
||
|
||
broker = NatsBroker(middlewares=(ContextMiddleware,), validate=False) | ||
|
||
TEST_SUBJECT = "test" | ||
|
||
|
||
class DIContainer(BaseContainer): | ||
context_request = providers.Factory( | ||
lambda: fetch_context_item("request"), | ||
) | ||
|
||
|
||
@broker.subscriber(TEST_SUBJECT) | ||
async def index_subscruber( | ||
context_request: typing.Annotated[ | ||
NatsMessage, | ||
Depends(DIContainer.context_request, cast=False), | ||
], | ||
message: typing.Annotated[ | ||
NatsMessage, | ||
Context(), | ||
], | ||
) -> bool: | ||
return message is context_request | ||
|
||
|
||
async def test_read_main() -> None: | ||
async with TestNatsBroker(broker) as br: | ||
result = await br.request(None, TEST_SUBJECT) | ||
|
||
assert (await result.decode()) is True |