148 grpc_service_port_fallback,
149 substitute_config_vars,
154 Returns a function that adjusts the static config for testsuite.
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".
164 Override this fixture to change the way `grpc-server` endpoint config is patched for tests.
166 @ingroup userver_testsuite_fixtures
169 def patch_config(config_yaml, config_vars):
170 components = config_yaml[
'components_manager'][
'components']
171 grpc_server = components.get(
'grpc-server',
None)
175 original_grpc_server = substitute_config_vars(grpc_server, config_vars)
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)
187 grpc_server.pop(
'unix-socket-path',
None)
188 grpc_server[
'port'] = choose_free_port(original_grpc_server.get(
'port',
None))
196def pytest_addhooks(pluginmanager: pytest.PytestPluginManager):
197 pluginmanager.add_hookspecs(_hookspec)
200class _AsyncExcCheckInterceptor(
201 grpc.aio.UnaryUnaryClientInterceptor,
202 grpc.aio.UnaryStreamClientInterceptor,
203 grpc.aio.StreamUnaryClientInterceptor,
204 grpc.aio.StreamStreamClientInterceptor,
206 def __init__(self, asyncexc_check: _AsyncExcCheck):
207 self._asyncexc_check = asyncexc_check
210 async def intercept_unary_unary(
212 continuation: Callable[[grpc.aio.ClientCallDetails, Any], Awaitable[grpc.aio.UnaryUnaryCall]],
213 client_call_details: grpc.aio.ClientCallDetails,
215 ) -> grpc.aio.UnaryUnaryCall:
216 self._asyncexc_check()
218 return await continuation(client_call_details, request)
220 self._asyncexc_check()
224 async def intercept_unary_stream(
226 continuation: Callable[[grpc.aio.ClientCallDetails, Any], grpc.aio.UnaryStreamCall],
227 client_call_details: grpc.aio.ClientCallDetails,
229 ) -> AsyncIterator[Any]:
230 self._asyncexc_check()
231 call = await continuation(client_call_details, request)
233 async def response_stream() -> AsyncIterator[Any]:
235 async for response
in call:
238 self._asyncexc_check()
240 return response_stream()
243 async def intercept_stream_unary(
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()
251 return await continuation(client_call_details, request_iterator)
253 self._asyncexc_check()
257 async def intercept_stream_stream(
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)
266 async def response_stream() -> AsyncIterator[Any]:
268 async for response
in call:
271 self._asyncexc_check()
273 return response_stream()
277 def __init__(self, service_client: pytest_userver.
client.Client):
278 self._service_client = service_client
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()
286def pytest_grpc_client_interceptors(request: pytest.FixtureRequest) -> Sequence[grpc.aio.ClientInterceptor]:
288 _AsyncExcCheckInterceptor(request.getfixturevalue(
'asyncexc_check')),
289 _UpdateServerStateInterceptor(request.getfixturevalue(
'service_client')),
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)]
299def _set_client_interceptors(channel: grpc.aio.Channel, interceptors: Sequence[grpc.aio.ClientInterceptor]) ->
None:
301 Allows to set interceptors dynamically while reusing the same underlying channel,
302 which is something grpc-io currently doesn't support.
304 Also fixes the bug: multi-inheritance interceptors are only registered for first matching type
305 https://github.com/grpc/grpc/issues/31442
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)