Skip to content

Commit

Permalink
pubsub: Improving test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
rakyll committed Jul 28, 2014
1 parent 8429c24 commit 2461ac4
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 4 deletions.
10 changes: 10 additions & 0 deletions lib/pubsub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,13 @@ Connection.prototype.makeReq = function(method, path, q, body, callback) {
* Exports Connection.
*/
module.exports.Connection = Connection;

/**
* Exports Topic.
*/
module.exports.Topic = Topic;

/**
* Exports Subscription.
*/
module.exports.Subscription = Subscription;
40 changes: 36 additions & 4 deletions regression/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,21 @@ before(function(done) {
describe('Topic', function() {

it('should be listed', function(done) {
// TODO(jbd): Add pagination.
conn.listTopics(function(err, topics) {
assert(topics.length, 3);
done(err);
});
});

it('should return a nextQuery if there are more results', function(done) {
conn.listTopics({ maxResults: 2 }, function(err, topics, next) {
assert(topics.length, 2);
assert(next.maxResults, 2);
assert(!!next.pageToken, true);
done(err);
});
})

it('should be created', function(done) {
conn.createTopic('topic-new', done);
});
Expand Down Expand Up @@ -121,8 +129,7 @@ describe('Subscription', function() {
});
});

// TODO(jbd): Add assertions.
it('should get a subscription', function(done) {
it('should be gettable', function(done) {
conn.getSubscription('sub1', function(err, sub) {
if (err) {
done(err); return;
Expand All @@ -139,11 +146,36 @@ describe('Subscription', function() {
});
});

it('should create a subscription', function(done) {
it('should be created', function(done) {
conn.createSubscription({
topic: 'topic1',
name: 'new-sub'
}, done);
});

it('should be able to pull and ack', function(done) {
conn.getTopic('topic1', function(err, topic) {
if (err) {
done(err); return;
}
topic.publish('hello', function(err) {
if(err) done(err); return;
});
});
conn.getSubscription('sub1', function(err, sub) {
if (err) {
done(err); return;
}
sub.on('message', function(msg) {
sub.ack(msg.ackId, done);
});
sub.pull({}, function(err) {
if (err) {
done(err);
return;
}
});
});
});

});
97 changes: 97 additions & 0 deletions test/pubsub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* Copyright 2014 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var assert = require('assert'),
pubsub = require('../lib/pubsub');

describe('Subscription', function() {

it('should ack messages if autoAck is set', function(done) {
var sub = new pubsub.Subscription({}, 'sub1');
sub.autoAck = true;
sub.conn.makeReq = function(method, path, qs, body, callback) {
if (path == 'subscriptions/pull') {
callback(null, { ackId: 'ackd-id' });
return;
}
if (path === 'subscriptions/acknowledge') {
done();
}
};
sub.pull({}, function() {});
});

it('should be closed', function(done) {
var sub = new pubsub.Subscription({}, 'sub1');
sub.close();
assert.strictEqual(sub.closed, true);
done();
});

it('should pull messages', function(done) {
var conn = new pubsub.Connection({
projectId: 'test-project'
});
conn.makeReq = function(method, path, qs, body, callback) {
switch (path) {
case 'subscriptions//subscriptions/test-project/sub1':
callback(null, {});
return;
case 'subscriptions/pull':
callback(null, { ackId: 123 });
return;
}
};
var sub = conn.subscribe('sub1', { autoAck: false });
var doneCalled = false;
sub.on('message', function() {
if (!doneCalled) {
done();
}
doneCalled = true;
});
});

it('should pull and ack messages', function(done) {
var conn = new pubsub.Connection({
projectId: 'test-project'
});
conn.makeReq = function(method, path, qs, body, callback) {
switch (path) {
case 'subscriptions//subscriptions/test-project/sub1':
callback(null, {});
return;
case 'subscriptions/pull':
setTimeout(function() {
callback(null, { ackId: 123 });
}, 500);
return;
case 'subscriptions/acknowledge':
callback(null, true);
return;
}
};
var sub = conn.subscribe('sub1', { autoAck: true });
var doneCalled = false;
sub.on('message', function() {
if (!doneCalled) {
done();
}
doneCalled = true;
});
});

});

0 comments on commit 2461ac4

Please sign in to comment.