HttpServerHandler extends HttpHandler { public void handleSend(@Nullable Resp response, @Nullable Throwable error, Span span) { if (span == null) throw new NullPointerException("span == null"); if (response == null && error == null) { throw new IllegalArgumentException( "Either the response or error parameters may be null, but not both"); } if (response == null) { span.error(error).finish(); return; } HttpServerResponse serverResponse; if (response instanceof HttpServerResponse) { serverResponse = (HttpServerResponse) response; if (serverResponse.error() == null && error != null) { span.error(error); } } else { serverResponse = new FromResponseAdapter<>(adapter, response, error); } handleFinish(serverResponse, span); } HttpServerHandler(HttpTracing httpTracing, @Deprecated HttpServerAdapter<Req, Resp> adapter); static HttpServerHandler<HttpServerRequest, HttpServerResponse> create(
    HttpTracing httpTracing); @Deprecated static HttpServerHandler<Req, Resp> create(HttpTracing httpTracing,
    HttpServerAdapter<Req, Resp> adapter); Span handleReceive(HttpServerRequest request); @Deprecated Span handleReceive(Extractor<Req> extractor, Req request); @Deprecated Span handleReceive(Extractor<C> extractor, C carrier, Req request); void handleSend(@Nullable Resp response, @Nullable Throwable error, Span span); void handleSend(HttpServerResponse response, Span span);  }
@Test public void handleSend_finishesSpanEvenIfUnwrappedNull() { brave.Span span = mock(brave.Span.class); when(span.context()).thenReturn(context); when(span.customizer()).thenReturn(span); handler.handleSend(response, span); verify(span).isNoop(); verify(span).context(); verify(span).customizer(); verify(span).finish(); verifyNoMoreInteractions(span); }
@Test public void handleSend_finishesSpanEvenIfUnwrappedNull_withError() { brave.Span span = mock(brave.Span.class); when(span.context()).thenReturn(context); when(span.customizer()).thenReturn(span); Exception error = new RuntimeException("peanuts"); when(response.error()).thenReturn(error); handler.handleSend(response, span); verify(span).isNoop(); verify(span).context(); verify(span).customizer(); verify(span).error(error); verify(span).finish(); verifyNoMoreInteractions(span); }
@Test public void handleSend_oneOfResponseError() { brave.Span span = mock(brave.Span.class); assertThatThrownBy(() -> handler.handleSend(null, span)) .isInstanceOf(NullPointerException.class) .hasMessage("response == null"); }
@Test public void deprecatedHandleSend_finishesSpanEvenIfUnwrappedNull() { brave.Span span = mock(brave.Span.class); when(span.context()).thenReturn(context); when(span.customizer()).thenReturn(span); handler.handleSend(mock(HttpServerResponse.class), null, span); verify(span).isNoop(); verify(span).context(); verify(span).customizer(); verify(span).finish(); verifyNoMoreInteractions(span); }
@Test public void deprecatedHandleSend_finishesSpanEvenIfUnwrappedNull_withError() { brave.Span span = mock(brave.Span.class); when(span.context()).thenReturn(context); when(span.customizer()).thenReturn(span); Exception error = new RuntimeException("peanuts"); handler.handleSend(mock(HttpServerResponse.class), error, span); verify(span).isNoop(); verify(span).context(); verify(span).customizer(); verify(span).error(error); verify(span).finish(); verifyNoMoreInteractions(span); }
@Test public void deprecatedHandleSend_oneOfResponseError() { brave.Span span = mock(brave.Span.class); assertThatThrownBy(() -> handler.handleSend(null, null, span)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Either the response or error parameters may be null, but not both"); }
@Test public void handleSend_responseParserDoesntSeeNullWhenUnwrappedNull() { AtomicBoolean reachedAssertion = new AtomicBoolean(); init(httpTracingBuilder(tracingBuilder()) .serverParser(new HttpServerParser() { @Override public <Resp> void response(HttpAdapter<?, Resp> adapter, Resp resp, Throwable error, SpanCustomizer span) { assertThat(resp).isSameAs(NULL_SENTINEL); reachedAssertion.set(true); } })); handler.handleSend(response, null, mock(brave.Span.class)); assertThat(reachedAssertion).isTrue(); }