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=no-member
8# pylint: disable=redefined-outer-name
9import asyncio
10
11import grpc
12import pytest
13
14DEFAULT_TIMEOUT = 15.0
15
16USERVER_CONFIG_HOOKS = ['userver_config_grpc_mockserver']
17
18
19@pytest.fixture(scope='session')
20def grpc_service_port(service_config) -> int:
21 """
22 Returns the gRPC listener port number of the service that is set in the
23 static configuration file.
24
25 Override this fixture to change the way the gRPC listener port number
26 is retrieved by the testsuite for tests.
27
28 @ingroup userver_testsuite_fixtures
29 """
30 components = service_config['components_manager']['components']
31 if 'grpc-server' not in components:
32 raise RuntimeError('No grpc-server component')
33 return int(components['grpc-server']['port'])
34
35
36@pytest.fixture(scope='session')
37def grpc_service_endpoint(grpc_service_port) -> str:
38 """
39 Returns the gRPC endpoint of the service.
40
41 Override this fixture to change the way the gRPC endpoint
42 is retrieved by the testsuite for tests.
43
44 @ingroup userver_testsuite_fixtures
45 """
46 return f'localhost:{grpc_service_port}'
47
48
49@pytest.fixture(scope='session')
50def grpc_service_timeout(pytestconfig) -> float:
51 """
52 Returns the gRPC timeout for the service that is set by the command
53 line option `--service-timeout`.
54
55 Override this fixture to change the way the gRPC timeout
56 is set.
57
58 @ingroup userver_testsuite_fixtures
59 """
60 return float(pytestconfig.option.service_timeout) or DEFAULT_TIMEOUT
61
62
63@pytest.fixture(scope='session')
64async def _grpc_session_channel(grpc_service_endpoint):
65 async with grpc.aio.insecure_channel(grpc_service_endpoint) as channel:
66 yield channel
67
68
69@pytest.fixture
70async def grpc_channel(
71 grpc_service_endpoint,
72 grpc_service_deps,
73 grpc_service_timeout,
74 _grpc_session_channel,
75):
76 """
77 Returns the gRPC channel configured by the parameters from the
78 @ref plugins.grpc.grpc_service_endpoint "grpc_service_endpoint" fixture.
79
80 @ingroup userver_testsuite_fixtures
81 """
82 try:
83 await asyncio.wait_for(
84 _grpc_session_channel.channel_ready(),
85 timeout=grpc_service_timeout,
86 )
87 except asyncio.TimeoutError:
88 raise RuntimeError(
89 f'Failed to connect to remote gRPC server by '
90 f'address {grpc_service_endpoint}',
91 )
92 return _grpc_session_channel
93
94
95@pytest.fixture
96def grpc_service_deps(service_client):
97 """
98 gRPC service dependencies hook. Feel free to override it.
99
100 @ingroup userver_testsuite_fixtures
101 """
102
103
104@pytest.fixture(scope='session')
105def userver_config_grpc_mockserver(grpc_mockserver_endpoint):
106 """
107 Returns a function that adjusts the static config for testsuite.
108 Walks through config_vars *values* equal to `$grpc_mockserver`,
109 and replaces them with @ref grpc_mockserver_endpoint.
110
111 @ingroup userver_testsuite_fixtures
112 """
113
114 def patch_config(_config_yaml, config_vars):
115 for name in config_vars:
116 if config_vars[name] == '$grpc_mockserver':
117 config_vars[name] = grpc_mockserver_endpoint
118
119 return patch_config