ClientSideMonitoringRequestHandler extends RequestHandler2 { @Override public void afterError(Request<?> request, Response<?> response, Exception e) { ApiCallMonitoringEvent event = generateApiCallMonitoringEvent(request, e); handToMonitoringListeners(event); } ClientSideMonitoringRequestHandler(String clientId, Collection<MonitoringListener> monitoringListeners); @Override void afterAttempt(HandlerAfterAttemptContext context); @Override void afterResponse(Request<?> request, Response<?> response); @Override void afterError(Request<?> request, Response<?> response, Exception e);  }
@Test public void afterError_shouldSendApiCallMonitoringEvent() { Request<?> request = getRequest(); AWSRequestMetrics metrics = getMetrics(1); request.setAWSRequestMetrics(metrics); requestHandler.afterError(request, getResponse(request), new IOException("")); ArgumentCaptor<ApiCallMonitoringEvent> monitoringEventArgumentCaptor = ArgumentCaptor.forClass(ApiCallMonitoringEvent.class); verify(agentMonitoringListener, times(1)).handleEvent(monitoringEventArgumentCaptor.capture()); verify(customMonitoringListener, times(1)).handleEvent(any(ApiCallMonitoringEvent.class)); ApiCallMonitoringEvent event = monitoringEventArgumentCaptor.getValue(); verifyApiCallMonitoringEvent(1, event); }
@Test public void afterError_customMonitoringListenerThrowException_shouldNotAffectAgentListener() { Request<?> request = getRequest(); Response<?> response = getResponse(request); doThrow(new RuntimeException("Oops")).when(customMonitoringListener).handleEvent(any(ApiMonitoringEvent.class)); requestHandler.afterError(request, response, new AmazonServiceException("test")); verify(agentMonitoringListener, times(1)).handleEvent(any(ApiMonitoringEvent.class)); verify(customMonitoringListener, times(1)).handleEvent(any(ApiMonitoringEvent.class)); }
@Test public void afterError_maxRetriesExceeded() { Request<?> request = getRequest(); AWSRequestMetrics metrics = getMetrics(3); metrics.addPropertyWith(MaxRetriesExceeded, true); request.setAWSRequestMetrics(metrics); requestHandler.afterError(request, getResponse(request), new IOException("")); ApiCallMonitoringEvent event = getCapturedEvent(ApiCallMonitoringEvent.class); assertThat(event.getMaxRetriesExceeded(), is(1)); }
@Test public void afterError_clientExecutionTimeoutException() { Request<?> request = getRequest(); AWSRequestMetrics metrics = getMetrics(1); request.setAWSRequestMetrics(metrics); requestHandler.afterError(request, getResponse(request), new ClientExecutionTimeoutException("")); ApiCallMonitoringEvent event = getCapturedEvent(ApiCallMonitoringEvent.class); assertThat(event.getApiCallTimeout(), is(1)); }
@Test public void afterErrorWithoutApiCallAttemptExcludesFinalFields() { Request<?> request = getRequest(); AWSRequestMetrics metrics = getMetrics(1); request.setAWSRequestMetrics(metrics); requestHandler.afterError(request, getResponse(request), new ClientExecutionTimeoutException("")); verifyEmptyFinalFields(getCapturedEvent(ApiCallMonitoringEvent.class)); }