Skip to content

Commit 2813964

Browse files
authored
modify helpers.js to be compatible with generated code (#116)
* modify helpers.js to be compatible with generated code * add newline to config
1 parent 7eedaac commit 2813964

File tree

6 files changed

+34
-49
lines changed

6 files changed

+34
-49
lines changed

packages/google-cloud-speech/.circleci/config.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ jobs:
143143
command: npm run samples-test
144144
environment:
145145
GCLOUD_PROJECT: long-door-651
146-
GOOGLE_APPLICATION_CREDENTIALS: /home/node/speech-samples/.circleci/key.json
146+
GOOGLE_APPLICATION_CREDENTIALS: /home/node/samples/.circleci/key.json
147147
NPM_CONFIG_PREFIX: /home/node/.npm-global
148148
- run:
149149
name: Remove unencrypted key.
150150
command: rm .circleci/key.json
151151
when: always
152-
working_directory: /home/node/speech-samples/
152+
working_directory: /home/node/samples/
153153
system_tests:
154154
docker:
155155
- image: 'node:8'
@@ -181,3 +181,4 @@ jobs:
181181
- checkout
182182
- run: 'echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc'
183183
- run: npm publish
184+

packages/google-cloud-speech/src/helpers.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,8 @@ module.exports = () => {
8787
recognizeStream.emit('response', response);
8888
});
8989

90-
// Write the initial configuration to the stream.
91-
requestStream.write({
92-
streamingConfig: config,
93-
});
90+
// The first message should contain the streaming config.
91+
let first_message = true;
9492

9593
// Set up appropriate piping between the stream returned by
9694
// the underlying API method and the one that we return.
@@ -99,9 +97,17 @@ module.exports = () => {
9997
// This entails that the user sends raw audio; it is wrapped in
10098
// the appropriate request structure.
10199
through.obj((obj, _, next) => {
102-
next(null, {
103-
audioContent: obj,
104-
});
100+
let payload = {};
101+
if (first_message && config !== undefined) {
102+
// Write the initial configuration to the stream.
103+
payload.streamingConfig = config;
104+
}
105+
106+
if (Object.keys(obj || {}).length) {
107+
payload.audioContent = obj;
108+
}
109+
110+
next(null, payload);
105111
}),
106112
requestStream,
107113
through.obj(),

packages/google-cloud-speech/synth.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@
3030
library = gapic.node_library('speech', version)
3131

3232
# skip index, protos, package.json, and README.md
33-
# Skip tests as they are handedited until the Generator is fixed.
34-
# https://github.com/googleapis/gapic-generator/issues/2129
3533
s.copy(
3634
library,
37-
excludes=['package.json', 'README.md', 'src/index.js',
38-
f'test/gapic-{version}.js']
35+
excludes=['package.json', 'README.md', 'src/index.js',]
3936
)
4037

4138
templates = common_templates.node_library(package_name="@google-cloud/speech")
4239
s.copy(templates)
40+
4341
#
4442
# Node.js specific cleanup
4543
#

packages/google-cloud-speech/test/gapic-v1.js

+5-15
Original file line numberDiff line numberDiff line change
@@ -229,22 +229,18 @@ describe('SpeechClient', () => {
229229

230230
// Mock response
231231
var expectedResponse = {};
232-
var expectedRequests = [{streamingConfig: undefined}, {audioContent: {}}];
233232

234233
// Mock Grpc layer
235234
client._innerApiCalls.streamingRecognize = mockBidiStreamingGrpcMethod(
236-
expectedRequests,
235+
request,
237236
expectedResponse
238237
);
239238

240-
let loops = 0;
241239
var stream = client
242240
.streamingRecognize()
243241
.on('data', response => {
244242
assert.deepStrictEqual(response, expectedResponse);
245-
if (++loops === expectedRequests.length) {
246-
done();
247-
}
243+
done();
248244
})
249245
.on('error', err => {
250246
done(err);
@@ -261,15 +257,10 @@ describe('SpeechClient', () => {
261257

262258
// Mock request
263259
var request = {};
264-
var expectedRequests = [
265-
{streamingConfig: undefined},
266-
{streamingConfig: undefined},
267-
{streamingConfig: undefined},
268-
];
269260

270261
// Mock Grpc layer
271262
client._innerApiCalls.streamingRecognize = mockBidiStreamingGrpcMethod(
272-
expectedRequests,
263+
request,
273264
null,
274265
error
275266
);
@@ -303,11 +294,10 @@ function mockSimpleGrpcMethod(expectedRequest, response, error) {
303294
};
304295
}
305296

306-
let callCount = 0;
307-
function mockBidiStreamingGrpcMethod(expectedRequests, response, error) {
297+
function mockBidiStreamingGrpcMethod(expectedRequest, response, error) {
308298
return () => {
309299
var mockStream = through2.obj((chunk, enc, callback) => {
310-
assert.deepStrictEqual(chunk, expectedRequests[callCount++]);
300+
assert.deepStrictEqual(chunk, expectedRequest);
311301
if (error) {
312302
callback(error);
313303
} else {

packages/google-cloud-speech/test/gapic-v1p1beta1.js

+5-15
Original file line numberDiff line numberDiff line change
@@ -226,25 +226,21 @@ describe('SpeechClient', () => {
226226

227227
// Mock request
228228
var request = {};
229-
var expectedRequests = [{streamingConfig: undefined}, {audioContent: {}}];
230229

231230
// Mock response
232231
var expectedResponse = {};
233232

234233
// Mock Grpc layer
235234
client._innerApiCalls.streamingRecognize = mockBidiStreamingGrpcMethod(
236-
expectedRequests,
235+
request,
237236
expectedResponse
238237
);
239238

240-
let loops = 0;
241239
var stream = client
242240
.streamingRecognize()
243241
.on('data', response => {
244242
assert.deepStrictEqual(response, expectedResponse);
245-
if (++loops === expectedRequests.length) {
246-
done();
247-
}
243+
done();
248244
})
249245
.on('error', err => {
250246
done(err);
@@ -261,15 +257,10 @@ describe('SpeechClient', () => {
261257

262258
// Mock request
263259
var request = {};
264-
var expectedRequests = [
265-
{streamingConfig: undefined},
266-
{streamingConfig: undefined},
267-
{streamingConfig: undefined},
268-
];
269260

270261
// Mock Grpc layer
271262
client._innerApiCalls.streamingRecognize = mockBidiStreamingGrpcMethod(
272-
expectedRequests,
263+
request,
273264
null,
274265
error
275266
);
@@ -303,11 +294,10 @@ function mockSimpleGrpcMethod(expectedRequest, response, error) {
303294
};
304295
}
305296

306-
let callCount = 0;
307-
function mockBidiStreamingGrpcMethod(expectedRequests, response, error) {
297+
function mockBidiStreamingGrpcMethod(expectedRequest, response, error) {
308298
return () => {
309299
var mockStream = through2.obj((chunk, enc, callback) => {
310-
assert.deepStrictEqual(chunk, expectedRequests[callCount++]);
300+
assert.deepStrictEqual(chunk, expectedRequest);
311301
if (error) {
312302
callback(error);
313303
} else {

packages/google-cloud-speech/test/helpers.test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('Speech helper methods', () => {
6666
next(null, data);
6767
};
6868

69-
userStream.emit('writing');
69+
userStream.write(undefined);
7070
});
7171

7272
it('does not require options', () => {
@@ -140,11 +140,11 @@ describe('Speech helper methods', () => {
140140
var audioContent = Buffer.from('audio content');
141141

142142
requestStream._write = (data, enc, next) => {
143-
if (data && data.streamingConfig !== CONFIG) {
144-
assert.deepStrictEqual(data, {audioContent});
145-
setImmediate(done);
146-
}
147-
143+
assert.deepStrictEqual(data, {
144+
audioContent: audioContent,
145+
streamingConfig: CONFIG,
146+
});
147+
setImmediate(done);
148148
next(null, data);
149149
};
150150

0 commit comments

Comments
 (0)