Skip to content

Commit

Permalink
modify helpers.js to be compatible with generated code (#116)
Browse files Browse the repository at this point in the history
* modify helpers.js to be compatible with generated code

* add newline to config
  • Loading branch information
crwilcox authored Jul 13, 2018
1 parent f85a5b4 commit b8de9e7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 49 deletions.
5 changes: 3 additions & 2 deletions packages/google-cloud-node/.circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ jobs:
command: npm run samples-test
environment:
GCLOUD_PROJECT: long-door-651
GOOGLE_APPLICATION_CREDENTIALS: /home/node/speech-samples/.circleci/key.json
GOOGLE_APPLICATION_CREDENTIALS: /home/node/samples/.circleci/key.json
NPM_CONFIG_PREFIX: /home/node/.npm-global
- run:
name: Remove unencrypted key.
command: rm .circleci/key.json
when: always
working_directory: /home/node/speech-samples/
working_directory: /home/node/samples/
system_tests:
docker:
- image: 'node:8'
Expand Down Expand Up @@ -181,3 +181,4 @@ jobs:
- checkout
- run: 'echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc'
- run: npm publish

20 changes: 13 additions & 7 deletions packages/google-cloud-node/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,8 @@ module.exports = () => {
recognizeStream.emit('response', response);
});

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

// Set up appropriate piping between the stream returned by
// the underlying API method and the one that we return.
Expand All @@ -99,9 +97,17 @@ module.exports = () => {
// This entails that the user sends raw audio; it is wrapped in
// the appropriate request structure.
through.obj((obj, _, next) => {
next(null, {
audioContent: obj,
});
let payload = {};
if (first_message && config !== undefined) {
// Write the initial configuration to the stream.
payload.streamingConfig = config;
}

if (Object.keys(obj || {}).length) {
payload.audioContent = obj;
}

next(null, payload);
}),
requestStream,
through.obj(),
Expand Down
6 changes: 2 additions & 4 deletions packages/google-cloud-node/synth.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@
library = gapic.node_library('speech', version)

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

templates = common_templates.node_library(package_name="@google-cloud/speech")
s.copy(templates)

#
# Node.js specific cleanup
#
Expand Down
20 changes: 5 additions & 15 deletions packages/google-cloud-node/test/gapic-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,18 @@ describe('SpeechClient', () => {

// Mock response
var expectedResponse = {};
var expectedRequests = [{streamingConfig: undefined}, {audioContent: {}}];

// Mock Grpc layer
client._innerApiCalls.streamingRecognize = mockBidiStreamingGrpcMethod(
expectedRequests,
request,
expectedResponse
);

let loops = 0;
var stream = client
.streamingRecognize()
.on('data', response => {
assert.deepStrictEqual(response, expectedResponse);
if (++loops === expectedRequests.length) {
done();
}
done();
})
.on('error', err => {
done(err);
Expand All @@ -261,15 +257,10 @@ describe('SpeechClient', () => {

// Mock request
var request = {};
var expectedRequests = [
{streamingConfig: undefined},
{streamingConfig: undefined},
{streamingConfig: undefined},
];

// Mock Grpc layer
client._innerApiCalls.streamingRecognize = mockBidiStreamingGrpcMethod(
expectedRequests,
request,
null,
error
);
Expand Down Expand Up @@ -303,11 +294,10 @@ function mockSimpleGrpcMethod(expectedRequest, response, error) {
};
}

let callCount = 0;
function mockBidiStreamingGrpcMethod(expectedRequests, response, error) {
function mockBidiStreamingGrpcMethod(expectedRequest, response, error) {
return () => {
var mockStream = through2.obj((chunk, enc, callback) => {
assert.deepStrictEqual(chunk, expectedRequests[callCount++]);
assert.deepStrictEqual(chunk, expectedRequest);
if (error) {
callback(error);
} else {
Expand Down
20 changes: 5 additions & 15 deletions packages/google-cloud-node/test/gapic-v1p1beta1.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,25 +226,21 @@ describe('SpeechClient', () => {

// Mock request
var request = {};
var expectedRequests = [{streamingConfig: undefined}, {audioContent: {}}];

// Mock response
var expectedResponse = {};

// Mock Grpc layer
client._innerApiCalls.streamingRecognize = mockBidiStreamingGrpcMethod(
expectedRequests,
request,
expectedResponse
);

let loops = 0;
var stream = client
.streamingRecognize()
.on('data', response => {
assert.deepStrictEqual(response, expectedResponse);
if (++loops === expectedRequests.length) {
done();
}
done();
})
.on('error', err => {
done(err);
Expand All @@ -261,15 +257,10 @@ describe('SpeechClient', () => {

// Mock request
var request = {};
var expectedRequests = [
{streamingConfig: undefined},
{streamingConfig: undefined},
{streamingConfig: undefined},
];

// Mock Grpc layer
client._innerApiCalls.streamingRecognize = mockBidiStreamingGrpcMethod(
expectedRequests,
request,
null,
error
);
Expand Down Expand Up @@ -303,11 +294,10 @@ function mockSimpleGrpcMethod(expectedRequest, response, error) {
};
}

let callCount = 0;
function mockBidiStreamingGrpcMethod(expectedRequests, response, error) {
function mockBidiStreamingGrpcMethod(expectedRequest, response, error) {
return () => {
var mockStream = through2.obj((chunk, enc, callback) => {
assert.deepStrictEqual(chunk, expectedRequests[callCount++]);
assert.deepStrictEqual(chunk, expectedRequest);
if (error) {
callback(error);
} else {
Expand Down
12 changes: 6 additions & 6 deletions packages/google-cloud-node/test/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('Speech helper methods', () => {
next(null, data);
};

userStream.emit('writing');
userStream.write(undefined);
});

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

requestStream._write = (data, enc, next) => {
if (data && data.streamingConfig !== CONFIG) {
assert.deepStrictEqual(data, {audioContent});
setImmediate(done);
}

assert.deepStrictEqual(data, {
audioContent: audioContent,
streamingConfig: CONFIG,
});
setImmediate(done);
next(null, data);
};

Expand Down

0 comments on commit b8de9e7

Please sign in to comment.