-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
test_thread.py
323 lines (244 loc) · 10 KB
/
test_thread.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# 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.
import threading
import time
import mock
import google.api_core.exceptions
from google.auth import credentials
from google.cloud.pubsub_v1 import publisher
from google.cloud.pubsub_v1 import types
from google.cloud.pubsub_v1.publisher import exceptions
from google.cloud.pubsub_v1.publisher._batch.base import BatchStatus
from google.cloud.pubsub_v1.publisher._batch import thread
from google.cloud.pubsub_v1.publisher._batch.thread import Batch
def create_client():
creds = mock.Mock(spec=credentials.Credentials)
return publisher.Client(credentials=creds)
def create_batch(autocommit=False, **batch_settings):
"""Return a batch object suitable for testing.
Args:
autocommit (bool): Whether the batch should commit after
``max_latency`` seconds. By default, this is ``False``
for unit testing.
kwargs (dict): Arguments passed on to the
:class:``~.pubsub_v1.types.BatchSettings`` constructor.
Returns:
~.pubsub_v1.publisher.batch.thread.Batch: A batch object.
"""
client = create_client()
settings = types.BatchSettings(**batch_settings)
return Batch(client, "topic_name", settings, autocommit=autocommit)
def test_init():
"""Establish that a monitor thread is usually created on init."""
client = create_client()
# Do not actually create a thread, but do verify that one was created;
# it should be running the batch's "monitor" method (which commits the
# batch once time elapses).
with mock.patch.object(threading, "Thread", autospec=True) as Thread:
batch = Batch(client, "topic_name", types.BatchSettings())
Thread.assert_called_once_with(
name="Thread-MonitorBatchPublisher", target=batch.monitor
)
# New batches start able to accept messages by default.
assert batch.status == BatchStatus.ACCEPTING_MESSAGES
def test_init_infinite_latency():
batch = create_batch(max_latency=float("inf"))
assert batch._thread is None
@mock.patch.object(threading, "Lock")
def test_make_lock(Lock):
lock = Batch.make_lock()
assert lock is Lock.return_value
Lock.assert_called_once_with()
def test_client():
client = create_client()
settings = types.BatchSettings()
batch = Batch(client, "topic_name", settings, autocommit=False)
assert batch.client is client
def test_commit():
batch = create_batch()
with mock.patch.object(threading, "Thread", autospec=True) as Thread:
batch.commit()
# A thread should have been created to do the actual commit.
Thread.assert_called_once_with(
name="Thread-CommitBatchPublisher", target=batch._commit
)
Thread.return_value.start.assert_called_once_with()
# The batch's status needs to be something other than "accepting messages",
# since the commit started.
assert batch.status != BatchStatus.ACCEPTING_MESSAGES
assert batch.status == BatchStatus.STARTING
def test_commit_no_op():
batch = create_batch()
batch._status = BatchStatus.IN_PROGRESS
with mock.patch.object(threading, "Thread", autospec=True) as Thread:
batch.commit()
# Make sure a thread was not created.
Thread.assert_not_called()
# Check that batch status is unchanged.
assert batch.status == BatchStatus.IN_PROGRESS
def test_blocking__commit():
batch = create_batch()
futures = (
batch.publish({"data": b"This is my message."}),
batch.publish({"data": b"This is another message."}),
)
# Set up the underlying API publish method to return a PublishResponse.
publish_response = types.PublishResponse(message_ids=["a", "b"])
patch = mock.patch.object(
type(batch.client.api), "publish", return_value=publish_response
)
with patch as publish:
batch._commit()
# Establish that the underlying API call was made with expected
# arguments.
publish.assert_called_once_with(
"topic_name",
[
types.PubsubMessage(data=b"This is my message."),
types.PubsubMessage(data=b"This is another message."),
],
)
# Establish that all of the futures are done, and that they have the
# expected values.
assert futures[0].done()
assert futures[0].result() == "a"
assert futures[1].done()
assert futures[1].result() == "b"
@mock.patch.object(thread, "_LOGGER")
def test_blocking__commit_starting(_LOGGER):
batch = create_batch()
batch._status = BatchStatus.STARTING
batch._commit()
assert batch._status == BatchStatus.SUCCESS
_LOGGER.debug.assert_called_once_with("No messages to publish, exiting commit")
@mock.patch.object(thread, "_LOGGER")
def test_blocking__commit_already_started(_LOGGER):
batch = create_batch()
batch._status = BatchStatus.IN_PROGRESS
batch._commit()
assert batch._status == BatchStatus.IN_PROGRESS
_LOGGER.debug.assert_called_once_with(
"Batch is already in progress, exiting commit"
)
def test_blocking__commit_no_messages():
batch = create_batch()
with mock.patch.object(type(batch.client.api), "publish") as publish:
batch._commit()
assert publish.call_count == 0
def test_blocking__commit_wrong_messageid_length():
batch = create_batch()
futures = (
batch.publish({"data": b"blah blah blah"}),
batch.publish({"data": b"blah blah blah blah"}),
)
# Set up a PublishResponse that only returns one message ID.
publish_response = types.PublishResponse(message_ids=["a"])
patch = mock.patch.object(
type(batch.client.api), "publish", return_value=publish_response
)
with patch:
batch._commit()
for future in futures:
assert future.done()
assert isinstance(future.exception(), exceptions.PublishError)
def test_block__commmit_api_error():
batch = create_batch()
futures = (
batch.publish({"data": b"blah blah blah"}),
batch.publish({"data": b"blah blah blah blah"}),
)
# Make the API throw an error when publishing.
error = google.api_core.exceptions.InternalServerError("uh oh")
patch = mock.patch.object(type(batch.client.api), "publish", side_effect=error)
with patch:
batch._commit()
for future in futures:
assert future.done()
assert future.exception() == error
def test_monitor():
batch = create_batch(max_latency=5.0)
with mock.patch.object(time, "sleep") as sleep:
with mock.patch.object(type(batch), "_commit") as _commit:
batch.monitor()
# The monitor should have waited the given latency.
sleep.assert_called_once_with(5.0)
# Since `monitor` runs in its own thread, it should call
# the blocking commit implementation.
_commit.assert_called_once_with()
def test_monitor_already_committed():
batch = create_batch(max_latency=5.0)
status = "something else"
batch._status = status
with mock.patch.object(time, "sleep") as sleep:
batch.monitor()
# The monitor should have waited the given latency.
sleep.assert_called_once_with(5.0)
# The status should not have changed.
assert batch._status == status
def test_publish():
batch = create_batch()
messages = (
types.PubsubMessage(data=b"foobarbaz"),
types.PubsubMessage(data=b"spameggs"),
types.PubsubMessage(data=b"1335020400"),
)
# Publish each of the messages, which should save them to the batch.
futures = [batch.publish(message) for message in messages]
# There should be three messages on the batch, and three futures.
assert len(batch.messages) == 3
assert batch._futures == futures
# The size should have been incremented by the sum of the size of the
# messages.
expected_size = sum([message_pb.ByteSize() for message_pb in messages])
assert batch.size == expected_size
assert batch.size > 0 # I do not always trust protobuf.
def test_publish_not_will_accept():
batch = create_batch(max_messages=0)
# Publish the message.
message = types.PubsubMessage(data=b"foobarbaz")
future = batch.publish(message)
assert future is None
assert batch.size == 0
assert batch.messages == []
assert batch._futures == []
def test_publish_exceed_max_messages():
max_messages = 4
batch = create_batch(max_messages=max_messages)
messages = (
types.PubsubMessage(data=b"foobarbaz"),
types.PubsubMessage(data=b"spameggs"),
types.PubsubMessage(data=b"1335020400"),
)
# Publish each of the messages, which should save them to the batch.
with mock.patch.object(batch, "commit") as commit:
futures = [batch.publish(message) for message in messages]
assert batch._futures == futures
assert len(futures) == max_messages - 1
# Commit should not yet have been called.
assert commit.call_count == 0
# When a fourth message is published, commit should be called.
# No future will be returned in this case.
future = batch.publish(types.PubsubMessage(data=b"last one"))
commit.assert_called_once_with()
assert future is None
assert batch._futures == futures
def test_publish_dict():
batch = create_batch()
future = batch.publish({"data": b"foobarbaz", "attributes": {"spam": "eggs"}})
# There should be one message on the batch.
expected_message = types.PubsubMessage(
data=b"foobarbaz", attributes={"spam": "eggs"}
)
assert batch.messages == [expected_message]
assert batch._futures == [future]