-
Notifications
You must be signed in to change notification settings - Fork 196
/
test_streaming_pull_manager.py
2572 lines (2092 loc) · 86.9 KB
/
test_streaming_pull_manager.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2018, Google LLC
#
# 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
#
# https://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 functools
import logging
import sys
import threading
import time
import types as stdlib_types
# special case python < 3.8
if sys.version_info.major == 3 and sys.version_info.minor < 8:
import mock
else:
from unittest import mock
import pytest
from google.api_core import bidi
from google.api_core import exceptions
from google.cloud.pubsub_v1 import types
from google.cloud.pubsub_v1.subscriber import client
from google.cloud.pubsub_v1.subscriber import message
from google.cloud.pubsub_v1.subscriber import scheduler
from google.cloud.pubsub_v1.subscriber._protocol import dispatcher
from google.cloud.pubsub_v1.subscriber._protocol import heartbeater
from google.cloud.pubsub_v1.subscriber._protocol import leaser
from google.cloud.pubsub_v1.subscriber._protocol import messages_on_hold
from google.cloud.pubsub_v1.subscriber._protocol import requests
from google.cloud.pubsub_v1.subscriber._protocol import streaming_pull_manager
from google.cloud.pubsub_v1.subscriber import exceptions as subscriber_exceptions
from google.cloud.pubsub_v1.subscriber import futures
from google.pubsub_v1 import types as gapic_types
import grpc
from google.rpc import status_pb2
from google.rpc import code_pb2
from google.rpc import error_details_pb2
@pytest.mark.parametrize(
"exception,expected_cls",
[
(ValueError("meep"), ValueError),
(
mock.create_autospec(grpc.RpcError, instance=True),
exceptions.GoogleAPICallError,
),
({"error": "RPC terminated"}, Exception),
("something broke", Exception),
],
)
def test__wrap_as_exception(exception, expected_cls):
assert isinstance(
streaming_pull_manager._wrap_as_exception(exception), expected_cls
)
def test__wrap_callback_errors_no_error():
msg = mock.create_autospec(message.Message, instance=True)
callback = mock.Mock()
on_callback_error = mock.Mock()
streaming_pull_manager._wrap_callback_errors(callback, on_callback_error, msg)
callback.assert_called_once_with(msg)
msg.nack.assert_not_called()
on_callback_error.assert_not_called()
@pytest.mark.parametrize(
"callback_error",
[
(ValueError("ValueError")),
(BaseException("BaseException")),
],
)
def test__wrap_callback_errors_error(callback_error):
msg = mock.create_autospec(message.Message, instance=True)
callback = mock.Mock(side_effect=callback_error)
on_callback_error = mock.Mock()
streaming_pull_manager._wrap_callback_errors(callback, on_callback_error, msg)
msg.nack.assert_called_once()
on_callback_error.assert_called_once_with(callback_error)
def test_constructor_and_default_state():
mock.sentinel.subscription = str()
manager = streaming_pull_manager.StreamingPullManager(
mock.sentinel.client, mock.sentinel.subscription
)
# Public state
assert manager.is_active is False
assert manager.flow_control == types.FlowControl()
assert manager.dispatcher is None
assert manager.leaser is None
assert manager.ack_histogram is not None
assert manager.ack_deadline == 10
assert manager.load == 0
# Private state
assert manager._client == mock.sentinel.client
assert manager._subscription == mock.sentinel.subscription
assert manager._scheduler is not None
assert manager._messages_on_hold is not None
assert manager._client_id is not None
def test_constructor_with_default_options():
mock.sentinel.subscription = str()
flow_control_ = types.FlowControl()
manager = streaming_pull_manager.StreamingPullManager(
mock.sentinel.client,
mock.sentinel.subscription,
flow_control=flow_control_,
scheduler=mock.sentinel.scheduler,
)
assert manager.flow_control == flow_control_
assert manager._scheduler == mock.sentinel.scheduler
assert manager._ack_deadline == 10
assert manager._stream_ack_deadline == 60
def test_constructor_with_min_and_max_duration_per_lease_extension_():
mock.sentinel.subscription = str()
flow_control_ = types.FlowControl(
min_duration_per_lease_extension=15, max_duration_per_lease_extension=20
)
manager = streaming_pull_manager.StreamingPullManager(
mock.sentinel.client,
mock.sentinel.subscription,
flow_control=flow_control_,
scheduler=mock.sentinel.scheduler,
)
assert manager._ack_deadline == 15
assert manager._stream_ack_deadline == 20
def test_constructor_with_min_duration_per_lease_extension_too_low():
mock.sentinel.subscription = str()
flow_control_ = types.FlowControl(
min_duration_per_lease_extension=9, max_duration_per_lease_extension=9
)
manager = streaming_pull_manager.StreamingPullManager(
mock.sentinel.client,
mock.sentinel.subscription,
flow_control=flow_control_,
scheduler=mock.sentinel.scheduler,
)
assert manager._ack_deadline == 10
assert manager._stream_ack_deadline == 10
def test_constructor_with_max_duration_per_lease_extension_too_high():
mock.sentinel.subscription = str()
flow_control_ = types.FlowControl(
max_duration_per_lease_extension=601, min_duration_per_lease_extension=601
)
manager = streaming_pull_manager.StreamingPullManager(
mock.sentinel.client,
mock.sentinel.subscription,
flow_control=flow_control_,
scheduler=mock.sentinel.scheduler,
)
assert manager._ack_deadline == 600
assert manager._stream_ack_deadline == 600
def make_manager(**kwargs):
client_ = mock.create_autospec(client.Client, instance=True)
scheduler_ = mock.create_autospec(scheduler.Scheduler, instance=True)
return streaming_pull_manager.StreamingPullManager(
client_, "subscription-name", scheduler=scheduler_, **kwargs
)
def complete_modify_ack_deadline_calls(dispatcher):
def complete_futures(*args, **kwargs):
modack_requests = args[0]
for req in modack_requests:
if req.future:
req.future.set_result(subscriber_exceptions.AcknowledgeStatus.SUCCESS)
dispatcher.modify_ack_deadline.side_effect = complete_futures
def fake_leaser_add(leaser, init_msg_count=0, assumed_msg_size=10):
"""Add a simplified fake add() method to a leaser instance.
The fake add() method actually increases the leaser's internal message count
by one for each message, and the total bytes by ``assumed_msg_size`` for
each message (regardless of the actual message size).
"""
def fake_add(self, items):
self.message_count += len(items)
self.bytes += len(items) * assumed_msg_size
leaser.message_count = init_msg_count
leaser.bytes = init_msg_count * assumed_msg_size
leaser.add = stdlib_types.MethodType(fake_add, leaser)
def test__obtain_ack_deadline_no_custom_flow_control_setting():
from google.cloud.pubsub_v1.subscriber._protocol import histogram
manager = make_manager()
# Make sure that min_duration_per_lease_extension and
# max_duration_per_lease_extension is disabled.
manager._flow_control = types.FlowControl(
min_duration_per_lease_extension=0, max_duration_per_lease_extension=0
)
assert manager._stream_ack_deadline == 60
assert manager._ack_deadline == 10
assert manager._obtain_ack_deadline(maybe_update=False) == 10
deadline = manager._obtain_ack_deadline(maybe_update=True)
assert deadline == histogram.MIN_ACK_DEADLINE
assert manager._stream_ack_deadline == 60
# When we get some historical data, the deadline is adjusted.
manager.ack_histogram.add(histogram.MIN_ACK_DEADLINE * 2)
deadline = manager._obtain_ack_deadline(maybe_update=True)
assert deadline == histogram.MIN_ACK_DEADLINE * 2
# Adding just a single additional data point does not yet change the deadline.
manager.ack_histogram.add(histogram.MIN_ACK_DEADLINE)
deadline = manager._obtain_ack_deadline(maybe_update=True)
assert deadline == histogram.MIN_ACK_DEADLINE * 2
def test__obtain_ack_deadline_with_max_duration_per_lease_extension():
from google.cloud.pubsub_v1.subscriber._protocol import histogram
manager = make_manager()
manager._flow_control = types.FlowControl(
max_duration_per_lease_extension=histogram.MIN_ACK_DEADLINE + 1
)
assert manager._ack_deadline == 10
manager.ack_histogram.add(histogram.MIN_ACK_DEADLINE * 3) # make p99 value large
# The deadline configured in flow control should prevail.
deadline = manager._obtain_ack_deadline(maybe_update=True)
assert deadline == histogram.MIN_ACK_DEADLINE + 1
assert manager._stream_ack_deadline == 60
def test__obtain_ack_deadline_with_min_duration_per_lease_extension():
from google.cloud.pubsub_v1.subscriber._protocol import histogram
manager = make_manager()
manager._flow_control = types.FlowControl(
min_duration_per_lease_extension=histogram.MAX_ACK_DEADLINE
)
manager.ack_histogram.add(histogram.MIN_ACK_DEADLINE) # make p99 value small
# The deadline configured in flow control should prevail.
deadline = manager._obtain_ack_deadline(maybe_update=True)
assert deadline == histogram.MAX_ACK_DEADLINE
assert manager._stream_ack_deadline == histogram.MAX_ACK_DEADLINE
def test__obtain_ack_deadline_with_max_duration_per_lease_extension_too_low():
from google.cloud.pubsub_v1.subscriber._protocol import histogram
manager = make_manager()
manager._flow_control = types.FlowControl(
max_duration_per_lease_extension=histogram.MIN_ACK_DEADLINE - 1
)
# The deadline configured in flow control should be adjusted to the minimum allowed.
deadline = manager._obtain_ack_deadline(maybe_update=True)
assert deadline == histogram.MIN_ACK_DEADLINE
def test__obtain_ack_deadline_with_min_duration_per_lease_extension_too_high():
from google.cloud.pubsub_v1.subscriber._protocol import histogram
manager = make_manager()
manager._flow_control = types.FlowControl(
min_duration_per_lease_extension=histogram.MAX_ACK_DEADLINE + 1
)
# The deadline configured in flow control should be adjusted to the maximum allowed.
deadline = manager._obtain_ack_deadline(maybe_update=True)
assert deadline == histogram.MAX_ACK_DEADLINE
assert manager._stream_ack_deadline == histogram.MAX_ACK_DEADLINE
def test__obtain_ack_deadline_with_exactly_once_enabled():
manager = make_manager()
manager._flow_control = types.FlowControl(
min_duration_per_lease_extension=0 # leave as default value
)
manager._exactly_once_enabled = True
manager.ack_histogram.add(
10
) # reduce p99 value below 60s min for exactly_once subscriptions
deadline = manager._obtain_ack_deadline(maybe_update=True)
# Since the 60-second min ack_deadline value for exactly_once subscriptions
# seconds is higher than the histogram value, the deadline should be 60 sec.
assert deadline == 60
assert manager._stream_ack_deadline == 60
def test__obtain_ack_deadline_with_min_duration_per_lease_extension_with_exactly_once_enabled():
from google.cloud.pubsub_v1.subscriber._protocol import histogram
manager = make_manager()
manager._flow_control = types.FlowControl(
min_duration_per_lease_extension=histogram.MAX_ACK_DEADLINE
)
manager._exactly_once_enabled = True
manager.ack_histogram.add(histogram.MIN_ACK_DEADLINE) # make p99 value small
# The deadline configured in flow control should prevail.
deadline = manager._obtain_ack_deadline(maybe_update=True)
# User-defined custom min ack_deadline value takes precedence over
# exactly_once default of 60 seconds.
assert deadline == histogram.MAX_ACK_DEADLINE
assert manager._stream_ack_deadline == histogram.MAX_ACK_DEADLINE
def test__obtain_ack_deadline_no_value_update():
manager = make_manager()
# Make sure that max_duration_per_lease_extension is disabled.
manager._flow_control = types.FlowControl(max_duration_per_lease_extension=0)
manager.ack_histogram.add(21)
deadline = manager._obtain_ack_deadline(maybe_update=True)
assert deadline == 21
for _ in range(5):
manager.ack_histogram.add(35) # Gather some new ACK data.
deadline = manager._obtain_ack_deadline(maybe_update=False)
assert deadline == 21 # still the same
# Accessing the value through the ack_deadline property has no side effects either.
assert manager.ack_deadline == 21
# Updating the ack deadline is reflected on ack_deadline wrapper, too.
deadline = manager._obtain_ack_deadline(maybe_update=True)
assert manager.ack_deadline == deadline == 35
def test_client_id():
manager1 = make_manager()
request1 = manager1._get_initial_request(stream_ack_deadline_seconds=60)
client_id_1 = request1.client_id
assert client_id_1
manager2 = make_manager()
request2 = manager2._get_initial_request(stream_ack_deadline_seconds=60)
client_id_2 = request2.client_id
assert client_id_2
assert client_id_1 != client_id_2
def test_streaming_flow_control():
manager = make_manager(
flow_control=types.FlowControl(max_messages=10, max_bytes=1000)
)
request = manager._get_initial_request(stream_ack_deadline_seconds=60)
assert request.max_outstanding_messages == 10
assert request.max_outstanding_bytes == 1000
def test_streaming_flow_control_use_legacy_flow_control():
manager = make_manager(
flow_control=types.FlowControl(max_messages=10, max_bytes=1000),
use_legacy_flow_control=True,
)
request = manager._get_initial_request(stream_ack_deadline_seconds=60)
assert request.max_outstanding_messages == 0
assert request.max_outstanding_bytes == 0
def test_maybe_pause_consumer_wo_consumer_set():
manager = make_manager(
flow_control=types.FlowControl(max_messages=10, max_bytes=1000)
)
manager.maybe_pause_consumer() # no raise
# Ensure load > 1
_leaser = manager._leaser = mock.create_autospec(leaser.Leaser)
_leaser.message_count = 100
_leaser.bytes = 10000
manager.maybe_pause_consumer() # no raise
def test_lease_load_and_pause():
manager = make_manager(
flow_control=types.FlowControl(max_messages=10, max_bytes=1000)
)
manager._leaser = leaser.Leaser(manager)
manager._consumer = mock.create_autospec(bidi.BackgroundConsumer, instance=True)
manager._consumer.is_paused = False
# This should mean that our messages count is at 10%, and our bytes
# are at 15%; load should return the higher (0.15), and shouldn't cause
# the consumer to pause.
manager.leaser.add(
[requests.LeaseRequest(ack_id="one", byte_size=150, ordering_key="")]
)
assert manager.load == 0.15
manager.maybe_pause_consumer()
manager._consumer.pause.assert_not_called()
# After this message is added, the messages should be higher at 20%
# (versus 16% for bytes).
manager.leaser.add(
[requests.LeaseRequest(ack_id="two", byte_size=10, ordering_key="")]
)
assert manager.load == 0.2
# Returning a number above 100% is fine, and it should cause this to pause.
manager.leaser.add(
[requests.LeaseRequest(ack_id="three", byte_size=1000, ordering_key="")]
)
assert manager.load == 1.16
manager.maybe_pause_consumer()
manager._consumer.pause.assert_called_once()
def test_drop_and_resume():
manager = make_manager(
flow_control=types.FlowControl(max_messages=10, max_bytes=1000)
)
manager._leaser = leaser.Leaser(manager)
manager._consumer = mock.create_autospec(bidi.BackgroundConsumer, instance=True)
manager._consumer.is_paused = True
# Add several messages until we're over the load threshold.
manager.leaser.add(
[
requests.LeaseRequest(ack_id="one", byte_size=750, ordering_key=""),
requests.LeaseRequest(ack_id="two", byte_size=250, ordering_key=""),
]
)
assert manager.load == 1.0
# Trying to resume now should have no effect as we're over the threshold.
manager.maybe_resume_consumer()
manager._consumer.resume.assert_not_called()
# Drop the 200 byte message, which should put us under the resume
# threshold.
manager.leaser.remove(
[requests.DropRequest(ack_id="two", byte_size=250, ordering_key="")]
)
manager.maybe_resume_consumer()
manager._consumer.resume.assert_called_once()
def test_resume_not_paused():
manager = make_manager()
manager._consumer = mock.create_autospec(bidi.BackgroundConsumer, instance=True)
manager._consumer.is_paused = False
# Resuming should have no effect is the consumer is not actually paused.
manager.maybe_resume_consumer()
manager._consumer.resume.assert_not_called()
def test_maybe_resume_consumer_wo_consumer_set():
manager = make_manager(
flow_control=types.FlowControl(max_messages=10, max_bytes=1000)
)
manager.maybe_resume_consumer() # no raise
def test__maybe_release_messages_on_overload():
manager = make_manager(
flow_control=types.FlowControl(max_messages=10, max_bytes=1000)
)
msg = mock.create_autospec(message.Message, instance=True, ack_id="ack", size=11)
manager._messages_on_hold.put(msg)
manager._on_hold_bytes = msg.size
# Ensure load is exactly 1.0 (to verify that >= condition is used)
_leaser = manager._leaser = mock.create_autospec(leaser.Leaser)
_leaser.message_count = 10
_leaser.bytes = 1000 + msg.size
manager._maybe_release_messages()
assert manager._messages_on_hold.size == 1
manager._leaser.add.assert_not_called()
manager._scheduler.schedule.assert_not_called()
def test__maybe_release_messages_below_overload():
manager = make_manager(
flow_control=types.FlowControl(max_messages=10, max_bytes=1000)
)
manager._callback = mock.sentinel.callback
# Init leaser message count to 11, so that when subtracting the 3 messages
# that are on hold, there is still room for another 2 messages before the
# max load is hit.
_leaser = manager._leaser = mock.create_autospec(leaser.Leaser)
fake_leaser_add(_leaser, init_msg_count=11, assumed_msg_size=10)
messages = [
mock.create_autospec(message.Message, instance=True, ack_id="ack_foo", size=10),
mock.create_autospec(message.Message, instance=True, ack_id="ack_bar", size=10),
mock.create_autospec(message.Message, instance=True, ack_id="ack_baz", size=10),
]
for msg in messages:
manager._messages_on_hold.put(msg)
manager._on_hold_bytes = 3 * 10
# the actual call of MUT
manager._maybe_release_messages()
assert manager._messages_on_hold.size == 1
msg = manager._messages_on_hold.get()
assert msg.ack_id == "ack_baz"
schedule_calls = manager._scheduler.schedule.mock_calls
assert len(schedule_calls) == 2
for _, call_args, _ in schedule_calls:
assert call_args[0] == mock.sentinel.callback
assert isinstance(call_args[1], message.Message)
assert call_args[1].ack_id in ("ack_foo", "ack_bar")
def test__maybe_release_messages_negative_on_hold_bytes_warning(caplog):
manager = make_manager(
flow_control=types.FlowControl(max_messages=10, max_bytes=1000)
)
manager._callback = lambda msg: msg # pragma: NO COVER
msg = mock.create_autospec(message.Message, instance=True, ack_id="ack", size=17)
manager._messages_on_hold.put(msg)
manager._on_hold_bytes = 5 # too low for some reason
_leaser = manager._leaser = mock.create_autospec(leaser.Leaser)
_leaser.message_count = 3
_leaser.bytes = 150
with caplog.at_level(logging.WARNING):
manager._maybe_release_messages()
expected_warnings = [
record.message.lower()
for record in caplog.records
if "unexpectedly negative" in record.message
]
assert len(expected_warnings) == 1
assert "on hold bytes" in expected_warnings[0]
assert "-12" in expected_warnings[0]
assert manager._on_hold_bytes == 0 # should be auto-corrected
def test_send_unary_ack():
manager = make_manager()
ack_reqs_dict = {
"ack_id1": requests.AckRequest(
ack_id="ack_id1", byte_size=0, time_to_ack=20, ordering_key="", future=None
),
"ack_id2": requests.AckRequest(
ack_id="ack_id2", byte_size=0, time_to_ack=20, ordering_key="", future=None
),
}
manager.send_unary_ack(ack_ids=["ack_id1", "ack_id2"], ack_reqs_dict=ack_reqs_dict)
manager._client.acknowledge.assert_called_once_with(
subscription=manager._subscription, ack_ids=["ack_id1", "ack_id2"]
)
def test_send_unary_ack_exactly_once_enabled_with_futures():
manager = make_manager()
manager._exactly_once_enabled = True
future1 = futures.Future()
future2 = futures.Future()
ack_reqs_dict = {
"ack_id1": requests.AckRequest(
ack_id="ack_id1",
byte_size=0,
time_to_ack=20,
ordering_key="",
future=future1,
),
"ack_id2": requests.AckRequest(
ack_id="ack_id2",
byte_size=0,
time_to_ack=20,
ordering_key="",
future=future2,
),
}
manager.send_unary_ack(ack_ids=["ack_id1", "ack_id2"], ack_reqs_dict=ack_reqs_dict)
manager._client.acknowledge.assert_called_once_with(
subscription=manager._subscription, ack_ids=["ack_id1", "ack_id2"]
)
assert future1.result() == subscriber_exceptions.AcknowledgeStatus.SUCCESS
assert future2.result() == subscriber_exceptions.AcknowledgeStatus.SUCCESS
def test_send_unary_ack_exactly_once_disabled_with_futures():
manager = make_manager()
future1 = futures.Future()
future2 = futures.Future()
ack_reqs_dict = {
"ack_id1": requests.AckRequest(
ack_id="ack_id1",
byte_size=0,
time_to_ack=20,
ordering_key="",
future=future1,
),
"ack_id2": requests.AckRequest(
ack_id="ack_id2",
byte_size=0,
time_to_ack=20,
ordering_key="",
future=future2,
),
}
manager.send_unary_ack(ack_ids=["ack_id1", "ack_id2"], ack_reqs_dict=ack_reqs_dict)
manager._client.acknowledge.assert_called_once_with(
subscription=manager._subscription, ack_ids=["ack_id1", "ack_id2"]
)
assert future1.result() == subscriber_exceptions.AcknowledgeStatus.SUCCESS
assert future2.result() == subscriber_exceptions.AcknowledgeStatus.SUCCESS
def test_send_unary_modack():
manager = make_manager()
ack_reqs_dict = {
"ack_id3": requests.ModAckRequest(ack_id="ack_id3", seconds=60, future=None),
"ack_id4": requests.ModAckRequest(ack_id="ack_id4", seconds=60, future=None),
"ack_id5": requests.ModAckRequest(ack_id="ack_id5", seconds=60, future=None),
}
manager.send_unary_modack(
modify_deadline_ack_ids=["ack_id3", "ack_id4", "ack_id5"],
modify_deadline_seconds=[10, 20, 20],
ack_reqs_dict=ack_reqs_dict,
)
manager._client.modify_ack_deadline.assert_has_calls(
[
mock.call(
subscription=manager._subscription,
ack_ids=["ack_id3"],
ack_deadline_seconds=10,
),
mock.call(
subscription=manager._subscription,
ack_ids=["ack_id4", "ack_id5"],
ack_deadline_seconds=20,
),
],
any_order=True,
)
def test_send_unary_modack_default_deadline():
manager = make_manager()
ack_reqs_dict = {
"ack_id3": requests.ModAckRequest(ack_id="ack_id3", seconds=60, future=None),
"ack_id4": requests.ModAckRequest(ack_id="ack_id4", seconds=60, future=None),
"ack_id5": requests.ModAckRequest(ack_id="ack_id5", seconds=60, future=None),
}
manager.send_unary_modack(
modify_deadline_ack_ids=["ack_id3", "ack_id4", "ack_id5"],
modify_deadline_seconds=None,
ack_reqs_dict=ack_reqs_dict,
default_deadline=10,
)
manager._client.modify_ack_deadline.assert_has_calls(
[
mock.call(
subscription=manager._subscription,
ack_ids=["ack_id3", "ack_id4", "ack_id5"],
ack_deadline_seconds=10,
),
],
any_order=True,
)
def test_send_unary_modack_exactly_once_enabled_with_futures():
manager = make_manager()
manager._exactly_once_enabled = True
future1 = futures.Future()
future2 = futures.Future()
future3 = futures.Future()
ack_reqs_dict = {
"ack_id3": requests.ModAckRequest(ack_id="ack_id3", seconds=60, future=future1),
"ack_id4": requests.ModAckRequest(ack_id="ack_id4", seconds=60, future=future2),
"ack_id5": requests.ModAckRequest(ack_id="ack_id5", seconds=60, future=future3),
}
manager.send_unary_modack(
modify_deadline_ack_ids=["ack_id3", "ack_id4", "ack_id5"],
modify_deadline_seconds=[10, 20, 20],
ack_reqs_dict=ack_reqs_dict,
)
manager._client.modify_ack_deadline.assert_has_calls(
[
mock.call(
subscription=manager._subscription,
ack_ids=["ack_id3"],
ack_deadline_seconds=10,
),
mock.call(
subscription=manager._subscription,
ack_ids=["ack_id4", "ack_id5"],
ack_deadline_seconds=20,
),
],
any_order=True,
)
assert future1.result() == subscriber_exceptions.AcknowledgeStatus.SUCCESS
assert future2.result() == subscriber_exceptions.AcknowledgeStatus.SUCCESS
assert future3.result() == subscriber_exceptions.AcknowledgeStatus.SUCCESS
def test_send_unary_modack_exactly_once_disabled_with_futures():
manager = make_manager()
future1 = futures.Future()
future2 = futures.Future()
future3 = futures.Future()
ack_reqs_dict = {
"ack_id3": requests.ModAckRequest(ack_id="ack_id3", seconds=60, future=future1),
"ack_id4": requests.ModAckRequest(ack_id="ack_id4", seconds=60, future=future2),
"ack_id5": requests.ModAckRequest(ack_id="ack_id5", seconds=60, future=future3),
}
manager.send_unary_modack(
modify_deadline_ack_ids=["ack_id3", "ack_id4", "ack_id5"],
modify_deadline_seconds=[10, 20, 20],
ack_reqs_dict=ack_reqs_dict,
)
manager._client.modify_ack_deadline.assert_has_calls(
[
mock.call(
subscription=manager._subscription,
ack_ids=["ack_id3"],
ack_deadline_seconds=10,
),
mock.call(
subscription=manager._subscription,
ack_ids=["ack_id4", "ack_id5"],
ack_deadline_seconds=20,
),
],
any_order=True,
)
assert future1.result() == subscriber_exceptions.AcknowledgeStatus.SUCCESS
assert future2.result() == subscriber_exceptions.AcknowledgeStatus.SUCCESS
assert future3.result() == subscriber_exceptions.AcknowledgeStatus.SUCCESS
def test_send_unary_ack_api_call_error(caplog):
caplog.set_level(logging.DEBUG)
manager = make_manager()
error = exceptions.GoogleAPICallError("The front fell off")
manager._client.acknowledge.side_effect = error
ack_reqs_dict = {
"ack_id1": requests.AckRequest(
ack_id="ack_id1", byte_size=0, time_to_ack=20, ordering_key="", future=None
),
"ack_id2": requests.AckRequest(
ack_id="ack_id2", byte_size=0, time_to_ack=20, ordering_key="", future=None
),
}
manager.send_unary_ack(ack_ids=["ack_id1", "ack_id2"], ack_reqs_dict=ack_reqs_dict)
assert "The front fell off" in caplog.text
def test_send_unary_modack_api_call_error(caplog):
caplog.set_level(logging.DEBUG)
manager = make_manager()
error = exceptions.GoogleAPICallError("The front fell off")
manager._client.modify_ack_deadline.side_effect = error
ack_reqs_dict = {
"ack_id1": requests.AckRequest(
ack_id="ack_id1",
byte_size=0,
time_to_ack=20,
ordering_key="",
future=futures.Future(),
),
"ack_id2": requests.AckRequest(
ack_id="ack_id2",
byte_size=0,
time_to_ack=20,
ordering_key="",
future=futures.Future(),
),
}
manager.send_unary_modack(
modify_deadline_ack_ids=["ack_id_string"],
modify_deadline_seconds=[0],
ack_reqs_dict=ack_reqs_dict,
)
assert "The front fell off" in caplog.text
def test_send_unary_ack_retry_error_exactly_once_disabled_no_futures(caplog):
caplog.set_level(logging.DEBUG)
manager, _, _, _, _, _ = make_running_manager()
manager._exactly_once_enabled = False
error = exceptions.RetryError(
"Too long a transient error", cause=Exception("Out of time!")
)
manager._client.acknowledge.side_effect = error
ack_reqs_dict = {
"ack_id1": requests.AckRequest(
ack_id="ack_id1",
byte_size=0,
time_to_ack=20,
ordering_key="",
future=None,
),
"ack_id2": requests.AckRequest(
ack_id="ack_id2",
byte_size=0,
time_to_ack=20,
ordering_key="",
future=None,
),
}
with pytest.raises(exceptions.RetryError):
manager.send_unary_ack(
ack_ids=["ack_id1", "ack_id2"], ack_reqs_dict=ack_reqs_dict
)
assert "RetryError while sending ack RPC" in caplog.text
assert "signaled streaming pull manager shutdown" in caplog.text
def test_send_unary_ack_retry_error_exactly_once_disabled_with_futures(caplog):
caplog.set_level(logging.DEBUG)
manager, _, _, _, _, _ = make_running_manager()
manager._exactly_once_enabled = False
error = exceptions.RetryError(
"Too long a transient error", cause=Exception("Out of time!")
)
manager._client.acknowledge.side_effect = error
future1 = futures.Future()
future2 = futures.Future()
ack_reqs_dict = {
"ack_id1": requests.AckRequest(
ack_id="ack_id1",
byte_size=0,
time_to_ack=20,
ordering_key="",
future=future1,
),
"ack_id2": requests.AckRequest(
ack_id="ack_id2",
byte_size=0,
time_to_ack=20,
ordering_key="",
future=future2,
),
}
with pytest.raises(exceptions.RetryError):
manager.send_unary_ack(
ack_ids=["ack_id1", "ack_id2"], ack_reqs_dict=ack_reqs_dict
)
assert "RetryError while sending ack RPC" in caplog.text
assert "signaled streaming pull manager shutdown" in caplog.text
assert future1.result() == subscriber_exceptions.AcknowledgeStatus.SUCCESS
assert future2.result() == subscriber_exceptions.AcknowledgeStatus.SUCCESS
def test_send_unary_ack_retry_error_exactly_once_enabled_no_futures(caplog):
caplog.set_level(logging.DEBUG)
manager, _, _, _, _, _ = make_running_manager()
manager._exactly_once_enabled = True
error = exceptions.RetryError(
"Too long a transient error", cause=Exception("Out of time!")
)
manager._client.acknowledge.side_effect = error
ack_reqs_dict = {
"ack_id1": requests.AckRequest(
ack_id="ack_id1",
byte_size=0,
time_to_ack=20,
ordering_key="",
future=None,
),
"ack_id2": requests.AckRequest(
ack_id="ack_id2",
byte_size=0,
time_to_ack=20,
ordering_key="",
future=None,
),
}
with pytest.raises(exceptions.RetryError):
manager.send_unary_ack(
ack_ids=["ack_id1", "ack_id2"], ack_reqs_dict=ack_reqs_dict
)
assert "RetryError while sending ack RPC" in caplog.text
assert "signaled streaming pull manager shutdown" in caplog.text
def test_send_unary_ack_retry_error_exactly_once_enabled_with_futures(caplog):
caplog.set_level(logging.DEBUG)
manager, _, _, _, _, _ = make_running_manager()
manager._exactly_once_enabled = True
error = exceptions.RetryError(
"Too long a transient error", cause=Exception("Out of time!")
)
manager._client.acknowledge.side_effect = error
future1 = futures.Future()
future2 = futures.Future()
ack_reqs_dict = {
"ack_id1": requests.AckRequest(
ack_id="ack_id1",
byte_size=0,
time_to_ack=20,
ordering_key="",
future=future1,
),
"ack_id2": requests.AckRequest(
ack_id="ack_id2",
byte_size=0,
time_to_ack=20,
ordering_key="",
future=future2,
),
}
with pytest.raises(exceptions.RetryError):
manager.send_unary_ack(
ack_ids=["ack_id1", "ack_id2"], ack_reqs_dict=ack_reqs_dict
)
assert "RetryError while sending ack RPC" in caplog.text
assert "signaled streaming pull manager shutdown" in caplog.text
assert isinstance(future1.exception(), subscriber_exceptions.AcknowledgeError)
assert (
future1.exception().error_code is subscriber_exceptions.AcknowledgeStatus.OTHER
)
assert isinstance(future2.exception(), subscriber_exceptions.AcknowledgeError)
assert (
future2.exception().error_code is subscriber_exceptions.AcknowledgeStatus.OTHER
)