1
+ import typing
2
+
1
3
import pytest
2
4
import structlog
5
+ from faststream .broker .core .usecase import BrokerUsecase
3
6
from faststream .redis import RedisBroker , TestRedisBroker
4
7
from faststream .redis .opentelemetry import RedisTelemetryMiddleware
5
8
from faststream .redis .prometheus import RedisPrometheusMiddleware
6
9
from opentelemetry .sdk .trace .export import ConsoleSpanExporter
7
10
from starlette import status
8
11
from starlette .testclient import TestClient
9
12
10
- from lite_bootstrap import FastStreamBootstrapper , FastStreamConfig , import_checker
11
- from tests .conftest import CustomInstrumentor
13
+ from lite_bootstrap import FastStreamBootstrapper , FastStreamConfig
14
+ from tests .conftest import CustomInstrumentor , emulate_package_missing
12
15
13
16
14
17
logger = structlog .getLogger (__name__ )
@@ -19,73 +22,68 @@ def broker() -> RedisBroker:
19
22
return RedisBroker ()
20
23
21
24
22
- async def test_faststream_bootstrap (broker : RedisBroker ) -> None :
23
- prometheus_metrics_path = "/custom-metrics/"
24
- health_check_path = "/custom-health/"
25
- bootstrapper = FastStreamBootstrapper (
26
- bootstrap_config = FastStreamConfig (
27
- broker = broker ,
28
- service_name = "microservice" ,
29
- service_version = "2.0.0" ,
30
- service_environment = "test" ,
31
- service_debug = False ,
32
- opentelemetry_endpoint = "otl" ,
33
- opentelemetry_instrumentors = [CustomInstrumentor ()],
34
- opentelemetry_span_exporter = ConsoleSpanExporter (),
35
- opentelemetry_middleware_cls = RedisTelemetryMiddleware ,
36
- prometheus_metrics_path = prometheus_metrics_path ,
37
- prometheus_middleware_cls = RedisPrometheusMiddleware ,
38
- sentry_dsn = "https://testdsn@localhost/1" ,
39
- health_checks_path = health_check_path ,
40
- logging_buffer_capacity = 0 ,
41
- ),
25
+ def build_faststream_config (broker : BrokerUsecase [typing .Any , typing .Any ] | None = None ) -> FastStreamConfig :
26
+ return FastStreamConfig (
27
+ service_name = "microservice" ,
28
+ service_version = "2.0.0" ,
29
+ service_environment = "test" ,
30
+ service_debug = False ,
31
+ opentelemetry_endpoint = "otl" ,
32
+ opentelemetry_instrumentors = [CustomInstrumentor ()],
33
+ opentelemetry_span_exporter = ConsoleSpanExporter (),
34
+ opentelemetry_middleware_cls = RedisTelemetryMiddleware ,
35
+ prometheus_metrics_path = "/custom-metrics/" ,
36
+ prometheus_middleware_cls = RedisPrometheusMiddleware ,
37
+ sentry_dsn = "https://testdsn@localhost/1" ,
38
+ health_checks_path = "/custom-health/" ,
39
+ logging_buffer_capacity = 0 ,
40
+ broker = broker ,
42
41
)
42
+
43
+
44
+ async def test_faststream_bootstrap (broker : RedisBroker ) -> None :
45
+ bootstrap_config = build_faststream_config (broker = broker )
46
+ bootstrapper = FastStreamBootstrapper (bootstrap_config = bootstrap_config )
43
47
application = bootstrapper .bootstrap ()
44
48
test_client = TestClient (app = application )
45
49
46
50
logger .info ("testing logging" , key = "value" )
47
51
48
52
async with TestRedisBroker (broker ):
49
- response = test_client .get (health_check_path )
53
+ response = test_client .get (bootstrap_config . health_checks_path )
50
54
assert response .status_code == status .HTTP_200_OK
51
55
assert response .json () == {"health_status" : True , "service_name" : "microservice" , "service_version" : "2.0.0" }
52
56
53
- response = test_client .get (prometheus_metrics_path )
57
+ response = test_client .get (bootstrap_config . prometheus_metrics_path )
54
58
assert response .status_code == status .HTTP_200_OK
55
59
56
60
57
61
async def test_faststream_bootstrap_health_check_wo_broker () -> None :
58
- health_check_path = "/custom-health-check-path"
59
- bootstrapper = FastStreamBootstrapper (
60
- bootstrap_config = FastStreamConfig (
61
- service_name = "microservice" ,
62
- service_version = "2.0.0" ,
63
- service_environment = "test" ,
64
- service_debug = False ,
65
- opentelemetry_endpoint = "otl" ,
66
- opentelemetry_instrumentors = [CustomInstrumentor ()],
67
- opentelemetry_span_exporter = ConsoleSpanExporter (),
68
- opentelemetry_middleware_cls = RedisTelemetryMiddleware ,
69
- prometheus_middleware_cls = RedisPrometheusMiddleware ,
70
- sentry_dsn = "https://testdsn@localhost/1" ,
71
- health_checks_path = health_check_path ,
72
- logging_buffer_capacity = 0 ,
73
- ),
74
- )
62
+ bootstrap_config = build_faststream_config ()
63
+ bootstrapper = FastStreamBootstrapper (bootstrap_config = bootstrap_config )
75
64
application = bootstrapper .bootstrap ()
76
65
test_client = TestClient (app = application )
77
66
78
- response = test_client .get (health_check_path )
67
+ response = test_client .get (bootstrap_config . health_checks_path )
79
68
assert response .status_code == status .HTTP_500_INTERNAL_SERVER_ERROR
80
69
assert response .text == "Service is unhealthy"
81
70
82
71
83
72
def test_faststream_bootstrapper_not_ready () -> None :
84
- import_checker .is_faststream_installed = False
85
- try :
86
- with pytest .raises (RuntimeError , match = "faststream is not installed" ):
87
- FastStreamBootstrapper (
88
- bootstrap_config = FastStreamConfig (),
89
- )
90
- finally :
91
- import_checker .is_faststream_installed = True
73
+ with emulate_package_missing ("faststream" ), pytest .raises (RuntimeError , match = "faststream is not installed" ):
74
+ FastStreamBootstrapper (bootstrap_config = FastStreamConfig ())
75
+
76
+
77
+ @pytest .mark .parametrize (
78
+ "package_name" ,
79
+ [
80
+ "opentelemetry" ,
81
+ "sentry_sdk" ,
82
+ "structlog" ,
83
+ "prometheus_client" ,
84
+ ],
85
+ )
86
+ def test_faststream_bootstrapper_with_missing_instrument_dependency (broker : RedisBroker , package_name : str ) -> None :
87
+ bootstrap_config = build_faststream_config (broker = broker )
88
+ with emulate_package_missing (package_name ), pytest .warns (UserWarning , match = package_name ):
89
+ FastStreamBootstrapper (bootstrap_config = bootstrap_config )
0 commit comments