Skip to content

Commit 53243d5

Browse files
committed
refactor lifespan tests
1 parent 13fa042 commit 53243d5

File tree

4 files changed

+46
-63
lines changed

4 files changed

+46
-63
lines changed

lite_bootstrap/bootstrappers/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111

1212
class BaseBootstrapper(abc.ABC, typing.Generic[ApplicationT]):
13+
SLOTS = "bootstrap_config", "instruments", "is_bootstrapped"
1314
instruments_types: typing.ClassVar[list[type[BaseInstrument]]]
1415
instruments: list[BaseInstrument]
1516
bootstrap_config: BaseConfig
1617

1718
def __init__(self, bootstrap_config: BaseConfig) -> None:
19+
self.is_bootstrapped = False
1820
if not self.is_ready():
1921
raise RuntimeError(self.not_ready_message)
2022

@@ -38,10 +40,12 @@ def _prepare_application(self) -> ApplicationT: ...
3840
def is_ready(self) -> bool: ...
3941

4042
def bootstrap(self) -> ApplicationT:
43+
self.is_bootstrapped = True
4144
for one_instrument in self.instruments:
4245
one_instrument.bootstrap()
4346
return self._prepare_application()
4447

4548
def teardown(self) -> None:
49+
self.is_bootstrapped = False
4650
for one_instrument in self.instruments:
4751
one_instrument.teardown()

tests/test_fastapi_bootstrap.py

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
import asyncio
2-
import contextlib
3-
import dataclasses
4-
import typing
5-
6-
import fastapi
71
import pytest
82
import structlog
93
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
@@ -39,11 +33,10 @@ def fastapi_config() -> FastAPIConfig:
3933
def test_fastapi_bootstrap(fastapi_config: FastAPIConfig) -> None:
4034
bootstrapper = FastAPIBootstrapper(bootstrap_config=fastapi_config)
4135
application = bootstrapper.bootstrap()
42-
test_client = TestClient(application)
43-
36+
assert bootstrapper.is_bootstrapped
4437
logger.info("testing logging", key="value")
4538

46-
try:
39+
with TestClient(application) as test_client:
4740
response = test_client.get(fastapi_config.health_checks_path)
4841
assert response.status_code == status.HTTP_200_OK
4942
assert response.json() == {"health_status": True, "service_name": "microservice", "service_version": "2.0.0"}
@@ -59,8 +52,8 @@ def test_fastapi_bootstrap(fastapi_config: FastAPIConfig) -> None:
5952
response = test_client.get(str(application.redoc_url))
6053
assert response.status_code == status.HTTP_200_OK
6154
assert response.text
62-
finally:
63-
bootstrapper.teardown()
55+
56+
assert not bootstrapper.is_bootstrapped
6457

6558

6659
def test_fastapi_bootstrapper_not_ready() -> None:
@@ -82,21 +75,3 @@ def test_fastapi_bootstrapper_with_missing_instrument_dependency(
8275
) -> None:
8376
with emulate_package_missing(package_name), pytest.warns(UserWarning, match=package_name):
8477
FastAPIBootstrapper(bootstrap_config=fastapi_config)
85-
86-
87-
def test_fastapi_bootstrap_lifespan(fastapi_config: FastAPIConfig) -> None:
88-
@contextlib.asynccontextmanager
89-
async def lifespan_manager(_: fastapi.FastAPI) -> typing.AsyncIterator[dict[str, typing.Any]]:
90-
try:
91-
yield {}
92-
finally:
93-
await asyncio.sleep(0)
94-
95-
fastapi_config = dataclasses.replace(fastapi_config, application=fastapi.FastAPI(lifespan=lifespan_manager))
96-
bootstrapper = FastAPIBootstrapper(bootstrap_config=fastapi_config)
97-
application = bootstrapper.bootstrap()
98-
99-
with TestClient(application) as test_client:
100-
response = test_client.get(fastapi_config.health_checks_path)
101-
assert response.status_code == status.HTTP_200_OK
102-
assert response.json() == {"health_status": True, "service_name": "microservice", "service_version": "2.0.0"}

tests/test_faststream_bootstrap.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,23 @@ async def test_faststream_bootstrap(broker: RedisBroker) -> None:
4545
bootstrap_config = build_faststream_config(broker=broker)
4646
bootstrapper = FastStreamBootstrapper(bootstrap_config=bootstrap_config)
4747
application = bootstrapper.bootstrap()
48-
test_client = TestClient(app=application)
49-
48+
assert bootstrapper.is_bootstrapped
5049
logger.info("testing logging", key="value")
5150

52-
async with TestRedisBroker(broker):
53-
response = test_client.get(bootstrap_config.health_checks_path)
54-
assert response.status_code == status.HTTP_200_OK
55-
assert response.json() == {"health_status": True, "service_name": "microservice", "service_version": "2.0.0"}
51+
with TestClient(app=application) as test_client:
52+
async with TestRedisBroker(broker):
53+
response = test_client.get(bootstrap_config.health_checks_path)
54+
assert response.status_code == status.HTTP_200_OK
55+
assert response.json() == {
56+
"health_status": True,
57+
"service_name": "microservice",
58+
"service_version": "2.0.0",
59+
}
60+
61+
response = test_client.get(bootstrap_config.prometheus_metrics_path)
62+
assert response.status_code == status.HTTP_200_OK
5663

57-
response = test_client.get(bootstrap_config.prometheus_metrics_path)
58-
assert response.status_code == status.HTTP_200_OK
64+
assert not bootstrapper.is_bootstrapped
5965

6066

6167
async def test_faststream_bootstrap_health_check_wo_broker() -> None:

tests/test_litestar_bootstrap.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,30 @@ def litestar_config() -> LitestarConfig:
3333
def test_litestar_bootstrap(litestar_config: LitestarConfig) -> None:
3434
bootstrapper = LitestarBootstrapper(bootstrap_config=litestar_config)
3535
application = bootstrapper.bootstrap()
36-
37-
try:
38-
logger.info("testing logging", key="value")
39-
40-
assert application.cors_config
41-
assert application.cors_config.allow_origins == litestar_config.cors_allowed_origins
42-
43-
with TestClient(app=application) as test_client:
44-
response = test_client.get(litestar_config.health_checks_path)
45-
assert response.status_code == status_codes.HTTP_200_OK
46-
assert response.json() == {
47-
"health_status": True,
48-
"service_name": "microservice",
49-
"service_version": "2.0.0",
50-
}
51-
52-
response = test_client.get(litestar_config.prometheus_metrics_path)
53-
assert response.status_code == status_codes.HTTP_200_OK
54-
assert response.text
55-
56-
response = test_client.get(litestar_config.swagger_path)
57-
assert response.status_code == status_codes.HTTP_200_OK
58-
response = test_client.get(f"{litestar_config.service_static_path}/swagger-ui.css")
59-
assert response.status_code == status_codes.HTTP_200_OK
60-
finally:
61-
bootstrapper.teardown()
36+
assert bootstrapper.is_bootstrapped
37+
logger.info("testing logging", key="value")
38+
assert application.cors_config
39+
assert application.cors_config.allow_origins == litestar_config.cors_allowed_origins
40+
41+
with TestClient(app=application) as test_client:
42+
response = test_client.get(litestar_config.health_checks_path)
43+
assert response.status_code == status_codes.HTTP_200_OK
44+
assert response.json() == {
45+
"health_status": True,
46+
"service_name": "microservice",
47+
"service_version": "2.0.0",
48+
}
49+
50+
response = test_client.get(litestar_config.prometheus_metrics_path)
51+
assert response.status_code == status_codes.HTTP_200_OK
52+
assert response.text
53+
54+
response = test_client.get(litestar_config.swagger_path)
55+
assert response.status_code == status_codes.HTTP_200_OK
56+
response = test_client.get(f"{litestar_config.service_static_path}/swagger-ui.css")
57+
assert response.status_code == status_codes.HTTP_200_OK
58+
59+
assert not bootstrapper.is_bootstrapped
6260

6361

6462
def test_litestar_bootstrapper_not_ready() -> None:

0 commit comments

Comments
 (0)