forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.py
More file actions
168 lines (136 loc) · 5.32 KB
/
Copy pathsystem.py
File metadata and controls
168 lines (136 loc) · 5.32 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
# Copyright 2017, Google LLC 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.
from __future__ import absolute_import
import datetime
import time
import uuid
import mock
import six
from google import auth
from google.cloud import pubsub_v1
def _resource_name(resource_type):
"""Return a randomly selected name for a resource.
Args:
resource_type (str): The resource for which a name is being
generated. Should be singular (e.g. "topic", "subscription")
"""
return 'projects/{project}/{resource_type}s/st-n{random}'.format(
project=auth.default()[1],
random=str(uuid.uuid4())[0:8],
resource_type=resource_type,
)
def test_publish_messages():
publisher = pubsub_v1.PublisherClient()
topic_name = _resource_name('topic')
futures = []
try:
publisher.create_topic(topic_name)
for i in range(0, 500):
futures.append(
publisher.publish(
topic_name,
b'The hail in Wales falls mainly on the snails.',
num=str(i),
),
)
for future in futures:
result = future.result()
assert isinstance(result, (six.text_type, six.binary_type))
finally:
publisher.delete_topic(topic_name)
def test_subscribe_to_messages():
publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()
topic_name = _resource_name('topic')
sub_name = _resource_name('subscription')
try:
# Create a topic.
publisher.create_topic(topic_name)
# Subscribe to the topic. This must happen before the messages
# are published.
subscriber.create_subscription(sub_name, topic_name)
subscription = subscriber.subscribe(sub_name)
# Publish some messages.
futures = [publisher.publish(
topic_name,
b'Wooooo! The claaaaaw!',
num=str(i),
) for i in range(0, 50)]
# Make sure the publish completes.
[f.result() for f in futures]
# The callback should process the message numbers to prove
# that we got everything at least once.
callback = mock.Mock(wraps=lambda message: message.ack())
# Actually open the subscription and hold it open for a few seconds.
subscription.open(callback)
for second in range(0, 10):
time.sleep(1)
# The callback should have fired at least fifty times, but it
# may take some time.
if callback.call_count >= 50:
return
# Okay, we took too long; fail out.
assert callback.call_count >= 50
finally:
publisher.delete_topic(topic_name)
def test_subscribe_to_messages_async_callbacks():
publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()
topic_name = _resource_name('topic')
sub_name = _resource_name('subscription')
try:
# Create a topic.
publisher.create_topic(topic_name)
# Subscribe to the topic. This must happen before the messages
# are published.
subscriber.create_subscription(sub_name, topic_name)
subscription = subscriber.subscribe(sub_name)
# Publish some messages.
futures = [publisher.publish(
topic_name,
b'Wooooo! The claaaaaw!',
num=str(i),
) for i in range(0, 2)]
# Make sure the publish completes.
[f.result() for f in futures]
# We want to make sure that the callback was called asynchronously. So
# track when each call happened and make sure below.
call_times = []
def process_message(message):
# list.append() is thread-safe.
call_times.append(datetime.datetime.now())
time.sleep(2)
message.ack()
callback = mock.Mock(wraps=process_message)
side_effect = mock.Mock()
callback.side_effect = side_effect
# Actually open the subscription and hold it open for a few seconds.
subscription.open(callback)
for second in range(0, 5):
time.sleep(4)
# The callback should have fired at least two times, but it may
# take some time.
if callback.call_count >= 2 and side_effect.call_count >= 2:
first = min(call_times[:2])
last = max(call_times[:2])
diff = last - first
# "Ensure" the first two callbacks were executed asynchronously
# (sequentially would have resulted in a difference of 2+
# seconds).
assert diff.days == 0
assert diff.seconds < 2
# Okay, we took too long; fail out.
assert callback.call_count >= 2
finally:
publisher.delete_topic(topic_name)