def test_vanilla_request_via_with_call(self): response = self.server_stub.OneQuestionOneResponse.with_call( stan_pb2.QuestionRequest(question="Are you there?")) self.assertEqual( response[0].answer, "Invention, my dear friends, is 93% perspiration, 6% electricity, 4% evaporation, and 2% butterscotch ripple. – Willy Wonka" )
def generate_questions(self): """ Used in the streaming grpc tests """ questions = [ stan_pb2.QuestionRequest(question="Are you there?"), stan_pb2.QuestionRequest(question="What time is it?"), stan_pb2.QuestionRequest(question="Where in the world is Waldo?"), stan_pb2.QuestionRequest( question="What did one campfire say to the other?"), stan_pb2.QuestionRequest(question="Is cereal soup?"), stan_pb2.QuestionRequest( question="What is always coming, but never arrives?") ] for q in questions: yield q time.sleep(random.uniform(0.2, 0.5))
def test_server_error(self): try: response = None with tracer.start_active_span('test'): response = self.server_stub.OneQuestionOneErrorResponse( stan_pb2.QuestionRequest(question="Do u error?")) except: pass self.assertIsNone(tracer.active_span) self.assertIsNone(response) spans = self.recorder.queued_spans() self.assertEqual(4, len(spans)) log_span = get_first_span_by_name(spans, 'log') server_span = get_first_span_by_name(spans, 'rpc-server') client_span = get_first_span_by_name(spans, 'rpc-client') test_span = get_first_span_by_name(spans, 'sdk') assert (log_span) assert (server_span) assert (client_span) assert (test_span) # Same traceId self.assertEqual(server_span.t, client_span.t) self.assertEqual(server_span.t, test_span.t) # Parent relationships self.assertEqual(server_span.p, client_span.s) self.assertEqual(client_span.p, test_span.s) # Error logging self.assertFalse(test_span.error) self.assertIsNone(test_span.ec) self.assertTrue(client_span.error) self.assertEqual(client_span.ec, 1) self.assertFalse(server_span.error) self.assertIsNone(server_span.ec) # rpc-server self.assertEqual(server_span.n, 'rpc-server') self.assertEqual(server_span.k, 1) self.assertIsNotNone(server_span.stack) self.assertEqual(2, len(server_span.stack)) self.assertEqual(server_span.data.rpc.flavor, 'grpc') self.assertEqual(server_span.data.rpc.call, '/stan.Stan/OneQuestionOneErrorResponse') self.assertEqual(server_span.data.rpc.host, testenv["grpc_host"]) self.assertEqual(server_span.data.rpc.port, str(testenv["grpc_port"])) self.assertIsNone(server_span.data.rpc.error) # rpc-client self.assertEqual(client_span.n, 'rpc-client') self.assertEqual(client_span.k, 2) self.assertIsNotNone(client_span.stack) self.assertEqual(client_span.data.rpc.flavor, 'grpc') self.assertEqual(client_span.data.rpc.call, '/stan.Stan/OneQuestionOneErrorResponse') self.assertEqual(client_span.data.rpc.host, testenv["grpc_host"]) self.assertEqual(client_span.data.rpc.port, str(testenv["grpc_port"])) self.assertEqual(client_span.data.rpc.call_type, 'unary') self.assertIsNotNone(client_span.data.rpc.error) # log self.assertEqual(log_span.n, 'log') self.assertIsNotNone(log_span.data.log) self.assertEqual(log_span.data.log['message'], 'Exception calling application: Simulated error') # test-span self.assertEqual(test_span.n, 'sdk') self.assertEqual(test_span.data.sdk.name, 'test')
def test_unary_one_to_one(self): with tracer.start_active_span('test'): response = self.server_stub.OneQuestionOneResponse( stan_pb2.QuestionRequest(question="Are you there?")) self.assertIsNone(tracer.active_span) self.assertIsNotNone(response) self.assertEqual( response.answer, "Invention, my dear friends, is 93% perspiration, 6% electricity, 4% evaporation, and 2% butterscotch ripple. – Willy Wonka" ) spans = self.recorder.queued_spans() self.assertEqual(3, len(spans)) server_span = get_first_span_by_name(spans, 'rpc-server') client_span = get_first_span_by_name(spans, 'rpc-client') test_span = get_first_span_by_name(spans, 'sdk') assert (server_span) assert (client_span) assert (test_span) # Same traceId self.assertEqual(server_span.t, client_span.t) self.assertEqual(server_span.t, test_span.t) # Parent relationships self.assertEqual(server_span.p, client_span.s) self.assertEqual(client_span.p, test_span.s) # Error logging self.assertFalse(test_span.error) self.assertIsNone(test_span.ec) self.assertFalse(client_span.error) self.assertIsNone(client_span.ec) self.assertFalse(server_span.error) self.assertIsNone(server_span.ec) # rpc-server self.assertEqual(server_span.n, 'rpc-server') self.assertEqual(server_span.k, 1) self.assertIsNotNone(server_span.stack) self.assertEqual(2, len(server_span.stack)) self.assertEqual(server_span.data.rpc.flavor, 'grpc') self.assertEqual(server_span.data.rpc.call, '/stan.Stan/OneQuestionOneResponse') self.assertEqual(server_span.data.rpc.host, testenv["grpc_host"]) self.assertEqual(server_span.data.rpc.port, str(testenv["grpc_port"])) self.assertIsNone(server_span.data.rpc.error) # rpc-client self.assertEqual(client_span.n, 'rpc-client') self.assertEqual(client_span.k, 2) self.assertIsNotNone(client_span.stack) self.assertEqual(client_span.data.rpc.flavor, 'grpc') self.assertEqual(client_span.data.rpc.call, '/stan.Stan/OneQuestionOneResponse') self.assertEqual(client_span.data.rpc.host, testenv["grpc_host"]) self.assertEqual(client_span.data.rpc.port, str(testenv["grpc_port"])) self.assertEqual(client_span.data.rpc.call_type, 'unary') self.assertIsNone(client_span.data.rpc.error) # test-span self.assertEqual(test_span.n, 'sdk') self.assertEqual(test_span.data.sdk.name, 'test')
def test_streaming_one_to_many(self): with tracer.start_active_span('test'): responses = self.server_stub.OneQuestionManyResponses( stan_pb2.QuestionRequest(question="Are you there?")) self.assertIsNone(tracer.active_span) self.assertIsNotNone(responses) final_answers = [] for response in responses: final_answers.append(response) self.assertEqual(len(final_answers), 6) spans = self.recorder.queued_spans() self.assertEqual(3, len(spans)) server_span = get_first_span_by_name(spans, 'rpc-server') client_span = get_first_span_by_name(spans, 'rpc-client') test_span = get_first_span_by_name(spans, 'sdk') assert (server_span) assert (client_span) assert (test_span) # Same traceId self.assertEqual(server_span.t, client_span.t) self.assertEqual(server_span.t, test_span.t) # Parent relationships self.assertEqual(server_span.p, client_span.s) self.assertEqual(client_span.p, test_span.s) # Error logging self.assertFalse(test_span.error) self.assertIsNone(test_span.ec) self.assertFalse(client_span.error) self.assertIsNone(client_span.ec) self.assertFalse(server_span.error) self.assertIsNone(server_span.ec) # rpc-server self.assertEqual(server_span.n, 'rpc-server') self.assertEqual(server_span.k, 1) self.assertIsNotNone(server_span.stack) self.assertEqual(2, len(server_span.stack)) self.assertEqual(server_span.data.rpc.flavor, 'grpc') self.assertEqual(server_span.data.rpc.call, '/stan.Stan/OneQuestionManyResponses') self.assertEqual(server_span.data.rpc.host, testenv["grpc_host"]) self.assertEqual(server_span.data.rpc.port, str(testenv["grpc_port"])) self.assertIsNone(server_span.data.rpc.error) # rpc-client self.assertEqual(client_span.n, 'rpc-client') self.assertEqual(client_span.k, 2) self.assertIsNotNone(client_span.stack) self.assertEqual(client_span.data.rpc.flavor, 'grpc') self.assertEqual(client_span.data.rpc.call, '/stan.Stan/OneQuestionManyResponses') self.assertEqual(client_span.data.rpc.host, testenv["grpc_host"]) self.assertEqual(client_span.data.rpc.port, str(testenv["grpc_port"])) self.assertEqual(client_span.data.rpc.call_type, 'stream') self.assertIsNone(client_span.data.rpc.error) # test-span self.assertEqual(test_span.n, 'sdk') self.assertEqual(test_span.data.sdk.name, 'test')
def test_async_unary(self): def process_response(future): result = future.result() self.assertEqual(type(result), stan_pb2.QuestionResponse) self.assertTrue(result.was_answered) self.assertEqual( result.answer, "Invention, my dear friends, is 93% perspiration, 6% electricity, 4% evaporation, and 2% butterscotch ripple. – Willy Wonka" ) with tracer.start_active_span('test'): future = self.server_stub.OneQuestionOneResponse.future( stan_pb2.QuestionRequest(question="Are you there?")) future.add_done_callback(process_response) time.sleep(0.7) self.assertIsNone(tracer.active_span) spans = self.recorder.queued_spans() self.assertEqual(3, len(spans)) server_span = get_first_span_by_name(spans, 'rpc-server') client_span = get_first_span_by_name(spans, 'rpc-client') test_span = get_first_span_by_name(spans, 'sdk') assert (server_span) assert (client_span) assert (test_span) # Same traceId self.assertEqual(server_span.t, client_span.t) self.assertEqual(server_span.t, test_span.t) # Parent relationships self.assertEqual(server_span.p, client_span.s) self.assertEqual(client_span.p, test_span.s) # Error logging self.assertIsNone(test_span.ec) self.assertIsNone(client_span.ec) self.assertIsNone(server_span.ec) # rpc-server self.assertEqual(server_span.n, 'rpc-server') self.assertEqual(server_span.k, 1) self.assertIsNone(server_span.stack) self.assertEqual(server_span.data["rpc"]["flavor"], 'grpc') self.assertEqual(server_span.data["rpc"]["call"], '/stan.Stan/OneQuestionOneResponse') self.assertEqual(server_span.data["rpc"]["host"], testenv["grpc_host"]) self.assertEqual(server_span.data["rpc"]["port"], str(testenv["grpc_port"])) self.assertIsNone(server_span.data["rpc"]["error"]) # rpc-client self.assertEqual(client_span.n, 'rpc-client') self.assertEqual(client_span.k, 2) self.assertIsNotNone(client_span.stack) self.assertEqual(client_span.data["rpc"]["flavor"], 'grpc') self.assertEqual(client_span.data["rpc"]["call"], '/stan.Stan/OneQuestionOneResponse') self.assertEqual(client_span.data["rpc"]["host"], testenv["grpc_host"]) self.assertEqual(client_span.data["rpc"]["port"], str(testenv["grpc_port"])) self.assertEqual(client_span.data["rpc"]["call_type"], 'unary') self.assertIsNone(client_span.data["rpc"]["error"]) # test-span self.assertEqual(test_span.n, 'sdk') self.assertEqual(test_span.data["sdk"]["name"], 'test')