userver: /data/code/userver/testsuite/pytest_plugins/pytest_userver/plugins/grpc/client.py Source File
Loading...
Searching...
No Matches
client.py
1"""
2Make gRPC requests to the service.
3
4@sa @ref scripts/docs/en/userver/tutorial/grpc_service.md
5"""
6
7# pylint: disable=redefined-outer-name
8import asyncio
9from collections.abc import AsyncIterator
10from collections.abc import Awaitable
11from collections.abc import Callable
12from collections.abc import Generator
13from collections.abc import Sequence
14import itertools
15import pathlib
16import tempfile
17from typing import Any
18from typing import TypeAlias
19
20import grpc
21import grpc.aio
22import pytest
23from typing_extensions import override
24
28from . import _hookspec
29
30DEFAULT_TIMEOUT = 15.0
31
32_AsyncExcCheck: TypeAlias = Callable[[], None]
33
34
35@pytest.fixture(scope='session')
37 """
38 Returns the gRPC port that should be used in service runner mode in case
39 no port is specified in the source config_yaml.
40
41 @ingroup userver_testsuite_fixtures
42 """
43 return 11080
44
45
46@pytest.fixture(scope='session')
47def grpc_service_endpoint(service_config) -> str:
48 """
49 Returns the gRPC endpoint of the service.
50 Used by @ref pytest_userver.plugins.grpc.client.grpc_channel "grpc_channel" fixture.
51
52 By default, gets the actual gRPC endpoint from `service_config`.
53 Override this fixture if you add gRPC server listening ports in a custom way.
54
55 @ingroup userver_testsuite_fixtures
56 """
57 components = service_config['components_manager']['components']
58 if 'grpc-server' not in components:
59 raise RuntimeError('No grpc-server component')
60 grpc_server = components['grpc-server']
61
62 if unix_socket_path := grpc_server.get('unix-socket-path'):
63 return f'unix:{unix_socket_path}'
64 if port := grpc_server.get('port'):
65 return f'localhost:{port}'
66 raise RuntimeError("No 'port' or 'unix-socket-path' found in 'grpc-server' component config")
67
68
69@pytest.fixture(scope='session')
70def grpc_service_timeout(pytestconfig) -> float:
71 """
72 Returns the gRPC timeout for the service that is set by the command
73 line option `--service-timeout`.
74
75 Override this fixture to change the way the gRPC timeout
76 is set.
77
78 @ingroup userver_testsuite_fixtures
79 """
80 return float(pytestconfig.option.service_timeout) or DEFAULT_TIMEOUT
81
82
83@pytest.fixture(scope='session')
84async def grpc_session_channel(grpc_service_endpoint):
85 async with grpc.aio.insecure_channel(grpc_service_endpoint) as channel:
86 yield channel
87
88
89@pytest.fixture
90async def grpc_channel(
91 service_client, # For daemon setup and userver_client_cleanup
92 grpc_service_endpoint,
93 grpc_service_timeout,
94 grpc_session_channel,
95 request,
96):
97 """
98 Returns the gRPC channel configured by the parameters from the
99 @ref pytest_userver.plugins.grpc.client.grpc_service_endpoint "grpc_service_endpoint" fixture.
100
101 You can add interceptors to the channel by implementing the
102 @ref pytest_userver.plugins.grpc._hookspec.pytest_grpc_client_interceptors "pytest_grpc_client_interceptors"
103 hook in your pytest plugin or initial (root) conftest.
104
105 @ingroup userver_testsuite_fixtures
106 """
107 interceptors = request.config.hook.pytest_grpc_client_interceptors(request=request)
108 interceptors_list = list(itertools.chain.from_iterable(interceptors))
109 # Sanity check: we have at least one "builtin" interceptor.
110 assert len(interceptors_list) != 0
111
112 try:
113 await asyncio.wait_for(
114 grpc_session_channel.channel_ready(),
115 timeout=grpc_service_timeout,
116 )
117 except asyncio.TimeoutError:
118 raise RuntimeError(
119 f'Failed to connect to remote gRPC server by address {grpc_service_endpoint}',
120 )
121
122 _set_client_interceptors(grpc_session_channel, interceptors_list)
123 try:
124 yield grpc_session_channel
125 finally:
126 _set_client_interceptors(grpc_session_channel, [])
127
128
129@pytest.fixture(scope='session')
130def grpc_socket_path() -> Generator[pathlib.Path, None, None]:
131 """
132 Path for the UNIX socket over which testsuite will talk to the gRPC service, if it chooses to use a UNIX socket.
133
134 @see pytest_userver.plugins.grpc.client.userver_config_grpc_endpoint "userver_config_grpc_endpoint"
135
136 @ingroup userver_testsuite_fixtures
137 """
138 # Path must be as short as possible due to 108 character limitation.
139 # 'service_tempdir', for example, is typically too long.
140 with tempfile.TemporaryDirectory(prefix='userver-grpc-socket-') as name:
141 yield pathlib.Path(name) / 'grpc.sock'
142
143
144@pytest.fixture(scope='session')
145@pytest_userver.config.patch
147 pytestconfig,
148 grpc_service_port_fallback,
149 substitute_config_vars,
150 request,
151 choose_free_port,
152):
153 """
154 Returns a function that adjusts the static config for testsuite.
155
156 * if the original service config specifies `grpc-server.port`, and that port is taken,
157 then adjusts it to a free port;
158 * if the original service config specifies `grpc-server.unix-socket-path`,
159 then adjusts it to a tmp path
160 (see @ref pytest_userver.plugins.grpc.client.grpc_socket_path "grpc_socket_path");
161 * in service runner mode, uses the original grpc port from config or
162 @ref pytest_userver.plugins.grpc.client.grpc_service_port_fallback "grpc_service_port_fallback".
163
164 Override this fixture to change the way `grpc-server` endpoint config is patched for tests.
165
166 @ingroup userver_testsuite_fixtures
167 """
168
169 def patch_config(config_yaml, config_vars):
170 components = config_yaml['components_manager']['components']
171 grpc_server = components.get('grpc-server', None)
172 if not grpc_server:
173 return
174
175 original_grpc_server = substitute_config_vars(grpc_server, config_vars)
176
177 if pytestconfig.option.service_runner_mode:
178 grpc_server.pop('unix-socket-path', None)
179 if 'port' not in original_grpc_server:
180 grpc_server['port'] = grpc_service_port_fallback
181 config_vars['grpc_server_port'] = grpc_service_port_fallback
182 elif 'unix-socket-path' in original_grpc_server:
183 grpc_server.pop('port', None)
184 grpc_socket_path = request.getfixturevalue('grpc_socket_path')
185 grpc_server['unix-socket-path'] = str(grpc_socket_path)
186 else:
187 grpc_server.pop('unix-socket-path', None)
188 grpc_server['port'] = choose_free_port(original_grpc_server.get('port', None))
189
190 return patch_config
191
192
193# @cond
194
195
196def pytest_addhooks(pluginmanager: pytest.PytestPluginManager):
197 pluginmanager.add_hookspecs(_hookspec)
198
199
200class _AsyncExcCheckInterceptor(
201 grpc.aio.UnaryUnaryClientInterceptor,
202 grpc.aio.UnaryStreamClientInterceptor,
203 grpc.aio.StreamUnaryClientInterceptor,
204 grpc.aio.StreamStreamClientInterceptor,
205):
206 def __init__(self, asyncexc_check: _AsyncExcCheck):
207 self._asyncexc_check = asyncexc_check
208
209 @override
210 async def intercept_unary_unary(
211 self,
212 continuation: Callable[[grpc.aio.ClientCallDetails, Any], Awaitable[grpc.aio.UnaryUnaryCall]],
213 client_call_details: grpc.aio.ClientCallDetails,
214 request: Any,
215 ) -> grpc.aio.UnaryUnaryCall:
216 self._asyncexc_check()
217 try:
218 return await continuation(client_call_details, request)
219 finally:
220 self._asyncexc_check()
221
222 # Note: full type of this function is Callable[[...], Awaitable[AsyncIterator[Any]]]
223 @override
224 async def intercept_unary_stream(
225 self,
226 continuation: Callable[[grpc.aio.ClientCallDetails, Any], grpc.aio.UnaryStreamCall],
227 client_call_details: grpc.aio.ClientCallDetails,
228 request: Any,
229 ) -> AsyncIterator[Any]:
230 self._asyncexc_check()
231 call = await continuation(client_call_details, request)
232
233 async def response_stream() -> AsyncIterator[Any]:
234 try:
235 async for response in call:
236 yield response
237 finally:
238 self._asyncexc_check()
239
240 return response_stream()
241
242 @override
243 async def intercept_stream_unary(
244 self,
245 continuation: Callable[[grpc.aio.ClientCallDetails, AsyncIterator[Any]], Awaitable[grpc.aio.StreamUnaryCall]],
246 client_call_details: grpc.aio.ClientCallDetails,
247 request_iterator: AsyncIterator[Any],
248 ) -> grpc.aio.StreamUnaryCall:
249 self._asyncexc_check()
250 try:
251 return await continuation(client_call_details, request_iterator)
252 finally:
253 self._asyncexc_check()
254
255 # Note: full type of this function is Callable[[...], Awaitable[AsyncIterator[Any]]]
256 @override
257 async def intercept_stream_stream(
258 self,
259 continuation: Callable[[grpc.aio.ClientCallDetails, AsyncIterator[Any]], grpc.aio.StreamStreamCall],
260 client_call_details: grpc.aio.ClientCallDetails,
261 request_iterator: AsyncIterator[Any],
262 ) -> AsyncIterator[Any]:
263 self._asyncexc_check()
264 call = await continuation(client_call_details, request_iterator)
265
266 async def response_stream() -> AsyncIterator[Any]:
267 try:
268 async for response in call:
269 yield response
270 finally:
271 self._asyncexc_check()
272
273 return response_stream()
274
275
276class _UpdateServerStateInterceptor(pytest_userver.grpc.PreCallClientInterceptor):
277 def __init__(self, service_client: pytest_userver.client.Client):
278 self._service_client = service_client
279
280 @override
281 async def pre_call_hook(self, client_call_details: grpc.aio.ClientCallDetails) -> None:
282 if hasattr(self._service_client, 'update_server_state'):
283 await self._service_client.update_server_state()
284
285
286def pytest_grpc_client_interceptors(request: pytest.FixtureRequest) -> Sequence[grpc.aio.ClientInterceptor]:
287 return [
288 _AsyncExcCheckInterceptor(request.getfixturevalue('asyncexc_check')),
289 _UpdateServerStateInterceptor(request.getfixturevalue('service_client')),
290 ]
291
292
293def _filter_interceptors(
294 interceptors: Sequence[grpc.aio.ClientInterceptor], desired_type: type[grpc.aio.ClientInterceptor]
295) -> list[grpc.aio.ClientInterceptor]:
296 return [interceptor for interceptor in interceptors if isinstance(interceptor, desired_type)]
297
298
299def _set_client_interceptors(channel: grpc.aio.Channel, interceptors: Sequence[grpc.aio.ClientInterceptor]) -> None:
300 """
301 Allows to set interceptors dynamically while reusing the same underlying channel,
302 which is something grpc-io currently doesn't support.
303
304 Also fixes the bug: multi-inheritance interceptors are only registered for first matching type
305 https://github.com/grpc/grpc/issues/31442
306 """
307 channel._unary_unary_interceptors = _filter_interceptors(interceptors, grpc.aio.UnaryUnaryClientInterceptor)
308 channel._unary_stream_interceptors = _filter_interceptors(interceptors, grpc.aio.UnaryStreamClientInterceptor)
309 channel._stream_unary_interceptors = _filter_interceptors(interceptors, grpc.aio.StreamUnaryClientInterceptor)
310 channel._stream_stream_interceptors = _filter_interceptors(interceptors, grpc.aio.StreamStreamClientInterceptor)
311
312
313# @endcond