Skip to content

Commit

Permalink
Merge pull request #152 from Lancetnik/develop
Browse files Browse the repository at this point in the history
fix: correct FastStream middleware injection to already created subsc…
  • Loading branch information
Tishka17 committed May 14, 2024
2 parents c3718c2 + 5d82662 commit f3bf281
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/integrations/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ With some frameworks we provide an option to inject dependencies in handlers wit
setup_dishka(container, app)
* For **FasStream** (**0.5.0** version and higher) you need to provide ``auto_inject=True`` when calling ``setup_dishka``. It is important here to call it before registering any subscribers or router include:
* For **FasStream** (**0.5.0** version and higher) you need to provide ``auto_inject=True`` when calling ``setup_dishka``. E.g:

.. code-block:: python
Expand Down
12 changes: 12 additions & 0 deletions src/dishka/integrations/faststream.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ def setup_dishka(
*app.broker._middlewares, # noqa: SLF001
)

for subscriber in app.broker._subscribers.values(): # noqa: SLF001
subscriber._broker_middlewares = ( # noqa: SLF001
DishkaMiddleware(container),
*subscriber._broker_middlewares, # noqa: SLF001
)

for publisher in app.broker._publishers.values(): # noqa: SLF001
publisher._broker_middlewares = ( # noqa: SLF001
DishkaMiddleware(container),
*publisher._broker_middlewares, # noqa: SLF001
)

if auto_inject:
app.broker._call_decorators = ( # noqa: SLF001
inject,
Expand Down
48 changes: 47 additions & 1 deletion tests/integrations/faststream/test_faststream.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from dishka import make_async_container
from dishka.integrations.faststream import (
FASTSTREAM_OLD_MIDDLEWARES,
FromDishka,
inject,
setup_dishka,
Expand Down Expand Up @@ -63,11 +64,56 @@ async def get_with_request(
return "passed"



@pytest.mark.asyncio
async def test_request_dependency(app_provider: AppProvider):
async with dishka_app(get_with_request, app_provider) as client:
assert await client.publish("", "test", rpc=True) == "passed"

app_provider.mock.assert_called_with(REQUEST_DEP_VALUE)
app_provider.request_released.assert_called_once()


@pytest.mark.asyncio
@pytest.mark.skipif(
FASTSTREAM_OLD_MIDDLEWARES,
reason="Requires FastStream 0.5.0+",
)
async def test_autoinject_before_subscriber(app_provider: AppProvider):
broker = NatsBroker()
app = FastStream(broker)

container = make_async_container(app_provider)
setup_dishka(container, app=app, auto_inject=True)

broker.subscriber("test")(get_with_request)

async with TestNatsBroker(broker) as br:
assert await br.publish("", "test", rpc=True) == "passed"

app_provider.mock.assert_called_with(REQUEST_DEP_VALUE)
app_provider.request_released.assert_called_once()

await container.close()


@pytest.mark.asyncio
@pytest.mark.skipif(
FASTSTREAM_OLD_MIDDLEWARES,
reason="Requires FastStream 0.5.0+",
)
async def test_autoinject_after_subscriber(app_provider: AppProvider):
broker = NatsBroker()
app = FastStream(broker)

broker.subscriber("test")(get_with_request)

container = make_async_container(app_provider)
setup_dishka(container, app=app, auto_inject=True)

async with TestNatsBroker(broker) as br:
assert await br.publish("", "test", rpc=True) == "passed"

app_provider.mock.assert_called_with(REQUEST_DEP_VALUE)
app_provider.request_released.assert_called_once()

await container.close()

0 comments on commit f3bf281

Please sign in to comment.