@Test public void onEvent_works_as_expected_for_REQUEST_RECEIVED() { // given ServerMetricsEvent event = ServerMetricsEvent.REQUEST_RECEIVED; // when listener.onEvent(event, null); // then verify(listener.inflightRequests).inc(); }
@Test public void onEvent_works_as_expected_for_RESPONSE_WRITE_FAILED() { // given ServerMetricsEvent event = ServerMetricsEvent.RESPONSE_WRITE_FAILED; // when listener.onEvent(event, null); // then verify(listener.inflightRequests).dec(); verify(listener.processedRequests).inc(); verify(listener.responseWriteFailed).inc(); }
@Test public void onEvent_does_nothing_if_event_type_is_unknown() { // given ServerMetricsEvent event = null; Logger loggerMock = mock(Logger.class); doReturn(false).when(loggerMock).isDebugEnabled(); Whitebox.setInternalState(listener, "logger", loggerMock); // when listener.onEvent(event, null); // then verifyZeroInteractions(listener.inflightRequests, listener.responseWriteFailed, listener.processedRequests); verify(loggerMock).error("Metrics Error: unknown metrics event " + event); }
@Test public void onEvent_for_RESPONSE_SENT_updates_responseSizes_histogram_with_0_if_getFinalContentLength_is_null() { // given doReturn(null).when(responseInfoMock).getFinalContentLength(); // when listener.onEvent(ServerMetricsEvent.RESPONSE_SENT, state); // then verify(listener.responseSizes).update(0L); }
@Test public void onEvent_should_do_nothing_if_passed_RESPONSE_SENT_with_value_object_that_is_not_a_HttpProcessingState() { // when listener.onEvent(ServerMetricsEvent.RESPONSE_SENT, new Object()); // then verifyZeroInteractions(listener.inflightRequests, listener.processedRequests); verify(endpointMetricsHandlerMock, never()).handleRequest( any(RequestInfo.class), any(ResponseInfo.class), any(HttpProcessingState.class), anyInt(), anyInt(), anyLong() ); }
@Test public void onEvent_gracefully_handles_thrown_exceptions() { // given ServerMetricsEvent event = ServerMetricsEvent.RESPONSE_SENT; RuntimeException ex = new RuntimeException("kaboom"); doThrow(ex).when(listener.inflightRequests).dec(); Logger loggerMock = mock(Logger.class); doReturn(false).when(loggerMock).isDebugEnabled(); Whitebox.setInternalState(listener, "logger", loggerMock); // when listener.onEvent(event, state); // then verifyZeroInteractions(listener.processedRequests); // Should have blown up before the processedRequests stuff. verify(loggerMock).error("Metrics Error: ", ex); }
@Test public void onEvent_should_short_circuit_for_RESPONSE_SENT_if_response_info_is_null() { // given state.setResponseInfo(null, null); // when listener.onEvent(ServerMetricsEvent.RESPONSE_SENT, state); // then // Inflight requests and processed requests counters should still be adjusted properly verify(listener.inflightRequests).dec(); verify(listener.processedRequests).inc(); // But we should short circuit immediately afterward verifyZeroInteractions(listener.requestSizes, listener.responseSizes); verify(endpointMetricsHandlerMock, never()).handleRequest( any(RequestInfo.class), any(ResponseInfo.class), any(HttpProcessingState.class), anyInt(), anyInt(), anyLong() ); }
@Test public void onEvent_should_short_circuit_for_RESPONSE_SENT_if_request_start_time_nanos_is_null() { // given state.setRequestStartTimeNanos(null); // when listener.onEvent(ServerMetricsEvent.RESPONSE_SENT, state); // then // Inflight requests and processed requests counters should still be adjusted properly verify(listener.inflightRequests).dec(); verify(listener.processedRequests).inc(); // But we should short circuit immediately afterward verifyZeroInteractions(listener.requestSizes, listener.responseSizes); verify(endpointMetricsHandlerMock, never()).handleRequest( any(RequestInfo.class), any(ResponseInfo.class), any(HttpProcessingState.class), anyInt(), anyInt(), anyLong() ); }
@Test public void onEvent_should_short_circuit_for_RESPONSE_SENT_if_request_info_is_null() { // given state.setRequestInfo(null); // when listener.onEvent(ServerMetricsEvent.RESPONSE_SENT, state); // then // Inflight requests and processed requests counters should still be adjusted properly verify(listener.inflightRequests).dec(); verify(listener.processedRequests).inc(); // But we should short circuit immediately afterward verifyZeroInteractions(listener.requestSizes, listener.responseSizes); verify(endpointMetricsHandlerMock, never()).handleRequest( any(RequestInfo.class), any(ResponseInfo.class), any(HttpProcessingState.class), anyInt(), anyInt(), anyLong() ); }
@Test public void code_coverage_hoops() { // jump! // Account for the logger.isDebugEnabled() branches. Logger loggerMock = mock(Logger.class); doReturn(false).when(loggerMock).isDebugEnabled(); Whitebox.setInternalState(listener, "logger", loggerMock); listener.onEvent(ServerMetricsEvent.REQUEST_RECEIVED, null); listener.onEvent(ServerMetricsEvent.RESPONSE_SENT, state); listener.onEvent(ServerMetricsEvent.RESPONSE_WRITE_FAILED, state); // Exercise enums for (ServerStatisticsMetricNames enumValue : ServerStatisticsMetricNames.values()) { assertThat(ServerStatisticsMetricNames.valueOf(enumValue.name())).isEqualTo(enumValue); } for (ServerConfigMetricNames enumValue : ServerConfigMetricNames.values()) { assertThat(ServerConfigMetricNames.valueOf(enumValue.name())).isEqualTo(enumValue); } }
listener.onEvent(event, state); long afterCallTimeNanos = System.nanoTime();
listener.onEvent(event, state); long afterCallTimeNanos = System.nanoTime();