Skip to content

Commit

Permalink
fix: add close method in fallbackServiceStub (#1182)
Browse files Browse the repository at this point in the history
  • Loading branch information
summer-ji-eng authored Feb 11, 2022
1 parent a7d4f6d commit a1153e9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/fallbackServiceStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ export function generateServiceStub(
? window.fetch
: (nodeFetch as unknown as NodeFetchType);

const serviceStub: FallbackServiceStub = {};
const serviceStub: FallbackServiceStub = {
// close method should close all cancel controllers. If this feature request in the future, we can have a cancelControllerFactory that tracks created cancel controllers, and abort them all in close method.
close: () => {
return {cancel: () => {}};
},
};
for (const [rpcName, rpc] of Object.entries(rpcs)) {
serviceStub[rpcName] = (
request: {},
Expand Down
19 changes: 15 additions & 4 deletions test/unit/grpc-fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ describe('createStub', () => {
assert.strictEqual(typeof echoStub.echo, 'function');
assert.strictEqual(typeof echoStub.pagedExpand, 'function');
assert.strictEqual(typeof echoStub.wait, 'function');
assert.strictEqual(typeof echoStub.close, 'function');

// There should be 7 methods for the echo service
assert.strictEqual(Object.keys(echoStub).length, 7);
// There should be 7 methods for the echo service + 1 close method.
assert.strictEqual(Object.keys(echoStub).length, 8);

// Each of the service methods should take 4 arguments (so that it works with createApiCall)
assert.strictEqual(echoStub.echo.length, 4);
Expand All @@ -143,9 +144,10 @@ describe('createStub', () => {
assert.strictEqual(typeof echoStub.echo, 'function');
assert.strictEqual(typeof echoStub.collect, 'function');
assert.strictEqual(typeof echoStub.chat, 'function');
assert.strictEqual(typeof echoStub.close, 'function');

// There should be 7 methods for the echo service
assert.strictEqual(Object.keys(echoStub).length, 7);
// There should be 7 methods for the echo service + 1 close method.
assert.strictEqual(Object.keys(echoStub).length, 8);

// Each of the service methods should take 4 arguments (so that it works with createApiCall)
assert.strictEqual(echoStub.echo.length, 4);
Expand Down Expand Up @@ -405,4 +407,13 @@ describe('grpc-fallback', () => {
// @ts-ignore
assert.strictEqual(createdAbortControllers[0].abortCalled, true);
});

it('should have close method', done => {
// @ts-ignore
sinon.stub(nodeFetch, 'Promise').returns(Promise.resolve({}));
gaxGrpc.createStub(echoService, stubOptions).then(stub => {
stub.close({}, {}, {}, () => {});
done();
});
});
});

0 comments on commit a1153e9

Please sign in to comment.