Skip to content

Commit 367753e

Browse files
simplify publishing
1 parent e00d32f commit 367753e

5 files changed

Lines changed: 72 additions & 68 deletions

File tree

lib/common/util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
/*jshint strict:false, noarg:false */
17+
/*jshint strict:false, noarg:false, eqnull:true */
1818

1919
/**
2020
* @private
@@ -92,7 +92,7 @@ module.exports.extendGlobalConfig = extendGlobalConfig;
9292
* // [ 'Hi' ]
9393
*/
9494
function arrayize(input) {
95-
if (!input) {
95+
if (input == null) {
9696
return [];
9797
}
9898

lib/pubsub/subscription.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ Subscription.prototype.startPulling_ = function() {
252252
Subscription.prototype.ack = function(ids, callback) {
253253
if (!ids || ids.length === 0) {
254254
throw new Error(
255-
'At least one ID must be specified before it can be acknowledged');
255+
'At least one ID must be specified before it can be acknowledged.');
256256
}
257257
ids = util.arrayize(ids);
258258
var body = {
@@ -326,13 +326,7 @@ Subscription.prototype.delete = function(callback) {
326326
* maxResults: 1
327327
* };
328328
*
329-
* subscription.pull(opts, function(err, message) {
330-
* // message = {
331-
* // ackId: '', // ID used to acknowledge its receival.
332-
* // id: '', // Unique message ID.
333-
* // data: '' // Contents of the message.
334-
* // }
335-
* });
329+
* subscription.pull(opts, function(err, messages) {});
336330
*/
337331
Subscription.prototype.pull = function(options, callback) {
338332
var that = this;
@@ -363,18 +357,16 @@ Subscription.prototype.pull = function(options, callback) {
363357
var messages = response.pullResponses || [response];
364358
messages = messages.map(Subscription.formatMessage_);
365359

366-
var messageResponse = response.pullResponses ? messages : messages[0];
367-
368360
if (that.autoAck) {
369361
var ackIds = messages.map(function(message) {
370362
return message.ackId;
371363
});
372364

373365
that.ack(ackIds, function(err) {
374-
callback(err, messageResponse);
366+
callback(err, messages);
375367
});
376368
} else {
377-
callback(null, messageResponse);
369+
callback(null, messages);
378370
}
379371
});
380372
};

regression/pubsub.js

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('pubsub', function() {
8181

8282
it('should publish a message', function(done) {
8383
pubsub.topic(topicNames[0])
84-
.publish('message from me', done);
84+
.publish({ data: 'message from me' }, done);
8585
});
8686

8787
it('should be deleted', function(done) {
@@ -176,15 +176,15 @@ describe('pubsub', function() {
176176
it('should be able to pull and ack', function(done) {
177177
var subscription = topic.subscription(subscriptions[0].name);
178178

179-
topic.publish('hello', function(err) {
179+
topic.publish({ data: 'hello' }, function(err) {
180180
assert.ifError(err);
181181

182182
subscription.pull({
183183
returnImmediately: true,
184-
maxCount: 1
185-
}, function(err, msg) {
184+
maxResults: 1
185+
}, function(err, msgs) {
186186
assert.ifError(err);
187-
subscription.ack(msg.ackId, done);
187+
subscription.ack(msgs[0].ackId, done);
188188
});
189189
});
190190
});
@@ -193,26 +193,26 @@ describe('pubsub', function() {
193193
var subscription = topic.subscription(subscriptions[0].name);
194194

195195
topic.publish([
196-
{ data: new Buffer('hello').toString('base64') },
197-
{ data: new Buffer('hello').toString('base64') },
198-
{ data: new Buffer('hello').toString('base64') },
199-
{ data: new Buffer('hello').toString('base64') },
200-
{ data: new Buffer('hello').toString('base64') },
201-
{ data: new Buffer('hello').toString('base64') },
202-
{ data: new Buffer('hello').toString('base64') },
203-
{ data: new Buffer('hello').toString('base64') },
204-
{ data: new Buffer('hello').toString('base64') },
205-
{ data: new Buffer('hello').toString('base64') }
196+
{ data: 'hello' },
197+
{ data: 'hello' },
198+
{ data: 'hello' },
199+
{ data: 'hello' },
200+
{ data: 'hello' },
201+
{ data: 'hello' },
202+
{ data: 'hello' },
203+
{ data: 'hello' },
204+
{ data: 'hello' },
205+
{ data: 'hello' }
206206
], function(err) {
207207
assert.ifError(err);
208208

209209
subscription.pull({
210210
returnImmediately: true,
211-
maxCount: 1
212-
}, function(err, msg) {
211+
maxResults: 1
212+
}, function(err, msgs) {
213213
assert.ifError(err);
214-
assert.equal(msg.data, 'hello');
215-
subscription.ack(msg.ackId, done);
214+
assert.equal(msgs[0].data, 'hello');
215+
subscription.ack(msgs[0].ackId, done);
216216
});
217217
});
218218
});
@@ -221,52 +221,52 @@ describe('pubsub', function() {
221221
var subscription = topic.subscription(subscriptions[0].name);
222222

223223
topic.publish([
224-
{ data: new Buffer('hello').toString('base64') },
225-
{ data: new Buffer('hello').toString('base64') },
226-
{ data: new Buffer('hello').toString('base64') },
227-
{ data: new Buffer('hello').toString('base64') },
228-
{ data: new Buffer('hello').toString('base64') },
229-
{ data: new Buffer('hello').toString('base64') },
230-
{ data: new Buffer('hello').toString('base64') },
231-
{ data: new Buffer('hello').toString('base64') },
232-
{ data: new Buffer('hello').toString('base64') },
233-
{ data: new Buffer('hello').toString('base64') }
224+
{ data: 'hello' },
225+
{ data: 'hello' },
226+
{ data: 'hello' },
227+
{ data: 'hello' },
228+
{ data: 'hello' },
229+
{ data: 'hello' },
230+
{ data: 'hello' },
231+
{ data: 'hello' },
232+
{ data: 'hello' },
233+
{ data: 'hello' }
234234
], function(err) {
235235
assert.ifError(err);
236236

237237
subscription.pull({
238238
returnImmediately: true,
239-
maxCount: 1
240-
}, function(err, msg) {
239+
maxResults: 1
240+
}, function(err, msgs) {
241241
assert.ifError(err);
242-
assert.equal(msg.data, 'hello');
243-
subscription.ack(msg.ackId, done);
242+
assert.equal(msgs[0].data, 'hello');
243+
subscription.ack(msgs[0].ackId, done);
244244
});
245245
});
246246
});
247247

248248
it('should receive the chosen amount of results', function(done) {
249249
var subscription = topic.subscription(subscriptions[0].name);
250-
var opts = { returnImmediately: true, maxCount: 3 };
250+
var opts = { returnImmediately: true, maxResults: 3 };
251251

252252
topic.publish([
253-
{ data: new Buffer('hello').toString('base64') },
254-
{ data: new Buffer('hello').toString('base64') },
255-
{ data: new Buffer('hello').toString('base64') },
256-
{ data: new Buffer('hello').toString('base64') },
257-
{ data: new Buffer('hello').toString('base64') },
258-
{ data: new Buffer('hello').toString('base64') },
259-
{ data: new Buffer('hello').toString('base64') },
260-
{ data: new Buffer('hello').toString('base64') },
261-
{ data: new Buffer('hello').toString('base64') },
262-
{ data: new Buffer('hello').toString('base64') }
253+
{ data: 'hello' },
254+
{ data: 'hello' },
255+
{ data: 'hello' },
256+
{ data: 'hello' },
257+
{ data: 'hello' },
258+
{ data: 'hello' },
259+
{ data: 'hello' },
260+
{ data: 'hello' },
261+
{ data: 'hello' },
262+
{ data: 'hello' }
263263
], function(err) {
264264
assert.ifError(err);
265265

266266
subscription.pull(opts, function(err, messages) {
267267
assert.ifError(err);
268268

269-
assert.equal(messages.length, opts.maxCount);
269+
assert.equal(messages.length, opts.maxResults);
270270

271271
var ackIds = messages.map(function(message) {
272272
return message.ackId;

test/common/util.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,21 @@ describe('common/util', function() {
6262
});
6363

6464
describe('arrayize', function() {
65-
it('should arrayize if the input is not an array', function(done) {
66-
var o = util.arrayize('text');
67-
assert.deepEqual(o, ['text']);
68-
done();
65+
it('should arrayize if the input is not an array', function() {
66+
assert.deepEqual(util.arrayize('text'), ['text']);
67+
});
68+
69+
it('should return the same array if given an array', function() {
70+
var arr = [1, 2, 3];
71+
assert.deepEqual(util.arrayize(arr), arr);
72+
});
73+
74+
it('should return an empty array in correct circumstance', function() {
75+
assert.deepEqual(util.arrayize(undefined), []);
76+
assert.deepEqual(util.arrayize(null), []);
77+
78+
assert.deepEqual(util.arrayize(false), [false]);
79+
assert.deepEqual(util.arrayize(0), [0]);
6980
});
7081
});
7182

test/pubsub/subscription.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,10 @@ describe('Subscription', function() {
243243
callback(null, apiResponse);
244244
};
245245

246-
subscription.pull(function(err, message) {
246+
subscription.pull(function(err, msgs) {
247247
assert.ifError(err);
248248

249-
assert.deepEqual(message, Subscription.formatMessage_(apiResponse));
249+
assert.deepEqual(msgs, [Subscription.formatMessage_(apiResponse)]);
250250

251251
done();
252252
});
@@ -310,7 +310,8 @@ describe('Subscription', function() {
310310

311311
assert.deepEqual(
312312
messages,
313-
apiResponse.pullResponses.map(Subscription.formatMessage_));
313+
apiResponse.pullResponses.map(Subscription.formatMessage_)
314+
);
314315

315316
done();
316317
});
@@ -341,8 +342,8 @@ describe('Subscription', function() {
341342
});
342343

343344
it('should execute callback with message', function(done) {
344-
subscription.pull({}, function(err, msg) {
345-
assert.deepEqual(msg, expectedMessage);
345+
subscription.pull({}, function(err, msgs) {
346+
assert.deepEqual(msgs, [expectedMessage]);
346347
done();
347348
});
348349
});

0 commit comments

Comments
 (0)