diff --git a/src/v1/publisher_client.js b/src/v1/publisher_client.js index a92b304fa..4410aa5cc 100644 --- a/src/v1/publisher_client.js +++ b/src/v1/publisher_client.js @@ -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', @@ -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. * diff --git a/synth.py b/synth.py index f25535a38..34149f068 100644 --- a/synth.py +++ b/synth.py @@ -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") diff --git a/system-test/pubsub.ts b/system-test/pubsub.ts index ac79049a1..1aab7783c 100644 --- a/system-test/pubsub.ts +++ b/system-test/pubsub.ts @@ -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); + }); });