-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Expand file tree
/
Copy pathsubscriber_test.py
More file actions
270 lines (193 loc) · 7.79 KB
/
subscriber_test.py
File metadata and controls
270 lines (193 loc) · 7.79 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# Copyright 2016 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.
import os
import time
import uuid
from gcp_devrel.testing import eventually_consistent
from google.cloud import pubsub_v1
import mock
import pytest
import subscriber
UUID = uuid.uuid4().hex
PROJECT = os.environ['GCLOUD_PROJECT']
TOPIC = 'subscription-test-topic-' + UUID
SUBSCRIPTION_ONE = 'subscription-test-subscription-one-' + UUID
SUBSCRIPTION_TWO = 'subscription-test-subscription-two-' + UUID
SUBSCRIPTION_THREE = 'subscription-test-subscription-three-' + UUID
ENDPOINT = 'https://{}.appspot.com/push'.format(PROJECT)
NEW_ENDPOINT = 'https://{}.appspot.com/push2'.format(PROJECT)
@pytest.fixture(scope='module')
def publisher_client():
yield pubsub_v1.PublisherClient()
@pytest.fixture(scope='module')
def topic(publisher_client):
topic_path = publisher_client.topic_path(PROJECT, TOPIC)
try:
response = publisher_client.get_topic(topic_path)
except: # noqa
response = publisher_client.create_topic(topic_path)
yield response.name
@pytest.fixture(scope='module')
def subscriber_client():
yield pubsub_v1.SubscriberClient()
@pytest.fixture(scope='module')
def subscription_one(subscriber_client, topic):
subscription_path = subscriber_client.subscription_path(
PROJECT, SUBSCRIPTION_ONE)
try:
response = subscriber_client.get_subscription(subscription_path)
except: # noqa
response = subscriber_client.create_subscription(
subscription_path, topic=topic)
yield response.name
@pytest.fixture(scope='module')
def subscription_two(subscriber_client, topic):
subscription_path = subscriber_client.subscription_path(
PROJECT, SUBSCRIPTION_TWO)
try:
response = subscriber_client.get_subscription(subscription_path)
except: # noqa
response = subscriber_client.create_subscription(
subscription_path, topic=topic)
yield response.name
@pytest.fixture(scope='module')
def subscription_three(subscriber_client, topic):
subscription_path = subscriber_client.subscription_path(
PROJECT, SUBSCRIPTION_THREE)
try:
response = subscriber_client.get_subscription(subscription_path)
except: # noqa
response = subscriber_client.create_subscription(
subscription_path, topic=topic)
yield response.name
def test_list_in_topic(subscription_one, capsys):
@eventually_consistent.call
def _():
subscriber.list_subscriptions_in_topic(PROJECT, TOPIC)
out, _ = capsys.readouterr()
assert subscription_one in out
def test_list_in_project(subscription_one, capsys):
@eventually_consistent.call
def _():
subscriber.list_subscriptions_in_project(PROJECT)
out, _ = capsys.readouterr()
assert subscription_one in out
def test_create(subscriber_client):
subscription_path = subscriber_client.subscription_path(
PROJECT, SUBSCRIPTION_ONE)
try:
subscriber_client.delete_subscription(subscription_path)
except Exception:
pass
subscriber.create_subscription(PROJECT, TOPIC, SUBSCRIPTION_ONE)
@eventually_consistent.call
def _():
assert subscriber_client.get_subscription(subscription_path)
def test_create_push(subscriber_client):
subscription_path = subscriber_client.subscription_path(
PROJECT, SUBSCRIPTION_ONE)
try:
subscriber_client.delete_subscription(subscription_path)
except Exception:
pass
subscriber.create_push_subscription(
PROJECT, TOPIC, SUBSCRIPTION_ONE, ENDPOINT)
@eventually_consistent.call
def _():
assert subscriber_client.get_subscription(subscription_path)
def test_update(subscriber_client, subscription_one, capsys):
subscriber.update_subscription(PROJECT, SUBSCRIPTION_ONE, NEW_ENDPOINT)
out, _ = capsys.readouterr()
assert 'Subscription updated' in out
def test_delete(subscriber_client, subscription_one):
subscriber.delete_subscription(PROJECT, SUBSCRIPTION_ONE)
@eventually_consistent.call
def _():
with pytest.raises(Exception):
subscriber_client.get_subscription(subscription_one)
def _publish_messages(publisher_client, topic):
for n in range(5):
data = u'Message {}'.format(n).encode('utf-8')
future = publisher_client.publish(topic, data=data)
future.result()
def _publish_messages_with_custom_attributes(publisher_client, topic):
data = u'Test message'.encode('utf-8')
future = publisher_client.publish(topic, data=data, origin='python-sample')
future.result()
def _make_sleep_patch():
real_sleep = time.sleep
def new_sleep(period):
if period == 60:
real_sleep(5)
raise RuntimeError('sigil')
else:
real_sleep(period)
return mock.patch('time.sleep', new=new_sleep)
def _to_delete():
publisher_client = pubsub_v1.PublisherClient()
subscriber_client = pubsub_v1.SubscriberClient()
resources = [TOPIC, SUBSCRIPTION_TWO, SUBSCRIPTION_THREE]
for item in resources:
if 'subscription-test-topic' in item:
publisher_client.delete_topic(
'projects/{}/topics/{}'.format(PROJECT, item))
if 'subscription-test-subscription' in item:
subscriber_client.delete_subscription(
'projects/{}/subscriptions/{}'.format(PROJECT, item))
def test_receive(publisher_client, topic, subscription_two, capsys):
_publish_messages(publisher_client, topic)
with _make_sleep_patch():
with pytest.raises(RuntimeError, match='sigil'):
subscriber.receive_messages(PROJECT, SUBSCRIPTION_TWO)
out, _ = capsys.readouterr()
assert 'Listening' in out
assert subscription_two in out
assert 'Message' in out
def test_receive_with_custom_attributes(
publisher_client, topic, subscription_two, capsys):
_publish_messages_with_custom_attributes(publisher_client, topic)
with _make_sleep_patch():
with pytest.raises(RuntimeError, match='sigil'):
subscriber.receive_messages_with_custom_attributes(
PROJECT, SUBSCRIPTION_TWO)
out, _ = capsys.readouterr()
assert 'Test message' in out
assert 'origin' in out
assert 'python-sample' in out
def test_receive_with_flow_control(
publisher_client, topic, subscription_two, capsys):
_publish_messages(publisher_client, topic)
with _make_sleep_patch():
with pytest.raises(RuntimeError, match='sigil'):
subscriber.receive_messages_with_flow_control(
PROJECT, SUBSCRIPTION_TWO)
out, _ = capsys.readouterr()
assert 'Listening' in out
assert subscription_two in out
assert 'Message' in out
def test_receive_synchronously(
publisher_client, topic, subscription_three, capsys):
_publish_messages(publisher_client, topic)
subscriber.synchronous_pull(PROJECT, SUBSCRIPTION_THREE)
out, _ = capsys.readouterr()
assert 'Done.' in out
def test_receive_synchronously_with_lease(
publisher_client, topic, subscription_three, capsys):
_publish_messages(publisher_client, topic)
subscriber.synchronous_pull_with_lease_management(
PROJECT, SUBSCRIPTION_THREE)
out, _ = capsys.readouterr()
assert 'Done.' in out
# Clean up resources after all the tests.
_to_delete()