14 """Test of propagation of contextvars to AuthMetadataPlugin threads.."""
24 from six.moves
import queue
28 _UNARY_UNARY =
"/test/UnaryUnary"
47 if handler_call_details.method == _UNARY_UNARY:
50 raise NotImplementedError()
53 @contextlib.contextmanager
56 server = test_common.test_server()
57 target =
'localhost:0'
58 port = server.add_insecure_port(target)
69 _EXPECTED_VALUE = 24601
70 test_var = contextvars.ContextVar(
"test_var", default=
None)
73 test_var.set(_EXPECTED_VALUE)
79 ) != _EXPECTED_VALUE
and not test_common.running_under_gevent():
83 raise AssertionError(
"{} != {}".
format(test_var.get(),
88 test.assertTrue(self._invoked)
89 test.assertEqual(_EXPECTED_VALUE, self._recorded_value)
103 @unittest.skipIf(os.name ==
"nt",
"LocalCredentials not supported on Windows.")
109 target =
"localhost:{}".
format(port)
113 test_call_credentials,
"test call credentials")
115 local_credentials, call_credentials)
117 stub = channel.unary_unary(_UNARY_UNARY)
118 response =
stub(_REQUEST, wait_for_ready=
True)
119 self.assertEqual(_REQUEST, response)
127 target =
"localhost:{}".
format(port)
131 test_call_credentials,
"test call credentials")
133 local_credentials, call_credentials)
136 def _run_on_thread(exception_queue):
139 composite_credentials)
as channel:
140 stub = channel.unary_unary(_UNARY_UNARY)
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:
147 exception_queue.put(e)
151 for _
in range(_THREAD_COUNT):
153 thread = threading.Thread(target=_run_on_thread, args=(q,))
154 thread.setDaemon(
True)
156 threads.append((thread, q))
158 for thread, q
in threads:
164 if __name__ ==
'__main__':
165 logging.basicConfig()
166 unittest.main(verbosity=2)