Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test__intercept_call_metadata_none(self):
tracer = mock.Mock()
tracer.span_context = mock.Mock()
test_header = 'test header'
mock_propagator = mock.Mock()
mock_propagator.to_header.return_value = test_header
interceptor = client_interceptor.OpenCensusClientInterceptor(
tracer=tracer, host_port='test')
interceptor._propagator = mock_propagator
mock_client_call_details = mock.Mock()
mock_client_call_details.metadata = None
mock_client_call_details.method = '/hello'
client_call_details, request_iterator, current_span =\
interceptor._intercept_call(
mock_client_call_details, mock.Mock(), 'unary_unary')
expected_metadata = (('grpc-trace-bin', test_header), )
self.assertEqual(expected_metadata, client_call_details.metadata)
def _stream_helper(self):
continuation = mock.Mock()
mock_response = iter([mock.Mock()])
continuation.return_value = mock_response
mock_tracer = mock.Mock()
interceptor = client_interceptor.OpenCensusClientInterceptor(
tracer=mock_tracer)
interceptor._intercept_call = mock.Mock(
return_value=(None, iter([mock.Mock()]), mock.Mock()))
return interceptor, continuation, mock_tracer
def test__end_span_between_context(self):
from opencensus.trace import execution_context
current_span = mock.Mock()
tracer = mock.Mock()
interceptor = client_interceptor.OpenCensusClientInterceptor(
tracer=tracer, host_port='test')
interceptor._end_span_between_context(current_span)
span_in_context = execution_context.get_current_span()
self.assertEqual(span_in_context, current_span)
self.assertTrue(tracer.end_span.called)
def _unary_helper(self):
continuation = mock.Mock()
mock_response = mock.Mock()
continuation.return_value = mock_response
interceptor = client_interceptor.OpenCensusClientInterceptor()
interceptor._intercept_call = mock.Mock(
return_value=(None, iter([mock.Mock()]), None))
return interceptor, continuation, mock_response
def test__intercept_call_metadata_tuple(self):
tracer = mock.Mock()
tracer.span_context = mock.Mock()
test_header = 'test header'
mock_propagator = mock.Mock()
mock_propagator.to_header.return_value = test_header
interceptor = client_interceptor.OpenCensusClientInterceptor(
tracer=tracer, host_port='test')
interceptor._propagator = mock_propagator
mock_client_call_details = mock.Mock()
mock_client_call_details.metadata = (('test_key', 'test_value'), )
mock_client_call_details.method = '/hello'
client_call_details, request_iterator, current_span =\
interceptor._intercept_call(
mock_client_call_details, mock.Mock(), 'unary_unary')
expected_metadata = (
('test_key', 'test_value'),
('grpc-trace-bin', test_header),
)
self.assertEqual(expected_metadata, client_call_details.metadata)
from logger import getJSONLogger
logger = getJSONLogger('recommendationservice-server')
if __name__ == "__main__":
# get port
if len(sys.argv) > 1:
port = sys.argv[1]
else:
port = "8080"
try:
exporter = stackdriver_exporter.StackdriverExporter()
tracer = Tracer(exporter=exporter)
tracer_interceptor = client_interceptor.OpenCensusClientInterceptor(tracer, host_port='localhost:'+port)
except:
tracer_interceptor = client_interceptor.OpenCensusClientInterceptor()
# set up server stub
channel = grpc.insecure_channel('localhost:'+port)
channel = grpc.intercept_channel(channel, tracer_interceptor)
stub = demo_pb2_grpc.RecommendationServiceStub(channel)
# form request
request = demo_pb2.ListRecommendationsRequest(user_id="test", product_ids=["test"])
# make call to server
response = stub.ListRecommendations(request)
logger.info(response)