@Before public void setUp() { // Wire the mock tracer factory when(tracerFactory.newTracer(any(SpanName.class))).thenReturn(tracer); // Wire the mock inner callable innerResult = SettableApiFuture.create(); when(innerCallable.futureCall(anyString(), any(ApiCallContext.class))).thenReturn(innerResult); // Build the system under test tracedUnaryCallable = new TracedUnaryCallable<>(innerCallable, tracerFactory, SPAN_NAME); callContext = FakeCallContext.createDefault(); }
@Test public void testTracerCreated() { tracedUnaryCallable.futureCall("test", callContext); verify(tracerFactory, times(1)).newTracer(SPAN_NAME); }
/** * Create a callable object with http/json-specific functionality. Designed for use by generated * code. * * @param httpJsonCallSettings the http/json call settings * @param callSettings {@link UnaryCallSettings} to configure the method-level settings with. * @param clientContext {@link ClientContext} to use to connect to the service. * @return {@link UnaryCallable} callable object. */ public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createUnaryCallable( HttpJsonCallSettings<RequestT, ResponseT> httpJsonCallSettings, UnaryCallSettings<RequestT, ResponseT> callSettings, ClientContext clientContext) { UnaryCallable<RequestT, ResponseT> innerCallable = createDirectUnaryCallable(httpJsonCallSettings); innerCallable = new TracedUnaryCallable<>( innerCallable, clientContext.getTracerFactory(), getSpanName(httpJsonCallSettings.getMethodDescriptor())); return createUnaryCallable(innerCallable, callSettings, clientContext); }
@Test public void testOperationFailed() { RuntimeException fakeError = new RuntimeException("fake error"); innerResult.setException(fakeError); tracedUnaryCallable.futureCall("test", callContext); verify(tracer, times(1)).operationFailed(fakeError); }
/** * Create a callable object with http/json-specific functionality. Designed for use by generated * code. * * @param httpJsonCallSettings the http/json call settings * @param callSettings {@link UnaryCallSettings} to configure the method-level settings with. * @param clientContext {@link ClientContext} to use to connect to the service. * @return {@link UnaryCallable} callable object. */ public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createUnaryCallable( HttpJsonCallSettings<RequestT, ResponseT> httpJsonCallSettings, UnaryCallSettings<RequestT, ResponseT> callSettings, ClientContext clientContext) { UnaryCallable<RequestT, ResponseT> innerCallable = createDirectUnaryCallable(httpJsonCallSettings); innerCallable = new TracedUnaryCallable<>( innerCallable, clientContext.getTracerFactory(), getSpanName(httpJsonCallSettings.getMethodDescriptor())); return createUnaryCallable(innerCallable, callSettings, clientContext); }
@Test public void testSyncError() { RuntimeException fakeError = new RuntimeException("fake error"); // Reset the irrelevant expectations from setup. (only needed to silence the warnings). @SuppressWarnings("unchecked") UnaryCallable<String, String>[] innerCallableWrapper = new UnaryCallable[] {innerCallable}; reset(innerCallableWrapper); when(innerCallable.futureCall(eq("failing test"), any(ApiCallContext.class))) .thenThrow(fakeError); try { tracedUnaryCallable.futureCall("failing test", callContext); } catch (RuntimeException e) { // ignored } verify(tracer, times(1)).operationFailed(fakeError); } }
/** * Create a callable object with grpc-specific functionality. Designed for use by generated code. * * @param grpcCallSettings the gRPC call settings */ public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createUnaryCallable( GrpcCallSettings<RequestT, ResponseT> grpcCallSettings, UnaryCallSettings<RequestT, ResponseT> callSettings, ClientContext clientContext) { UnaryCallable<RequestT, ResponseT> callable = createBaseUnaryCallable(grpcCallSettings, callSettings, clientContext); callable = new TracedUnaryCallable<>( callable, clientContext.getTracerFactory(), getSpanName(grpcCallSettings.getMethodDescriptor())); return callable.withDefaultCallContext(clientContext.getDefaultCallContext()); }
@Test public void testOperationFinish() { innerResult.set("successful result"); tracedUnaryCallable.futureCall("test", callContext); verify(tracer, times(1)).operationSucceeded(); }
/** * Create a callable object with grpc-specific functionality. Designed for use by generated code. * * @param grpcCallSettings the gRPC call settings */ public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createUnaryCallable( GrpcCallSettings<RequestT, ResponseT> grpcCallSettings, UnaryCallSettings<RequestT, ResponseT> callSettings, ClientContext clientContext) { UnaryCallable<RequestT, ResponseT> callable = createBaseUnaryCallable(grpcCallSettings, callSettings, clientContext); callable = new TracedUnaryCallable<>( callable, clientContext.getTracerFactory(), getSpanName(grpcCallSettings.getMethodDescriptor())); return callable.withDefaultCallContext(clientContext.getDefaultCallContext()); }
@Test public void testOperationCancelled() { innerResult.cancel(true); tracedUnaryCallable.futureCall("test", callContext); verify(tracer, times(1)).operationCancelled(); }