Skip to content

Commit fbe169e

Browse files
Bumping GAX to fix long-running operations (#2619)
Adding Speech integration test to verify the fix
1 parent 04c183e commit fbe169e

3 files changed

Lines changed: 161 additions & 2 deletions

File tree

google-cloud-speech/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@
8888
<classifier>testlib</classifier>
8989
<scope>test</scope>
9090
</dependency>
91+
<dependency>
92+
<groupId>com.google.truth</groupId>
93+
<artifactId>truth</artifactId>
94+
<scope>test</scope>
95+
</dependency>
9196
</dependencies>
9297
<profiles>
9398
<profile>
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright 2017 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.speech.v1.it;
18+
19+
import com.google.api.gax.rpc.ApiStreamObserver;
20+
import com.google.cloud.speech.v1.LongRunningRecognizeResponse;
21+
import com.google.cloud.speech.v1.RecognitionAudio;
22+
import com.google.cloud.speech.v1.RecognitionConfig;
23+
import com.google.cloud.speech.v1.RecognizeResponse;
24+
import com.google.cloud.speech.v1.SpeechClient;
25+
import com.google.cloud.speech.v1.StreamingRecognitionConfig;
26+
import com.google.cloud.speech.v1.StreamingRecognizeRequest;
27+
import com.google.cloud.speech.v1.StreamingRecognizeResponse;
28+
import com.google.common.io.Resources;
29+
import com.google.common.truth.Truth;
30+
import com.google.common.util.concurrent.SettableFuture;
31+
import com.google.protobuf.ByteString;
32+
import org.junit.AfterClass;
33+
import org.junit.BeforeClass;
34+
import org.junit.Rule;
35+
import org.junit.Test;
36+
import org.junit.rules.Timeout;
37+
38+
import java.net.URL;
39+
import java.util.List;
40+
41+
public class ITSpeechTest {
42+
private static SpeechClient speechClient;
43+
44+
@Rule
45+
public Timeout globalTimeout = Timeout.seconds(300);
46+
47+
@BeforeClass
48+
public static void setupClass() throws Exception {
49+
speechClient = SpeechClient.create();
50+
}
51+
52+
@AfterClass
53+
public static void tearDownClass() throws Exception {
54+
speechClient.close();
55+
}
56+
57+
@Test
58+
public void syncRecognize() {
59+
RecognizeResponse response = speechClient.recognize(config(), audio());
60+
61+
Truth.assertThat(response.getResultsCount()).isGreaterThan(0);
62+
Truth.assertThat(response.getResults(0).getAlternativesCount()).isGreaterThan(0);
63+
String text = response.getResults(0).getAlternatives(0).getTranscript();
64+
Truth.assertThat(text).isEqualTo("hello");
65+
}
66+
67+
@Test
68+
public void longrunningRecognize() throws Exception {
69+
LongRunningRecognizeResponse response = speechClient.longRunningRecognizeAsync(config(), audio()).get();
70+
71+
Truth.assertThat(response.getResultsCount()).isGreaterThan(0);
72+
Truth.assertThat(response.getResults(0).getAlternativesCount()).isGreaterThan(0);
73+
String text = response.getResults(0).getAlternatives(0).getTranscript();
74+
Truth.assertThat(text).isEqualTo("hello");
75+
}
76+
77+
@Test
78+
public void streamingRecognize() throws Exception {
79+
byte[] audioBytes = Resources.toByteArray(new URL("https://storage.googleapis.com/gapic-toolkit/hello.flac"));
80+
81+
StreamingRecognitionConfig streamingConfig = StreamingRecognitionConfig.newBuilder()
82+
.setConfig(config())
83+
.build();
84+
85+
ResponseApiStreamingObserver<StreamingRecognizeResponse> responseObserver =
86+
new ResponseApiStreamingObserver<>();
87+
88+
ApiStreamObserver<StreamingRecognizeRequest> requestObserver =
89+
speechClient.streamingRecognizeCallable().bidiStreamingCall(responseObserver);
90+
91+
// The first request must **only** contain the audio configuration:
92+
requestObserver.onNext(StreamingRecognizeRequest.newBuilder()
93+
.setStreamingConfig(streamingConfig)
94+
.build());
95+
96+
// Subsequent requests must **only** contain the audio data.
97+
requestObserver.onNext(StreamingRecognizeRequest.newBuilder()
98+
.setAudioContent(ByteString.copyFrom(audioBytes))
99+
.build());
100+
101+
// Mark transmission as completed after sending the data.
102+
requestObserver.onCompleted();
103+
104+
List<StreamingRecognizeResponse> responses = responseObserver.future().get();
105+
106+
Truth.assertThat(responses.size()).isGreaterThan(0);
107+
Truth.assertThat(responses.get(0).getResultsCount()).isGreaterThan(0);
108+
Truth.assertThat(responses.get(0).getResults(0).getAlternativesCount()).isGreaterThan(0);
109+
String text = responses.get(0).getResults(0).getAlternatives(0).getTranscript();
110+
Truth.assertThat(text).isEqualTo("hello");
111+
}
112+
113+
private static class ResponseApiStreamingObserver<T> implements ApiStreamObserver<T> {
114+
private final SettableFuture<List<T>> future = SettableFuture.create();
115+
private final List<T> messages = new java.util.ArrayList<T>();
116+
117+
@Override
118+
public void onNext(T message) {
119+
messages.add(message);
120+
}
121+
122+
@Override
123+
public void onError(Throwable t) {
124+
future.setException(t);
125+
}
126+
127+
@Override
128+
public void onCompleted() {
129+
future.set(messages);
130+
}
131+
132+
// Returns the SettableFuture object to get received messages / exceptions.
133+
public SettableFuture<List<T>> future() {
134+
return future;
135+
}
136+
}
137+
138+
private RecognitionConfig config() {
139+
String languageCode = "en-US";
140+
int sampleRateHertz = 44100;
141+
RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
142+
RecognitionConfig config =
143+
RecognitionConfig.newBuilder()
144+
.setLanguageCode(languageCode)
145+
.setSampleRateHertz(sampleRateHertz)
146+
.setEncoding(encoding)
147+
.build();
148+
return config;
149+
}
150+
151+
public RecognitionAudio audio() {
152+
return RecognitionAudio.newBuilder().setUri("gs://gapic-toolkit/hello.flac").build();
153+
}
154+
}

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@
140140
<api-client.version>1.23.0</api-client.version>
141141

142142
<api-common.version>1.2.0</api-common.version>
143-
<gax.version>1.14.0</gax.version>
144-
<gax-grpc.version>1.14.0</gax-grpc.version>
143+
<gax.version>1.15.0</gax.version>
144+
<gax-grpc.version>1.15.0</gax-grpc.version>
145145
<generated-proto-beta.version>0.1.24</generated-proto-beta.version>
146146
<generated-proto-ga.version>1.0.0</generated-proto-ga.version>
147147
<google.auth.version>0.9.0</google.auth.version>

0 commit comments

Comments
 (0)