Skip to content

Commit

Permalink
fix: provide missing close() method in the generated gapic client (#941)
Browse files Browse the repository at this point in the history
* fix: backport the close() method to the publisher client from the typescript gapic generator

* fix: add the publisher_client.js hack to synth.py and update publisher_client.js to match it

* fix: add a system-test to verify that calling close() works

Co-authored-by: Justin Beckwith <justin.beckwith@gmail.com>
  • Loading branch information
feywind and JustinBeckwith committed Apr 6, 2020
1 parent 0cc4881 commit 6bf8f14
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/v1/publisher_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ class PublisherClient {

// Iterate over each of the methods that the service provides
// and create an API call method for each.
// note: editing generated code
this.publisherStub = publisherStub;
const publisherStubMethods = [
'createTopic',
'updateTopic',
Expand Down Expand Up @@ -1170,6 +1172,18 @@ class PublisherClient {
});
}

/**
* Terminate the GRPC channel and close the client.
* note: editing generated code
*
* The client will no longer be usable and all future behavior is undefined.
*/
close() {
return this.publisherStub.then(stub => {
stub.close();
});
}

/**
* Parse the projectName from a project resource.
*
Expand Down
23 changes: 23 additions & 0 deletions synth.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@
" };\n"
"\g<0>")

# The JavaScript generator didn't implement close(). TypeScript gapic does,
# so this should be removed when we merge that.
s.replace("src/v1/publisher_client.js",
" \* Parse the projectName from a project resource.\n",
" * Terminate the GRPC channel and close the client.\n"
" * note: editing generated code\n"
" *\n"
" * The client will no longer be usable and all future behavior is undefined.\n"
" */\n"
" close() {\n"
" return this.publisherStub.then(stub => {\n"
" stub.close();\n"
" });\n"
" }\n"
" \n"
" /**\n"
" \g<0>")
s.replace("src/v1/publisher_client.js",
" const publisherStubMethods = \\[\n",
" // note: editing generated code\n"
" this.publisherStub = publisherStub;\n"
" \g<0>")

# Update path discovery due to build/ dir and TypeScript conversion.
s.replace("src/v1/publisher_client.js", "../../package.json", "../../../package.json")
s.replace("src/v1/subscriber_client.js", "../../package.json", "../../../package.json")
Expand Down
13 changes: 13 additions & 0 deletions system-test/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,4 +888,17 @@ describe('pubsub', () => {
});
});
});

it('should allow closing of publisher clients', async () => {
// The full call stack of close() is tested in unit tests; this is mostly
// to verify that the close() method is actually there and doesn't error.
const localPubsub = new PubSub();

// Just use the client object to make sure it has opened a connection.
await pubsub.getTopics();

// Tell it to close, and validate that it's marked closed.
await localPubsub.close();
assert.strictEqual(localPubsub.isOpen, false);
});
});

0 comments on commit 6bf8f14

Please sign in to comment.