Skip to content

Commit

Permalink
fix: add an internal getOperation method in operation client (#812)
Browse files Browse the repository at this point in the history
* add internal getOperation call

* add unit test for getOperationInternal

* fix

* fix
  • Loading branch information
xiaozhenliu-gg5 authored Apr 23, 2020
1 parent c8e98b0 commit cc99de0
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/longRunningCalls/longrunning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export class Operation extends EventEmitter {
}
const request = new operationProtos.google.longrunning.GetOperationRequest();
request.name = this.latestResponse.name;
this.currentCallPromise_ = operationsClient.getOperation(
this.currentCallPromise_ = operationsClient.getOperationInternal(
request,
this._callOptions!
);
Expand Down
16 changes: 14 additions & 2 deletions src/operationsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,19 @@ export class OperationsClient {
}

// Service calls

getOperationInternal(
request: protos.google.longrunning.GetOperationRequest,
options?: gax.CallOptions,
callback?: Callback<
protos.google.longrunning.Operation,
protos.google.longrunning.GetOperationRequest,
{} | null | undefined
>
): CancellablePromise<ResultTuple> {
request = request || {};
options = options || {};
return this.innerApiCalls.getOperation(request, options, callback);
}
/**
* Gets the latest state of a long-running operation. Clients can use this
* method to poll the operation result at intervals as recommended by the API
Expand Down Expand Up @@ -208,7 +220,7 @@ export class OperationsClient {
protos.google.longrunning.GetOperationRequest,
{} | null | undefined
>
): CancellablePromise<ResultTuple> {
): Promise<protos.google.longrunning.Operation> {
let options: gax.CallOptions;
if (optionsOrCallback instanceof Function && callback === undefined) {
callback = (optionsOrCallback as unknown) as Callback<
Expand Down
1 change: 1 addition & 0 deletions test/unit/longrunning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ describe('longrunning', () => {
return Promise.resolve();
});
return {
getOperationInternal: getOperationSpy,
getOperation: getOperationSpy,
cancelOperation: cancelOperationSpy,
cancelGetOperationSpy,
Expand Down
103 changes: 103 additions & 0 deletions test/unit/operationClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {OperationsClientBuilder} from '../../src/operationsClient';
import * as protobuf from 'protobufjs';
import {GrpcClient} from '../../src/grpc';
import {PassThrough} from 'stream';
import {ResultTuple} from '../../src/apitypes';

function generateSampleMessage<T extends object>(instance: T) {
const filledObject = (instance.constructor as typeof protobuf.Message).toObject(
Expand Down Expand Up @@ -209,6 +210,108 @@ describe('operation client', () => {
);
});
});
describe('getOperationInternal ', () => {
it('invokes getOperationInternal without error', async () => {
const grpcClient = new GrpcClient();
const clientOptions = {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
};
const client = new OperationsClientBuilder(grpcClient).operationsClient(
clientOptions
);

const request = generateSampleMessage(
new protos.google.longrunning.GetOperationRequest()
);
const expectedResponse: ResultTuple = [
new protos.google.longrunning.Operation(),
null,
new protos.google.longrunning.Operation(),
];
client.innerApiCalls.getOperation = stubSimpleCall(expectedResponse);
const response = await client.getOperationInternal(request);
assert.deepStrictEqual(response, [expectedResponse]);
assert(
(client.innerApiCalls.getOperation as SinonStub)
.getCall(0)
.calledWith(request)
);
});

it('invokes getOperation without error using callback', async () => {
const grpcClient = new GrpcClient();
const clientOptions = {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
};
const client = new OperationsClientBuilder(grpcClient).operationsClient(
clientOptions
);

const request = generateSampleMessage(
new protos.google.longrunning.GetOperationRequest()
);
const expectedResponse: ResultTuple = [
new protos.google.longrunning.Operation(),
null,
new protos.google.longrunning.Operation(),
];
client.innerApiCalls.getOperation = stubSimpleCallWithCallback(
expectedResponse
);
const promise = new Promise((resolve, reject) => {
client.getOperationInternal(
request,
undefined,
(
err?: Error | null,
result?: protos.google.longrunning.Operation | null
) => {
if (err) {
reject(err);
} else {
resolve(result);
}
}
);
});
const response = await promise;
assert.deepStrictEqual(response, expectedResponse);
assert(
(client.innerApiCalls.getOperation as SinonStub)
.getCall(0)
.calledWith(request /* callback function above */)
);
});

it('invokes getOperationInternal with error', async () => {
const grpcClient = new GrpcClient();
const clientOptions = {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
};
const client = new OperationsClientBuilder(grpcClient).operationsClient(
clientOptions
);
const request = generateSampleMessage(
new protos.google.longrunning.GetOperationRequest()
);
const expectedError = new Error('expected');
client.innerApiCalls.getOperation = stubSimpleCall(
undefined,
expectedError
);
await assert.rejects(async () => {
await client.getOperationInternal(request);
}, expectedError);
assert(
(client.innerApiCalls.getOperation as SinonStub)
.getCall(0)
.calledWith(request)
);
});
});
describe('listOperations ', () => {
it('invokes listOperations without error', async () => {
const grpcClient = new GrpcClient();
Expand Down

0 comments on commit cc99de0

Please sign in to comment.