|
16 | 16 |
|
17 | 17 | import static org.assertj.core.api.Assertions.assertThat;
|
18 | 18 | import static org.junit.Assert.assertTrue;
|
| 19 | +import static org.mockito.Mockito.mock; |
| 20 | +import static org.mockito.Mockito.verify; |
19 | 21 | import static software.amazon.awssdk.http.Header.CONTENT_TYPE;
|
20 | 22 |
|
21 | 23 | import java.io.File;
|
22 | 24 | import java.io.FileInputStream;
|
23 | 25 | import java.io.FileNotFoundException;
|
24 | 26 | import java.io.InputStream;
|
25 |
| -import java.net.URISyntaxException; |
| 27 | +import java.time.Duration; |
26 | 28 | import java.util.List;
|
27 | 29 | import java.util.concurrent.CompletableFuture;
|
28 | 30 | import java.util.concurrent.ExecutionException;
|
29 | 31 | import org.junit.BeforeClass;
|
30 | 32 | import org.junit.Test;
|
| 33 | +import org.mockito.ArgumentCaptor; |
31 | 34 | import org.reactivestreams.Publisher;
|
32 | 35 | import org.reactivestreams.Subscriber;
|
33 | 36 | import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
|
|
36 | 39 | import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
|
37 | 40 | import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
|
38 | 41 | import software.amazon.awssdk.core.internal.util.Mimetype;
|
| 42 | +import software.amazon.awssdk.core.metrics.CoreMetric; |
39 | 43 | import software.amazon.awssdk.metrics.MetricCollection;
|
40 | 44 | import software.amazon.awssdk.metrics.MetricPublisher;
|
41 | 45 | import software.amazon.awssdk.regions.Region;
|
|
44 | 48 | import software.amazon.awssdk.services.transcribestreaming.model.MediaEncoding;
|
45 | 49 | import software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionRequest;
|
46 | 50 | import software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionResponseHandler;
|
| 51 | +import software.amazon.awssdk.utils.Logger; |
47 | 52 |
|
48 | 53 | /**
|
49 | 54 | * An example test class to show the usage of
|
|
53 | 58 | * The audio files used in this class don't have voice, so there won't be any transcripted text would be empty
|
54 | 59 | */
|
55 | 60 | public class TranscribeStreamingIntegrationTest {
|
| 61 | + private static final Logger log = Logger.loggerFor(TranscribeStreamingIntegrationTest.class); |
56 | 62 |
|
57 | 63 | private static TranscribeStreamingAsyncClient client;
|
58 | 64 |
|
| 65 | + private static MetricPublisher mockPublisher; |
| 66 | + |
59 | 67 | @BeforeClass
|
60 |
| - public static void setup() throws URISyntaxException { |
| 68 | + public static void setup() { |
| 69 | + mockPublisher = mock(MetricPublisher.class); |
61 | 70 | client = TranscribeStreamingAsyncClient.builder()
|
62 | 71 | .region(Region.US_EAST_1)
|
63 | 72 | .overrideConfiguration(b -> b.addExecutionInterceptor(new VerifyHeaderInterceptor())
|
64 |
| - .metricPublisher(new MetricPublisher() { |
65 |
| - @Override |
66 |
| - public void publish(MetricCollection metricCollection) { |
67 |
| - System.out.println(metricCollection); |
68 |
| - } |
69 |
| - |
70 |
| - @Override |
71 |
| - public void close() { |
72 |
| - |
73 |
| - } |
74 |
| - }) |
75 |
| - ) |
| 73 | + .metricPublisher(mockPublisher)) |
76 | 74 | .credentialsProvider(getCredentials())
|
77 | 75 | .build();
|
78 | 76 | }
|
79 | 77 |
|
80 | 78 | @Test
|
81 |
| - public void testFileWith16kRate() throws ExecutionException, InterruptedException, URISyntaxException { |
| 79 | + public void testFileWith16kRate() throws InterruptedException { |
82 | 80 | CompletableFuture<Void> result = client.startStreamTranscription(getRequest(16_000),
|
83 | 81 | new AudioStreamPublisher(
|
84 | 82 | getInputStream("silence_16kHz_s16le.wav")),
|
85 | 83 | TestResponseHandlers.responseHandlerBuilder_Classic());
|
86 | 84 |
|
87 |
| - // Blocking call to keep the main thread for shutting down |
88 |
| - result.get(); |
| 85 | + result.join(); |
| 86 | + verifyMetrics(); |
89 | 87 | }
|
90 | 88 |
|
91 | 89 | @Test
|
92 |
| - public void testFileWith8kRate() throws ExecutionException, InterruptedException, URISyntaxException { |
| 90 | + public void testFileWith8kRate() throws ExecutionException, InterruptedException { |
93 | 91 | CompletableFuture<Void> result = client.startStreamTranscription(getRequest(8_000),
|
94 | 92 | new AudioStreamPublisher(
|
95 | 93 | getInputStream("silence_8kHz_s16le.wav")),
|
@@ -143,4 +141,34 @@ public void beforeTransmission(Context.BeforeTransmission context, ExecutionAttr
|
143 | 141 | assertThat(contentTypeHeader.get(0)).isEqualTo(Mimetype.MIMETYPE_EVENT_STREAM);
|
144 | 142 | }
|
145 | 143 | }
|
| 144 | + |
| 145 | + private void verifyMetrics() throws InterruptedException { |
| 146 | + // wait for 100ms for metrics to be delivered to mockPublisher |
| 147 | + Thread.sleep(100); |
| 148 | + ArgumentCaptor<MetricCollection> collectionCaptor = ArgumentCaptor.forClass(MetricCollection.class); |
| 149 | + verify(mockPublisher).publish(collectionCaptor.capture()); |
| 150 | + MetricCollection capturedCollection = collectionCaptor.getValue(); |
| 151 | + assertThat(capturedCollection.name()).isEqualTo("ApiCall"); |
| 152 | + log.info(() -> "captured collection: " + capturedCollection); |
| 153 | + |
| 154 | + assertThat(capturedCollection.metricValues(CoreMetric.CREDENTIALS_FETCH_DURATION).get(0)) |
| 155 | + .isGreaterThanOrEqualTo(Duration.ZERO); |
| 156 | + assertThat(capturedCollection.metricValues(CoreMetric.MARSHALLING_DURATION).get(0)) |
| 157 | + .isGreaterThanOrEqualTo(Duration.ZERO); |
| 158 | + assertThat(capturedCollection.metricValues(CoreMetric.API_CALL_DURATION).get(0)) |
| 159 | + .isGreaterThan(Duration.ZERO); |
| 160 | + |
| 161 | + MetricCollection attemptCollection = capturedCollection.children().get(0); |
| 162 | + assertThat(attemptCollection.name()).isEqualTo("ApiCallAttempt"); |
| 163 | + assertThat(attemptCollection.children()).isEmpty(); |
| 164 | + assertThat(attemptCollection.metricValues(CoreMetric.HTTP_STATUS_CODE)) |
| 165 | + .containsExactly(200); |
| 166 | + assertThat(attemptCollection.metricValues(CoreMetric.SIGNING_DURATION).get(0)) |
| 167 | + .isGreaterThanOrEqualTo(Duration.ZERO); |
| 168 | + assertThat(attemptCollection.metricValues(CoreMetric.AWS_REQUEST_ID).get(0)).isNotEmpty(); |
| 169 | + |
| 170 | + assertThat(attemptCollection.metricValues(CoreMetric.HTTP_REQUEST_ROUND_TRIP_TIME).get(0)) |
| 171 | + .isGreaterThanOrEqualTo(Duration.ofMillis(100)); |
| 172 | + } |
| 173 | + |
146 | 174 | }
|
0 commit comments