-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
subscriber_test.py
264 lines (189 loc) · 7.11 KB
/
subscriber_test.py
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
# 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 uuid
from gcp_devrel.testing import eventually_consistent
from google.cloud import pubsub_v1
import pytest
import subscriber
UUID = uuid.uuid4().hex
PROJECT = os.environ["GCLOUD_PROJECT"]
TOPIC = "subscription-test-topic-" + UUID
SUBSCRIPTION_ADMIN = "subscription-test-subscription-admin-" + UUID
SUBSCRIPTION_ASYNC = "subscription-test-subscription-async-" + UUID
SUBSCRIPTION_SYNC = "subscription-test-subscription-sync-" + 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:
subscription = publisher_client.get_topic(topic_path)
except: # noqa
subscription = publisher_client.create_topic(topic_path)
yield subscription.name
publisher_client.delete_topic(subscription.name)
@pytest.fixture(scope="module")
def subscriber_client():
yield pubsub_v1.SubscriberClient()
@pytest.fixture(scope="module")
def subscription_admin(subscriber_client, topic):
subscription_path = subscriber_client.subscription_path(
PROJECT, SUBSCRIPTION_ADMIN
)
try:
subscription = subscriber_client.get_subscription(subscription_path)
except: # noqa
subscription = subscriber_client.create_subscription(
subscription_path, topic=topic
)
yield subscription.name
@pytest.fixture(scope="module")
def subscription_sync(subscriber_client, topic):
subscription_path = subscriber_client.subscription_path(
PROJECT, SUBSCRIPTION_SYNC
)
try:
subscription = subscriber_client.get_subscription(subscription_path)
except: # noqa
subscription = subscriber_client.create_subscription(
subscription_path, topic=topic
)
yield subscription.name
subscriber_client.delete_subscription(subscription.name)
@pytest.fixture(scope="module")
def subscription_async(subscriber_client, topic):
subscription_path = subscriber_client.subscription_path(
PROJECT, SUBSCRIPTION_ASYNC
)
try:
subscription = subscriber_client.get_subscription(subscription_path)
except: # noqa
subscription = subscriber_client.create_subscription(
subscription_path, topic=topic
)
yield subscription.name
subscriber_client.delete_subscription(subscription.name)
def test_list_in_topic(subscription_admin, capsys):
@eventually_consistent.call
def _():
subscriber.list_subscriptions_in_topic(PROJECT, TOPIC)
out, _ = capsys.readouterr()
assert subscription_admin in out
def test_list_in_project(subscription_admin, capsys):
@eventually_consistent.call
def _():
subscriber.list_subscriptions_in_project(PROJECT)
out, _ = capsys.readouterr()
assert subscription_admin in out
def test_create(subscriber_client):
subscription_path = subscriber_client.subscription_path(
PROJECT, SUBSCRIPTION_ADMIN
)
try:
subscriber_client.delete_subscription(subscription_path)
except Exception:
pass
subscriber.create_subscription(PROJECT, TOPIC, SUBSCRIPTION_ADMIN)
@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_ADMIN
)
try:
subscriber_client.delete_subscription(subscription_path)
except Exception:
pass
subscriber.create_push_subscription(
PROJECT, TOPIC, SUBSCRIPTION_ADMIN, ENDPOINT
)
@eventually_consistent.call
def _():
assert subscriber_client.get_subscription(subscription_path)
def test_update(subscriber_client, subscription_admin, capsys):
subscriber.update_subscription(PROJECT, SUBSCRIPTION_ADMIN, NEW_ENDPOINT)
out, _ = capsys.readouterr()
assert "Subscription updated" in out
def test_delete(subscriber_client, subscription_admin):
subscriber.delete_subscription(PROJECT, SUBSCRIPTION_ADMIN)
@eventually_consistent.call
def _():
with pytest.raises(Exception):
subscriber_client.get_subscription(subscription_admin)
def _publish_messages(publisher_client, topic):
for n in range(5):
data = u"message {}".format(n).encode("utf-8")
publish_future = publisher_client.publish(
topic, data=data, origin="python-sample"
)
publish_future.result()
def test_receive(publisher_client, topic, subscription_async, capsys):
_publish_messages(publisher_client, topic)
subscriber.receive_messages(PROJECT, SUBSCRIPTION_ASYNC, 5)
out, _ = capsys.readouterr()
assert "Listening" in out
assert subscription_async in out
assert "message" in out
def test_receive_with_custom_attributes(
publisher_client, topic, subscription_async, capsys
):
_publish_messages(publisher_client, topic)
subscriber.receive_messages_with_custom_attributes(
PROJECT, SUBSCRIPTION_ASYNC, 5
)
out, _ = capsys.readouterr()
assert "message" in out
assert "origin" in out
assert "python-sample" in out
def test_receive_with_flow_control(
publisher_client, topic, subscription_async, capsys
):
_publish_messages(publisher_client, topic)
subscriber.receive_messages_with_flow_control(
PROJECT, SUBSCRIPTION_ASYNC, 5
)
out, _ = capsys.readouterr()
assert "Listening" in out
assert subscription_async in out
assert "message" in out
def test_receive_synchronously(
publisher_client, topic, subscription_sync, capsys
):
_publish_messages(publisher_client, topic)
subscriber.synchronous_pull(PROJECT, SUBSCRIPTION_SYNC)
out, _ = capsys.readouterr()
assert "Done." in out
def test_receive_synchronously_with_lease(
publisher_client, topic, subscription_sync, capsys
):
_publish_messages(publisher_client, topic)
subscriber.synchronous_pull_with_lease_management(
PROJECT, SUBSCRIPTION_SYNC
)
out, _ = capsys.readouterr()
assert "Done." in out
def test_listen_for_errors(
publisher_client, topic, subscription_async, capsys
):
_publish_messages(publisher_client, topic)
subscriber.listen_for_errors(PROJECT, SUBSCRIPTION_ASYNC, 5)
out, _ = capsys.readouterr()
assert "Listening" in out
assert subscription_async in out
assert "threw an exception" in out