Skip to content

Commit

Permalink
add system tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Oct 10, 2016
1 parent e7211c2 commit fcd1097
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 13 deletions.
4 changes: 4 additions & 0 deletions packages/pubsub/src/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

var arrify = require('arrify');
var common = require('@google-cloud/common');
var extend = require('extend');
var is = require('is');
var util = require('util');

Expand Down Expand Up @@ -367,6 +368,9 @@ Topic.prototype.publish = function(messages, options, callback) {
topic: this.name,
messages: messages
.map(function(message) {
if (is.object(message)) {
message = extend(true, {}, message);
}
return options.raw ? message : { data: message };
})
.map(Topic.formatMessage_)
Expand Down
69 changes: 56 additions & 13 deletions packages/pubsub/system-test/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ var uuid = require('node-uuid');
var env = require('../../../system-test/env.js');
var pubsub = require('../')(env);

function generateSubName() {
return 'test-subscription-' + uuid.v4();
}

function generateTopicName() {
return 'test-topic-' + uuid.v4();
}

describe('pubsub', function() {
var TOPIC_NAMES = [
generateTopicName(),
Expand All @@ -51,6 +43,53 @@ describe('pubsub', function() {
TOPICS[2].name
];

function generateSubName() {
return 'test-subscription-' + uuid.v4();
}

function generateTopicName() {
return 'test-topic-' + uuid.v4();
}

function publishPop(message, options, callback) {
if (!callback) {
callback = options;
options = {};
}

options = options || {};

var topic = pubsub.topic(generateTopicName());
var subscription = topic.subscription(generateSubName());

async.series([
topic.create.bind(topic),
subscription.create.bind(subscription),
function(callback) {
async.times(6, function(_, callback) {
topic.publish(message, options, callback);
}, callback);
}
], function(err) {
if (err) {
callback(err);
return;
}

subscription.pull({
returnImmediately: true,
maxResults: 1
}, function(err, messages) {
if (err) {
callback(err);
return;
}

callback(null, messages.pop());
});
});
}

before(function(done) {
// create all needed topics
async.each(TOPICS, function(topic, cb) {
Expand Down Expand Up @@ -128,15 +167,19 @@ describe('pubsub', function() {
});

it('should publish a message with attributes', function(done) {
var topic = pubsub.topic(TOPIC_NAMES[1]);
topic.publish({
var rawMessage = {
data: 'raw message data',
attributes: {
raw: true
customAttribute: 'value'
}
}, function(err, messageIds) {
};

publishPop(rawMessage, { raw: true }, function(err, message) {
assert.ifError(err);
assert.strictEqual(messageIds.length, 1);

assert.strictEqual(message.data, rawMessage.data);
assert.deepEqual(message.attributes, rawMessage.attributes);

done();
});
});
Expand Down
14 changes: 14 additions & 0 deletions packages/pubsub/test/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,20 @@ describe('Topic', function() {
}, { raw: true }, assert.ifError);
});

it('should clone the provided message', function(done) {
var message = {
data: 'data'
};
var originalMessage = extend({}, message);

topic.request = function() {
assert.deepEqual(message, originalMessage);
done();
};

topic.publish(message, { raw: true }, assert.ifError);
});

it('should execute callback', function(done) {
topic.request = function(protoOpts, reqOpts, callback) {
callback(null, {});
Expand Down

0 comments on commit fcd1097

Please sign in to comment.