Debian Codegraph @

src/python/grpcio_tests/tests/unit/_contextvars_propagation_test.py

raw
1 # Copyright 2020 The gRPC authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """Test of propagation of contextvars to AuthMetadataPlugin threads.."""
15
16 import contextlib
17 import logging
18 import os
19 import queue
20 import sys
21 import threading
22 import unittest
23
24 import grpc
25
26 from tests.unit import test_common
27
28 _UNARY_UNARY = "/test/UnaryUnary"
29 _REQUEST = b"0000"
30
31
32 def _unary_unary_handler(request, context):
33 return request
34
35
36 def contextvars_supported():
37 try:
38 import contextvars
39 return True
40 except ImportError:
41 return False
42
43
44 class _GenericHandler(grpc.GenericRpcHandler):
45
46 def service(self, handler_call_details):
47 if handler_call_details.method == _UNARY_UNARY:
48 return grpc.unary_unary_rpc_method_handler(_unary_unary_handler)
49 else:
50 raise NotImplementedError()
51
52
53 @contextlib.contextmanager
54 def _server():
55 try:
56 server = test_common.test_server()
57 target = 'localhost:0'
58 port = server.add_insecure_port(target)
59 server.add_generic_rpc_handlers((_GenericHandler(),))
60 server.start()
61 yield port
62 finally:
63 server.stop(None)
64
65
66 if contextvars_supported():
67 import contextvars
68
69 _EXPECTED_VALUE = 24601
70 test_var = contextvars.ContextVar("test_var", default=None)
71
72 def set_up_expected_context():
73 test_var.set(_EXPECTED_VALUE)
74
75 class TestCallCredentials(grpc.AuthMetadataPlugin):
76
77 def __call__(self, context, callback):
78 if test_var.get(
79 ) != _EXPECTED_VALUE and not test_common.running_under_gevent():
80 # contextvars do not work under gevent, but the rest of this
81 # test is still valuable as a test of concurrent runs of the
82 # metadata credentials code path.
83 raise AssertionError("{} != {}".format(test_var.get(),
84 _EXPECTED_VALUE))
85 callback((), None)
86
87 def assert_called(self, test):
88 test.assertTrue(self._invoked)
89 test.assertEqual(_EXPECTED_VALUE, self._recorded_value)
90
91 else:
92
93 def set_up_expected_context():
94 pass
95
96 class TestCallCredentials(grpc.AuthMetadataPlugin):
97
98 def __call__(self, context, callback):
99 callback((), None)
100
101
102 # TODO(https://github.com/grpc/grpc/issues/22257)
103 @unittest.skipIf(os.name == "nt", "LocalCredentials not supported on Windows.")
104 class ContextVarsPropagationTest(unittest.TestCase):
105
106 def test_propagation_to_auth_plugin(self):
107 set_up_expected_context()
108 with _server() as port:
109 target = "localhost:{}".format(port)
110 local_credentials = grpc.local_channel_credentials()
111 test_call_credentials = TestCallCredentials()
112 call_credentials = grpc.metadata_call_credentials(
113 test_call_credentials, "test call credentials")
114 composite_credentials = grpc.composite_channel_credentials(
115 local_credentials, call_credentials)
116 with grpc.secure_channel(target, composite_credentials) as channel:
117 stub = channel.unary_unary(_UNARY_UNARY)
118 response = stub(_REQUEST, wait_for_ready=True)
119 self.assertEqual(_REQUEST, response)
120
121 def test_concurrent_propagation(self):
122 _THREAD_COUNT = 32
123 _RPC_COUNT = 32
124
125 set_up_expected_context()
126 with _server() as port:
127 target = "localhost:{}".format(port)
128 local_credentials = grpc.local_channel_credentials()
129 test_call_credentials = TestCallCredentials()
130 call_credentials = grpc.metadata_call_credentials(
131 test_call_credentials, "test call credentials")
132 composite_credentials = grpc.composite_channel_credentials(
133 local_credentials, call_credentials)
134 wait_group = test_common.WaitGroup(_THREAD_COUNT)
135
136 def _run_on_thread(exception_queue):
137 try:
138 with grpc.secure_channel(target,
139 composite_credentials) as channel:
140 stub = channel.unary_unary(_UNARY_UNARY)
141 wait_group.done()
142 wait_group.wait()
143 for i in range(_RPC_COUNT):
144 response = stub(_REQUEST, wait_for_ready=True)
145 self.assertEqual(_REQUEST, response)
146 except Exception as e: # pylint: disable=broad-except
147 exception_queue.put(e)
148
149 threads = []
150
151 for _ in range(_THREAD_COUNT):
152 q = queue.Queue()
153 thread = threading.Thread(target=_run_on_thread, args=(q,))
154 thread.setDaemon(True)
155 thread.start()
156 threads.append((thread, q))
157
158 for thread, q in threads:
159 thread.join()
160 if not q.empty():
161 raise q.get()
162
163
164 if __name__ == '__main__':
165 logging.basicConfig()
166 unittest.main(verbosity=2)