forked from googleapis/google-cloud-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpubsub.js
More file actions
177 lines (155 loc) · 5.1 KB
/
pubsub.js
File metadata and controls
177 lines (155 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* 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.
*/
/*global describe, it, before */
'use strict';
var assert = require('assert');
var async = require('async');
var env = require('./env.js');
var gcloud = require('../lib');
var Topic = require('../lib/pubsub/topic.js');
var Subscription = require('../lib/pubsub/subscription.js');
describe('PubSub', function() {
var pubsub;
before(function(done) {
pubsub = new gcloud.PubSub(env);
var topicNames = ['topic1', 'topic2', 'topic3'];
// TODO: Handle pagination.
pubsub.getTopics(function(err, topics) {
assert.ifError(err);
async.series(
// delete existing topics.
topics.map(function(topic) {
return topic.delete.bind(topic);
}),
function(err) {
assert.ifError(err);
// create new topics.
async.map(topicNames, function(topic, callback) {
pubsub.topic(topic).on('ready', callback);
}, done);
});
});
});
describe('Topic', function() {
it('should return Topic instances for existing topics', function(done) {
pubsub.getTopics(function(err, topics) {
assert(topics.length, 3);
topics.forEach(function(topic) {
assert(topic instanceof Topic);
});
done(err);
});
});
it('should return a nextQuery if there are more results', function(done) {
pubsub.getTopics({ maxResults: 2 }, function(err, topics, next) {
assert.ifError(err);
assert.equal(topics.length, 2);
assert.equal(next.maxResults, 2);
assert.equal(!!next.pageToken, true);
done();
});
});
it('should be created', function(done) {
var topic = pubsub.topic('topic-new');
topic.on('error', assert.ifError);
topic.on('ready', done);
});
it('should be gettable', function(done) {
var topic = pubsub.topic('topic1');
topic.on('error', assert.ifError);
topic.on('ready', done);
});
it('should publish a message', function(done) {
pubsub.topic('topic1').publish('message from me', function(err) {
assert.ifError(err);
done();
});
});
it('should be deleted', function(done) {
pubsub.topic('topic1').delete(function (err) {
assert.ifError(err);
done();
});
});
});
describe('Subscription', function() {
var subscriptionObjects = [
{ name: 'sub1', ackDeadlineSeconds: 30, autoPull: false },
{ name: 'sub2', ackDeadlineSeconds: 60, autoPull: false }
];
var topic;
before(function(done) {
topic = pubsub.topic('topic1');
topic.on('error', assert.ifError);
pubsub.getSubscriptions(function(err, subscriptions) {
assert.ifError(err);
async.series(
// delete existing subscriptions.
subscriptions.map(function(subscription) {
return subscription.delete.bind(subscription);
}),
function(err) {
assert.ifError(err);
// re-subscribe.
async.map(subscriptionObjects, function(subscriptionObj, callback) {
topic.subscribe(subscriptionObj).on('ready', callback);
}, done);
});
});
});
it('should return Subscription instances for existing subscriptions',
function(done) {
topic.getSubscriptions(function(err, subscriptions) {
assert.ifError(err);
assert.strictEqual(subscriptions.length, subscriptionObjects.length);
subscriptions.forEach(function(subscription) {
assert(subscription instanceof Subscription);
});
done();
});
});
it('should be gettable', function(done) {
var sub = topic.subscribe(subscriptionObjects[0]);
sub.on('error', assert.ifError);
sub.on('ready', function() {
assert.equal(sub.name, '/subscriptions/' + env.projectId + '/sub1');
sub.close();
done();
});
});
it('should create a subscription', function(done) {
var newSub = topic.subscribe('new-sub');
newSub.on('error', assert.ifError);
newSub.on('ready', function() {
newSub.close();
done();
});
});
it('should be able to pull and ack', function(done) {
topic.publish('hello');
var sub = topic.subscribe(subscriptionObjects[0].name);
sub.on('error', assert.ifError);
sub.on('message', function(msg) {
sub.ack(msg.ackId, function(err) {
assert.ifError(err);
sub.close();
done();
});
});
sub.pull();
});
});
});