|
3 | 3 | import faststream |
4 | 4 | from faststream import TestApp |
5 | 5 | from faststream.nats import NatsBroker, TestNatsBroker |
| 6 | +from modern_di import Container |
6 | 7 |
|
7 | | -from modern_di_faststream import FromDI, fetch_di_container |
8 | | -from tests.dependencies import Dependencies, SimpleCreator |
| 8 | +from modern_di_faststream import FromDI, fetch_di_container, setup_di |
| 9 | +from tests.dependencies import Dependencies, Finalizable, SimpleCreator |
9 | 10 |
|
10 | 11 |
|
11 | 12 | TEST_SUBJECT = "test" |
@@ -43,3 +44,54 @@ async def index_subscriber( |
43 | 44 | async with TestApp(app): |
44 | 45 | assert not container.closed |
45 | 46 | await br.publish("test", TEST_SUBJECT) |
| 47 | + |
| 48 | + |
| 49 | +def _unmanaged_app() -> tuple[faststream.FastStream, NatsBroker, Container, list[Finalizable]]: |
| 50 | + broker = NatsBroker() |
| 51 | + app = faststream.FastStream(broker) |
| 52 | + container = setup_di(app, Container(groups=[Dependencies]), manage_lifespan=False) |
| 53 | + request_instances: list[Finalizable] = [] |
| 54 | + |
| 55 | + @broker.subscriber(TEST_SUBJECT) |
| 56 | + async def index_subscriber( |
| 57 | + message: str, |
| 58 | + app_instance: typing.Annotated[Finalizable, FromDI(Dependencies.app_finalizable)], |
| 59 | + request_instance: typing.Annotated[Finalizable, FromDI(Dependencies.request_finalizable)], |
| 60 | + ) -> None: |
| 61 | + assert message == "test" |
| 62 | + assert app_instance is container.resolve_provider(Dependencies.app_finalizable) |
| 63 | + request_instances.append(request_instance) |
| 64 | + |
| 65 | + return app, broker, container, request_instances |
| 66 | + |
| 67 | + |
| 68 | +async def test_manage_lifespan_false_leaves_the_root_open_after_shutdown() -> None: |
| 69 | + app, broker, container, _ = _unmanaged_app() |
| 70 | + |
| 71 | + async with TestNatsBroker(broker) as br, TestApp(app): |
| 72 | + await br.publish("test", TEST_SUBJECT) |
| 73 | + |
| 74 | + assert not container.closed |
| 75 | + assert container.resolve_provider(Dependencies.app_finalizable).finalized == 0 |
| 76 | + |
| 77 | + |
| 78 | +async def test_manage_lifespan_false_still_builds_and_closes_the_request_container() -> None: |
| 79 | + app, broker, _, request_instances = _unmanaged_app() |
| 80 | + |
| 81 | + async with TestNatsBroker(broker) as br, TestApp(app): |
| 82 | + await br.publish("test", TEST_SUBJECT) |
| 83 | + |
| 84 | + assert [instance.finalized for instance in request_instances] == [1] |
| 85 | + |
| 86 | + |
| 87 | +async def test_manage_lifespan_false_lets_the_user_own_the_root() -> None: |
| 88 | + app, broker, container, _ = _unmanaged_app() |
| 89 | + |
| 90 | + async with container: |
| 91 | + async with TestNatsBroker(broker) as br, TestApp(app): |
| 92 | + await br.publish("test", TEST_SUBJECT) |
| 93 | + app_instance = container.resolve_provider(Dependencies.app_finalizable) |
| 94 | + assert app_instance.finalized == 0 |
| 95 | + |
| 96 | + assert container.closed |
| 97 | + assert app_instance.finalized == 1 |
0 commit comments