From 2d6a54dca37540caa6afa0886d473f0b3b1e97ec Mon Sep 17 00:00:00 2001 From: Christoph Tavan Date: Fri, 3 Feb 2017 22:23:59 +0100 Subject: [PATCH] pubsub: Fix broken abortion of outstanding requests This restores the fix from GoogleCloudPlatform/google-cloud-node#1105 which was broken after GoogleCloudPlatform/google-cloud-node#1070 --- packages/common-grpc/src/service.js | 4 +++- packages/common-grpc/test/service.js | 12 ++++++++++++ packages/pubsub/src/subscription.js | 4 ++-- packages/pubsub/test/subscription.js | 4 ++-- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/common-grpc/src/service.js b/packages/common-grpc/src/service.js index 3230f784049f..3b0b2f4cdd43 100644 --- a/packages/common-grpc/src/service.js +++ b/packages/common-grpc/src/service.js @@ -280,7 +280,9 @@ GrpcService.prototype.request = function(protoOpts, reqOpts, callback) { } }; - retryRequest(null, retryOpts, function(err, resp) { + // Return the request object to allow abort()-ing long-running requests (e.g. + // pub/sub pulls) + return retryRequest(null, retryOpts, function(err, resp) { if (!err && resp === respError) { err = respError; resp = null; diff --git a/packages/common-grpc/test/service.js b/packages/common-grpc/test/service.js index 0878b5996154..7e6c620afba4 100644 --- a/packages/common-grpc/test/service.js +++ b/packages/common-grpc/test/service.js @@ -648,6 +648,18 @@ describe('GrpcService', function() { delete global.GCLOUD_SANDBOX_ENV; }); + it('should return request object with abort() method', function(done) { + grpcService.protos.Service = { + service: function() { + setImmediate(done); + return new ProtoService(); + } + }; + + var request = grpcService.request(PROTO_OPTS, REQ_OPTS, assert.ifError); + assert.strictEqual(typeof request.abort, 'function'); + }); + it('should access the specified service proto object', function(done) { grpcService.protos.CustomService = { CustomService: function() { diff --git a/packages/pubsub/src/subscription.js b/packages/pubsub/src/subscription.js index 53b968f90191..e3e759d1d00a 100644 --- a/packages/pubsub/src/subscription.js +++ b/packages/pubsub/src/subscription.js @@ -732,8 +732,8 @@ Subscription.prototype.listenForEvents_ = function() { if (event === 'message' && --self.messageListeners === 0) { self.closed = true; - if (self.activeRequest_ && self.activeRequest_.cancel) { - self.activeRequest_.cancel(); + if (self.activeRequest_ && self.activeRequest_.abort) { + self.activeRequest_.abort(); } } }); diff --git a/packages/pubsub/test/subscription.js b/packages/pubsub/test/subscription.js index f28bb98c63ab..4f8002615bd9 100644 --- a/packages/pubsub/test/subscription.js +++ b/packages/pubsub/test/subscription.js @@ -961,11 +961,11 @@ describe('Subscription', function() { assert.strictEqual(subscription.closed, true); }); - it('should cancel the HTTP request when listeners removed', function(done) { + it('should abort the HTTP request when listeners removed', function(done) { subscription.startPulling_ = util.noop; subscription.activeRequest_ = { - cancel: done + abort: done }; subscription.on('message', util.noop);