Debian Codegraph @

tests/test_client.py

raw symbols SCIP
1 from __future__ import annotations
2
3 import asyncio
4 import base64
5 from collections.abc import Awaitable, Callable
6 from typing import Any, cast
7
8 import aiohttp
9 import pytest
10 from aiohttp import ClientSession, web
11 from aiohttp.test_utils import TestClient, TestServer
12
13 from rtsp_to_webrtc.client import get_adaptive_client, get_diagnostics
14 from rtsp_to_webrtc.exceptions import ClientError
15
16 OFFER_SDP = "v=0\r\no=carol 28908764872 28908764872 IN IP4 100.3.6.6\r\n..."
17 ANSWER_SDP = "v=0\r\no=bob 2890844730 2890844730 IN IP4 h.example.com\r\n..."
18 ANSWER_PAYLOAD = base64.b64encode(ANSWER_SDP.encode("utf-8")).decode("utf-8")
19 RTSP_URL = "rtsp://example"
20 STREAM_1 = {
21 "name": "test video",
22 "channels": {
23 "0": {
24 "name": "ch1",
25 "url": "rtsp://example",
26 },
27 "1": {
28 "name": "ch2",
29 "url": "rtsp://example",
30 },
31 },
32 }
33 SUCCESS_RESPONSE = {
34 "status": 1,
35 "payload": "success",
36 }
37
38
39 @pytest.fixture
40 def event_loop() -> Any:
41 loop = asyncio.get_event_loop()
42 yield loop
43
44
45 @pytest.fixture
46 def cli_cb(
47 loop: Any,
48 app: web.Application,
49 aiohttp_client: Callable[[web.Application], Awaitable[TestClient]],
50 ) -> Callable[[], Awaitable[TestClient]]:
51 """Creates a fake aiohttp client."""
52
53 async def func() -> TestClient:
54 return await aiohttp_client(app)
55
56 return func
57
58
59 async def test_adaptive_web_client(
60 cli_cb: Callable[[], Awaitable[TestClient]],
61 app: web.Application,
62 request_handler: Callable[[aiohttp.web.Request], Awaitable[aiohttp.web.Response]],
63 ) -> None:
64 """Test adaptive client picks Web when both succeed."""
65 app.router.add_get("/streams", request_handler)
66 app.router.add_get("/static", request_handler)
67 app.router.add_post("/stream/{stream_id}/add", request_handler)
68 app.router.add_post(
69 "/stream/{stream_id}/channel/{channel_id}/webrtc", request_handler
70 )
71 cli = await cli_cb()
72 assert isinstance(cli.server, TestServer)
73 # Web heartbeat
74 cli.server.app["response"].append(
75 aiohttp.web.json_response(
76 {
77 "status": 1,
78 "payload": {
79 "demo1": STREAM_1,
80 },
81 }
82 )
83 )
84 # WebRTC heartbeat
85 cli.server.app["response"].append(
86 aiohttp.web.Response(status=404),
87 )
88 # List call
89 cli.server.app["response"].append(
90 aiohttp.web.json_response(
91 {
92 "status": 1,
93 "payload": {},
94 }
95 )
96 )
97 # Add stream
98 cli.server.app["response"].append(aiohttp.web.json_response(SUCCESS_RESPONSE))
99 # Web Offer
100 cli.server.app["response"].append(aiohttp.web.Response(body=ANSWER_PAYLOAD))
101
102 client = await get_adaptive_client(cast(ClientSession, cli))
103
104 answer_sdp = await client.offer(OFFER_SDP, RTSP_URL)
105 assert answer_sdp == ANSWER_SDP
106
107 assert get_diagnostics() == {
108 "discovery": {"attempt": 1, "web.success": 1, "webrtc.failure": 1},
109 "web": {
110 "add_stream.request": 1,
111 "add_stream.success": 1,
112 "heartbeat.request": 1,
113 "heartbeat.success": 1,
114 "list_streams.request": 1,
115 "list_streams.success": 1,
116 "webrtc.request": 1,
117 "webrtc.success": 1,
118 },
119 "webrtc": {"heartbeat.request": 1, "heartbeat.response_error": 1},
120 }
121
122
123 async def test_adaptive_both_succeed_web_client(
124 cli_cb: Callable[[], Awaitable[TestClient]],
125 app: web.Application,
126 request_handler: Callable[[aiohttp.web.Request], Awaitable[aiohttp.web.Response]],
127 ) -> None:
128 """Test adaptive client picks Web when both succeed."""
129 app.router.add_get("/streams", request_handler)
130 app.router.add_get("/static", request_handler)
131 app.router.add_post("/stream/{stream_id}/add", request_handler)
132 app.router.add_post(
133 "/stream/{stream_id}/channel/{channel_id}/webrtc", request_handler
134 )
135 cli = await cli_cb()
136 assert isinstance(cli.server, TestServer)
137 # Web heartbeat
138 cli.server.app["response"].append(
139 aiohttp.web.json_response(
140 {
141 "status": 1,
142 "payload": {
143 "demo1": STREAM_1,
144 },
145 }
146 )
147 )
148 # WebRTC heartbeat
149 cli.server.app["response"].append(
150 aiohttp.web.Response(status=200),
151 )
152 # List call
153 cli.server.app["response"].append(
154 aiohttp.web.json_response(
155 {
156 "status": 1,
157 "payload": {},
158 }
159 )
160 )
161 # Add stream
162 cli.server.app["response"].append(aiohttp.web.json_response(SUCCESS_RESPONSE))
163 # Web Offer
164 cli.server.app["response"].append(aiohttp.web.Response(body=ANSWER_PAYLOAD))
165
166 client = await get_adaptive_client(cast(ClientSession, cli))
167
168 answer_sdp = await client.offer(OFFER_SDP, RTSP_URL)
169 assert answer_sdp == ANSWER_SDP
170
171
172 async def test_adaptive_webrtc_client(
173 cli_cb: Callable[[], Awaitable[TestClient]],
174 app: web.Application,
175 request_handler: Callable[[aiohttp.web.Request], Awaitable[aiohttp.web.Response]],
176 ) -> None:
177 """Test List Streams calls."""
178 app.router.add_get("/streams", request_handler)
179 app.router.add_get("/static", request_handler)
180 app.router.add_post("/stream", request_handler)
181 cli = await cli_cb()
182 assert isinstance(cli.server, TestServer)
183 # Web heartbeat fails
184 cli.server.app["response"].append(aiohttp.web.Response(status=404))
185 # WebRTC heartbeat succeeds
186 cli.server.app["response"].append(
187 aiohttp.web.Response(status=200),
188 )
189 # WebRTC offer
190 cli.server.app["response"].append(
191 aiohttp.web.json_response({"sdp64": ANSWER_PAYLOAD})
192 )
193
194 client = await get_adaptive_client(cast(ClientSession, cli))
195
196 answer_sdp = await client.offer(OFFER_SDP, RTSP_URL)
197 assert answer_sdp == ANSWER_SDP
198
199 assert get_diagnostics() == {
200 "discovery": {"attempt": 1, "webrtc.success": 1, "web.failure": 1},
201 "web": {"heartbeat.request": 1, "heartbeat.response_error": 1},
202 "webrtc": {
203 "stream.request": 1,
204 "stream.success": 1,
205 "heartbeat.request": 1,
206 "heartbeat.success": 1,
207 },
208 }
209
210
211 async def test_adaptive_both_fail(
212 cli_cb: Callable[[], Awaitable[TestClient]],
213 app: web.Application,
214 ) -> None:
215 """Test successful response from RTSPtoWebRTC server."""
216 cli = await cli_cb()
217 assert isinstance(cli.server, TestServer)
218
219 with pytest.raises(ClientError):
220 await get_adaptive_client(cast(ClientSession, cli))