|
16 | 16 | package software.amazon.awssdk.services.metrics;
|
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.Assertions.assertThat;
|
| 19 | +import static org.hamcrest.Matchers.is; |
19 | 20 | import static org.mockito.Matchers.any;
|
20 | 21 | import static org.mockito.Mockito.mock;
|
21 | 22 | import static org.mockito.Mockito.verify;
|
@@ -232,17 +233,66 @@ public void testApiCall_clientSideExceptionThrown_includedInMetrics() {
|
232 | 233 | when(mockHttpClient.prepareRequest(any(HttpExecuteRequest.class))).thenThrow(new RuntimeException("oops"));
|
233 | 234 |
|
234 | 235 | thrown.expect(RuntimeException.class);
|
| 236 | + try { |
| 237 | + client.allTypes(); |
| 238 | + } finally { |
235 | 239 |
|
236 |
| - client.allTypes(); |
| 240 | + ArgumentCaptor<MetricCollection> collectionCaptor = ArgumentCaptor.forClass(MetricCollection.class); |
| 241 | + verify(mockPublisher).publish(collectionCaptor.capture()); |
237 | 242 |
|
238 |
| - ArgumentCaptor<MetricCollection> collectionCaptor = ArgumentCaptor.forClass(MetricCollection.class); |
239 |
| - verify(mockPublisher).publish(collectionCaptor.capture()); |
| 243 | + MetricCollection capturedCollection = collectionCaptor.getValue(); |
240 | 244 |
|
241 |
| - MetricCollection capturedCollection = collectionCaptor.getValue(); |
| 245 | + MetricCollection requestMetrics = capturedCollection.children().get(0); |
242 | 246 |
|
243 |
| - MetricCollection requestMetrics = capturedCollection.children().get(0); |
| 247 | + assertThat(requestMetrics.metricValues(CoreMetric.EXCEPTION).get(0)) |
| 248 | + .isExactlyInstanceOf(RuntimeException.class); |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + @Test |
| 253 | + public void testApiCall_requestExecutionThrowsClientSideException_includedInMetrics() throws IOException { |
| 254 | + IOException ioe = new IOException("oops"); |
| 255 | + ExecutableHttpRequest mockExecutableRequest = mock(ExecutableHttpRequest.class); |
| 256 | + when(mockExecutableRequest.call()).thenThrow(ioe); |
244 | 257 |
|
245 |
| - assertThat(requestMetrics.metricValues(CoreMetric.EXCEPTION).get(0)).isExactlyInstanceOf(RuntimeException.class); |
| 258 | + when(mockHttpClient.prepareRequest(any(HttpExecuteRequest.class))).thenReturn(mockExecutableRequest); |
| 259 | + |
| 260 | + thrown.expectCause(is(ioe)); |
| 261 | + try { |
| 262 | + client.allTypes(); |
| 263 | + } finally { |
| 264 | + ArgumentCaptor<MetricCollection> collectionCaptor = ArgumentCaptor.forClass(MetricCollection.class); |
| 265 | + verify(mockPublisher).publish(collectionCaptor.capture()); |
| 266 | + |
| 267 | + MetricCollection callMetrics = collectionCaptor.getValue(); |
| 268 | + MetricCollection attemptMetrics = callMetrics.children().get(0); |
| 269 | + |
| 270 | + assertThat(attemptMetrics.metricValues(CoreMetric.EXCEPTION)).containsExactly(ioe); |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + @Test |
| 275 | + public void testApiCall_streamingOutput_transformerThrows_includedInMetrics() throws IOException { |
| 276 | + IOException ioe = new IOException("oops"); |
| 277 | + ExecutableHttpRequest mockExecutableRequest = mock(ExecutableHttpRequest.class); |
| 278 | + when(mockExecutableRequest.call()).thenThrow(ioe); |
| 279 | + |
| 280 | + when(mockHttpClient.prepareRequest(any(HttpExecuteRequest.class))).thenReturn(mockExecutableRequest); |
| 281 | + |
| 282 | + thrown.expectCause(is(ioe)); |
| 283 | + try { |
| 284 | + client.streamingOutputOperation(req -> {}, (response, is) -> { |
| 285 | + throw ioe; |
| 286 | + }); |
| 287 | + } finally { |
| 288 | + ArgumentCaptor<MetricCollection> collectionCaptor = ArgumentCaptor.forClass(MetricCollection.class); |
| 289 | + verify(mockPublisher).publish(collectionCaptor.capture()); |
| 290 | + |
| 291 | + MetricCollection callMetrics = collectionCaptor.getValue(); |
| 292 | + MetricCollection attemptMetrics = callMetrics.children().get(0); |
| 293 | + |
| 294 | + assertThat(attemptMetrics.metricValues(CoreMetric.EXCEPTION)).containsExactly(ioe); |
| 295 | + } |
246 | 296 | }
|
247 | 297 |
|
248 | 298 | private static HttpExecuteResponse mockExecuteResponse(SdkHttpFullResponse httpResponse) {
|
|
0 commit comments