Coverage for tests/cli/test_run_asgi.py: 95%
41 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
1import random
2import time
4import httpx2
5import psutil
6import pytest
8from tests.cli import interfaces
9from tests.marks import skip_windows
12@pytest.mark.slow()
13@skip_windows
14def test_run(
15 generate_template: interfaces.GenerateTemplateFactory,
16 faststream_cli: interfaces.FastStreamCLIFactory,
17) -> None:
18 app_code = """
19 import json
21 from faststream import FastStream, specification
22 from faststream.rabbit import RabbitBroker
23 from faststream.asgi import AsgiResponse, get
25 CONTEXT = {}
27 @get
28 async def context(scope):
29 return AsgiResponse(json.dumps(CONTEXT).encode(), status_code=200)
31 broker = RabbitBroker()
32 app = FastStream(broker, specification=specification.AsyncAPI()).as_asgi(
33 asgi_routes=[
34 ("/context", context),
35 ("/liveness", AsgiResponse(b"hello world", status_code=200)),
36 ],
37 asyncapi_path="/docs",
38 )
40 @app.on_startup
41 async def start(test: int, port: int) -> None:
42 CONTEXT["test"] = test
43 CONTEXT["port"] = port
44 """
45 port = random.randrange(40000, 65535)
46 extra_param = random.randrange(1, 100)
48 with (
49 generate_template(app_code) as app_path,
50 faststream_cli(
51 "faststream",
52 "run",
53 f"{app_path.stem}:app",
54 "--port",
55 f"{port}",
56 "--test",
57 f"{extra_param}",
58 readiness_url=f"http://127.0.0.1:{port}/liveness",
59 ),
60 ):
61 # Test liveness
62 response = httpx2.get(f"http://127.0.0.1:{port}/liveness")
63 assert response.text == "hello world"
64 assert response.status_code == 200
66 # Test documentation
67 response = httpx2.get(f"http://127.0.0.1:{port}/docs")
68 assert response.text.strip().startswith("<!DOCTYPE html>")
69 assert len(response.text) > 1200
71 # Test extra context
72 response = httpx2.get(f"http://127.0.0.1:{port}/context")
73 assert response.json() == {"test": extra_param, "port": port}
74 assert response.status_code == 200
77@pytest.mark.slow()
78@skip_windows
79def test_single_worker(
80 generate_template: interfaces.GenerateTemplateFactory,
81 faststream_cli: interfaces.FastStreamCLIFactory,
82) -> None:
83 app_code = """
84 from faststream.asgi import AsgiFastStream, AsgiResponse
85 from faststream.nats import NatsBroker
87 broker = NatsBroker()
89 app = AsgiFastStream(broker, asgi_routes=[
90 ("/liveness", AsgiResponse(b"hello world", status_code=200)),
91 ])
92 """
93 with (
94 generate_template(app_code) as app_path,
95 faststream_cli(
96 "faststream",
97 "run",
98 f"{app_path.stem}:app",
99 "--workers",
100 "1",
101 readiness_url="http://127.0.0.1:8000/liveness",
102 ),
103 ):
104 response = httpx2.get("http://127.0.0.1:8000/liveness")
105 assert response.text == "hello world"
106 assert response.status_code == 200
109@pytest.mark.slow()
110@skip_windows
111@pytest.mark.flaky(reruns=3, reruns_delay=1)
112def test_many_workers(
113 generate_template: interfaces.GenerateTemplateFactory,
114 faststream_cli: interfaces.FastStreamCLIFactory,
115) -> None:
116 app_code = """
117 from faststream.asgi import AsgiFastStream
118 from faststream.nats import NatsBroker
120 app = AsgiFastStream(NatsBroker())
121 """
123 workers = 2
125 with (
126 generate_template(app_code) as app_path,
127 faststream_cli(
128 "faststream",
129 "run",
130 f"{app_path.stem}:app",
131 "--workers",
132 str(workers),
133 ) as cli_thread,
134 ):
135 process = psutil.Process(pid=cli_thread.process.pid)
136 expected = workers + 1 # 1 for the main process
138 # uvicorn logs its first line before it spawns the workers,
139 # so the CLI counts as started while they are still coming up
140 deadline = time.monotonic() + 10.0
141 while len(process.children()) < expected and time.monotonic() < deadline: 141 ↛ 142line 141 didn't jump to line 142 because the condition on line 141 was never true
142 time.sleep(0.1)
144 assert len(process.children()) == expected
147@pytest.mark.slow()
148@skip_windows
149def test_factory(
150 generate_template: interfaces.GenerateTemplateFactory,
151 faststream_cli: interfaces.FastStreamCLIFactory,
152) -> None:
153 app_code = """
154 from faststream.asgi import AsgiFastStream, AsgiResponse, get
155 from faststream.nats import NatsBroker
157 broker = NatsBroker()
159 def app_factory():
160 return AsgiFastStream(broker, asgi_routes=[
161 ("/liveness", AsgiResponse(b"hello world", status_code=200)),
162 ])
163 """
165 with (
166 generate_template(app_code) as app_path,
167 faststream_cli(
168 "faststream",
169 "run",
170 f"{app_path.stem}:app_factory",
171 "--factory",
172 readiness_url="http://127.0.0.1:8000/liveness",
173 ),
174 ):
175 response = httpx2.get("http://127.0.0.1:8000/liveness")
176 assert response.text == "hello world"
177 assert response.status_code == 200