diff --git a/lib/common/util.js b/lib/common/util.js index f023ac074edf..122446e5232b 100644 --- a/lib/common/util.js +++ b/lib/common/util.js @@ -14,7 +14,7 @@ * limitations under the License. */ -/*jshint strict:false, noarg:false */ +/*jshint strict:false, noarg:false, eqnull:true */ /** * @private @@ -92,7 +92,7 @@ module.exports.extendGlobalConfig = extendGlobalConfig; * // [ 'Hi' ] */ function arrayize(input) { - if (!input) { + if (input == null) { return []; } diff --git a/lib/pubsub/subscription.js b/lib/pubsub/subscription.js index 3986f4e5e220..2bfd763434d6 100644 --- a/lib/pubsub/subscription.js +++ b/lib/pubsub/subscription.js @@ -252,7 +252,7 @@ Subscription.prototype.startPulling_ = function() { Subscription.prototype.ack = function(ids, callback) { if (!ids || ids.length === 0) { throw new Error( - 'At least one ID must be specified before it can be acknowledged'); + 'At least one ID must be specified before it can be acknowledged.'); } ids = util.arrayize(ids); var body = { @@ -326,13 +326,7 @@ Subscription.prototype.delete = function(callback) { * maxResults: 1 * }; * - * subscription.pull(opts, function(err, message) { - * // message = { - * // ackId: '', // ID used to acknowledge its receival. - * // id: '', // Unique message ID. - * // data: '' // Contents of the message. - * // } - * }); + * subscription.pull(opts, function(err, messages) {}); */ Subscription.prototype.pull = function(options, callback) { var that = this; @@ -363,18 +357,16 @@ Subscription.prototype.pull = function(options, callback) { var messages = response.pullResponses || [response]; messages = messages.map(Subscription.formatMessage_); - var messageResponse = response.pullResponses ? messages : messages[0]; - if (that.autoAck) { var ackIds = messages.map(function(message) { return message.ackId; }); that.ack(ackIds, function(err) { - callback(err, messageResponse); + callback(err, messages); }); } else { - callback(null, messageResponse); + callback(null, messages); } }); }; diff --git a/regression/pubsub.js b/regression/pubsub.js index 158659881c61..9afcfe84c5b8 100644 --- a/regression/pubsub.js +++ b/regression/pubsub.js @@ -81,7 +81,7 @@ describe('pubsub', function() { it('should publish a message', function(done) { pubsub.topic(topicNames[0]) - .publish('message from me', done); + .publish({ data: 'message from me' }, done); }); it('should be deleted', function(done) { @@ -176,15 +176,15 @@ describe('pubsub', function() { it('should be able to pull and ack', function(done) { var subscription = topic.subscription(subscriptions[0].name); - topic.publish('hello', function(err) { + topic.publish({ data: 'hello' }, function(err) { assert.ifError(err); subscription.pull({ returnImmediately: true, - maxCount: 1 - }, function(err, msg) { + maxResults: 1 + }, function(err, msgs) { assert.ifError(err); - subscription.ack(msg.ackId, done); + subscription.ack(msgs[0].ackId, done); }); }); }); @@ -193,26 +193,26 @@ describe('pubsub', function() { var subscription = topic.subscription(subscriptions[0].name); topic.publish([ - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') } + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' } ], function(err) { assert.ifError(err); subscription.pull({ returnImmediately: true, - maxCount: 1 - }, function(err, msg) { + maxResults: 1 + }, function(err, msgs) { assert.ifError(err); - assert.equal(msg.data, 'hello'); - subscription.ack(msg.ackId, done); + assert.equal(msgs[0].data, 'hello'); + subscription.ack(msgs[0].ackId, done); }); }); }); @@ -221,52 +221,52 @@ describe('pubsub', function() { var subscription = topic.subscription(subscriptions[0].name); topic.publish([ - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') } + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' } ], function(err) { assert.ifError(err); subscription.pull({ returnImmediately: true, - maxCount: 1 - }, function(err, msg) { + maxResults: 1 + }, function(err, msgs) { assert.ifError(err); - assert.equal(msg.data, 'hello'); - subscription.ack(msg.ackId, done); + assert.equal(msgs[0].data, 'hello'); + subscription.ack(msgs[0].ackId, done); }); }); }); it('should receive the chosen amount of results', function(done) { var subscription = topic.subscription(subscriptions[0].name); - var opts = { returnImmediately: true, maxCount: 3 }; + var opts = { returnImmediately: true, maxResults: 3 }; topic.publish([ - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') }, - { data: new Buffer('hello').toString('base64') } + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' }, + { data: 'hello' } ], function(err) { assert.ifError(err); subscription.pull(opts, function(err, messages) { assert.ifError(err); - assert.equal(messages.length, opts.maxCount); + assert.equal(messages.length, opts.maxResults); var ackIds = messages.map(function(message) { return message.ackId; diff --git a/test/common/util.js b/test/common/util.js index b648a3cfdfd3..02d49d7c0448 100644 --- a/test/common/util.js +++ b/test/common/util.js @@ -62,10 +62,21 @@ describe('common/util', function() { }); describe('arrayize', function() { - it('should arrayize if the input is not an array', function(done) { - var o = util.arrayize('text'); - assert.deepEqual(o, ['text']); - done(); + it('should arrayize if the input is not an array', function() { + assert.deepEqual(util.arrayize('text'), ['text']); + }); + + it('should return the same array if given an array', function() { + var arr = [1, 2, 3]; + assert.deepEqual(util.arrayize(arr), arr); + }); + + it('should return an empty array in correct circumstance', function() { + assert.deepEqual(util.arrayize(undefined), []); + assert.deepEqual(util.arrayize(null), []); + + assert.deepEqual(util.arrayize(false), [false]); + assert.deepEqual(util.arrayize(0), [0]); }); }); diff --git a/test/pubsub/subscription.js b/test/pubsub/subscription.js index d18fbd10fc6c..6e6368289ab6 100644 --- a/test/pubsub/subscription.js +++ b/test/pubsub/subscription.js @@ -243,10 +243,10 @@ describe('Subscription', function() { callback(null, apiResponse); }; - subscription.pull(function(err, message) { + subscription.pull(function(err, msgs) { assert.ifError(err); - assert.deepEqual(message, Subscription.formatMessage_(apiResponse)); + assert.deepEqual(msgs, [Subscription.formatMessage_(apiResponse)]); done(); }); @@ -310,7 +310,8 @@ describe('Subscription', function() { assert.deepEqual( messages, - apiResponse.pullResponses.map(Subscription.formatMessage_)); + apiResponse.pullResponses.map(Subscription.formatMessage_) + ); done(); }); @@ -341,8 +342,8 @@ describe('Subscription', function() { }); it('should execute callback with message', function(done) { - subscription.pull({}, function(err, msg) { - assert.deepEqual(msg, expectedMessage); + subscription.pull({}, function(err, msgs) { + assert.deepEqual(msgs, [expectedMessage]); done(); }); });