forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest__gax.py
More file actions
1045 lines (828 loc) · 37.9 KB
/
test__gax.py
File metadata and controls
1045 lines (828 loc) · 37.9 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
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 2016 Google Inc.
#
# 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 unittest
try:
# pylint: disable=unused-import
import google.cloud.pubsub._gax
# pylint: enable=unused-import
except ImportError: # pragma: NO COVER
_HAVE_GAX = False
else:
_HAVE_GAX = True
from google.cloud._testing import _GAXBaseAPI
class _Base(object):
PROJECT = 'PROJECT'
PROJECT_PATH = 'projects/%s' % (PROJECT,)
LIST_TOPICS_PATH = '%s/topics' % (PROJECT_PATH,)
TOPIC_NAME = 'topic_name'
TOPIC_PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
LIST_TOPIC_SUBSCRIPTIONS_PATH = '%s/subscriptions' % (TOPIC_PATH,)
SUB_NAME = 'sub_name'
SUB_PATH = '%s/subscriptions/%s' % (TOPIC_PATH, SUB_NAME)
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
@unittest.skipUnless(_HAVE_GAX, 'No gax-python')
class Test_PublisherAPI(_Base, unittest.TestCase):
def _getTargetClass(self):
from google.cloud.pubsub._gax import _PublisherAPI
return _PublisherAPI
def test_ctor(self):
gax_api = _GAXPublisherAPI()
api = self._makeOne(gax_api)
self.assertIs(api._gax_api, gax_api)
def test_list_topics_no_paging(self):
from google.gax import INITIAL_PAGE
from google.cloud._testing import _GAXPageIterator
TOKEN = 'TOKEN'
response = _GAXPageIterator([_TopicPB(self.TOPIC_PATH)], TOKEN)
gax_api = _GAXPublisherAPI(_list_topics_response=response)
api = self._makeOne(gax_api)
topics, next_token = api.list_topics(self.PROJECT)
self.assertEqual(len(topics), 1)
topic = topics[0]
self.assertIsInstance(topic, dict)
self.assertEqual(topic['name'], self.TOPIC_PATH)
self.assertEqual(next_token, TOKEN)
name, page_size, options = gax_api._list_topics_called_with
self.assertEqual(name, self.PROJECT_PATH)
self.assertEqual(page_size, 0)
self.assertIs(options.page_token, INITIAL_PAGE)
def test_list_topics_with_paging(self):
from google.cloud._testing import _GAXPageIterator
SIZE = 23
TOKEN = 'TOKEN'
NEW_TOKEN = 'NEW_TOKEN'
response = _GAXPageIterator(
[_TopicPB(self.TOPIC_PATH)], NEW_TOKEN)
gax_api = _GAXPublisherAPI(_list_topics_response=response)
api = self._makeOne(gax_api)
topics, next_token = api.list_topics(
self.PROJECT, page_size=SIZE, page_token=TOKEN)
self.assertEqual(len(topics), 1)
topic = topics[0]
self.assertIsInstance(topic, dict)
self.assertEqual(topic['name'], self.TOPIC_PATH)
self.assertEqual(next_token, NEW_TOKEN)
name, page_size, options = gax_api._list_topics_called_with
self.assertEqual(name, self.PROJECT_PATH)
self.assertEqual(page_size, SIZE)
self.assertEqual(options.page_token, TOKEN)
def test_topic_create(self):
topic_pb = _TopicPB(self.TOPIC_PATH)
gax_api = _GAXPublisherAPI(_create_topic_response=topic_pb)
api = self._makeOne(gax_api)
resource = api.topic_create(self.TOPIC_PATH)
self.assertEqual(resource, {'name': self.TOPIC_PATH})
topic_path, options = gax_api._create_topic_called_with
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertIsNone(options)
def test_topic_create_already_exists(self):
from google.cloud.exceptions import Conflict
gax_api = _GAXPublisherAPI(_create_topic_conflict=True)
api = self._makeOne(gax_api)
with self.assertRaises(Conflict):
api.topic_create(self.TOPIC_PATH)
topic_path, options = gax_api._create_topic_called_with
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertIsNone(options)
def test_topic_create_error(self):
from google.gax.errors import GaxError
gax_api = _GAXPublisherAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
with self.assertRaises(GaxError):
api.topic_create(self.TOPIC_PATH)
topic_path, options = gax_api._create_topic_called_with
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertIsNone(options)
def test_topic_get_hit(self):
topic_pb = _TopicPB(self.TOPIC_PATH)
gax_api = _GAXPublisherAPI(_get_topic_response=topic_pb)
api = self._makeOne(gax_api)
resource = api.topic_get(self.TOPIC_PATH)
self.assertEqual(resource, {'name': self.TOPIC_PATH})
topic_path, options = gax_api._get_topic_called_with
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertIsNone(options)
def test_topic_get_miss(self):
from google.cloud.exceptions import NotFound
gax_api = _GAXPublisherAPI()
api = self._makeOne(gax_api)
with self.assertRaises(NotFound):
api.topic_get(self.TOPIC_PATH)
topic_path, options = gax_api._get_topic_called_with
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertIsNone(options)
def test_topic_get_error(self):
from google.gax.errors import GaxError
gax_api = _GAXPublisherAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
with self.assertRaises(GaxError):
api.topic_get(self.TOPIC_PATH)
topic_path, options = gax_api._get_topic_called_with
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertIsNone(options)
def test_topic_delete_hit(self):
gax_api = _GAXPublisherAPI(_delete_topic_ok=True)
api = self._makeOne(gax_api)
api.topic_delete(self.TOPIC_PATH)
topic_path, options = gax_api._delete_topic_called_with
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertIsNone(options)
def test_topic_delete_miss(self):
from google.cloud.exceptions import NotFound
gax_api = _GAXPublisherAPI(_delete_topic_ok=False)
api = self._makeOne(gax_api)
with self.assertRaises(NotFound):
api.topic_delete(self.TOPIC_PATH)
topic_path, options = gax_api._delete_topic_called_with
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertIsNone(options)
def test_topic_delete_error(self):
from google.gax.errors import GaxError
gax_api = _GAXPublisherAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
with self.assertRaises(GaxError):
api.topic_delete(self.TOPIC_PATH)
topic_path, options = gax_api._delete_topic_called_with
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertIsNone(options)
def test_topic_publish_hit(self):
import base64
PAYLOAD = b'This is the message text'
B64 = base64.b64encode(PAYLOAD).decode('ascii')
MSGID = 'DEADBEEF'
MESSAGE = {'data': B64, 'attributes': {}}
response = _PublishResponsePB([MSGID])
gax_api = _GAXPublisherAPI(_publish_response=response)
api = self._makeOne(gax_api)
resource = api.topic_publish(self.TOPIC_PATH, [MESSAGE])
self.assertEqual(resource, [MSGID])
topic_path, message_pbs, options = gax_api._publish_called_with
self.assertEqual(topic_path, self.TOPIC_PATH)
message_pb, = message_pbs
self.assertEqual(message_pb.data.decode('ascii'), B64)
self.assertEqual(message_pb.attributes, {})
self.assertEqual(options.is_bundling, False)
def test_topic_publish_miss_w_attrs_w_bytes_payload(self):
import base64
from google.cloud.exceptions import NotFound
PAYLOAD = b'This is the message text'
B64 = base64.b64encode(PAYLOAD)
MESSAGE = {'data': B64, 'attributes': {'foo': 'bar'}}
gax_api = _GAXPublisherAPI()
api = self._makeOne(gax_api)
with self.assertRaises(NotFound):
api.topic_publish(self.TOPIC_PATH, [MESSAGE])
topic_path, message_pbs, options = gax_api._publish_called_with
self.assertEqual(topic_path, self.TOPIC_PATH)
message_pb, = message_pbs
self.assertEqual(message_pb.data, B64)
self.assertEqual(message_pb.attributes, {'foo': 'bar'})
self.assertEqual(options.is_bundling, False)
def test_topic_publish_error(self):
import base64
from google.gax.errors import GaxError
PAYLOAD = b'This is the message text'
B64 = base64.b64encode(PAYLOAD).decode('ascii')
MESSAGE = {'data': B64, 'attributes': {}}
gax_api = _GAXPublisherAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
with self.assertRaises(GaxError):
api.topic_publish(self.TOPIC_PATH, [MESSAGE])
topic_path, message_pbs, options = gax_api._publish_called_with
self.assertEqual(topic_path, self.TOPIC_PATH)
message_pb, = message_pbs
self.assertEqual(message_pb.data.decode('ascii'), B64)
self.assertEqual(message_pb.attributes, {})
self.assertEqual(options.is_bundling, False)
def test_topic_list_subscriptions_no_paging(self):
from google.gax import INITIAL_PAGE
from google.cloud._testing import _GAXPageIterator
response = _GAXPageIterator([
{'name': self.SUB_PATH, 'topic': self.TOPIC_PATH}], None)
gax_api = _GAXPublisherAPI(_list_topic_subscriptions_response=response)
api = self._makeOne(gax_api)
subscriptions, next_token = api.topic_list_subscriptions(
self.TOPIC_PATH)
self.assertEqual(len(subscriptions), 1)
subscription = subscriptions[0]
self.assertIsInstance(subscription, dict)
self.assertEqual(subscription['name'], self.SUB_PATH)
self.assertEqual(subscription['topic'], self.TOPIC_PATH)
self.assertIsNone(next_token)
topic_path, page_size, options = (
gax_api._list_topic_subscriptions_called_with)
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertEqual(page_size, 0)
self.assertIs(options.page_token, INITIAL_PAGE)
def test_topic_list_subscriptions_with_paging(self):
from google.cloud._testing import _GAXPageIterator
SIZE = 23
TOKEN = 'TOKEN'
NEW_TOKEN = 'NEW_TOKEN'
response = _GAXPageIterator([
{'name': self.SUB_PATH, 'topic': self.TOPIC_PATH}], NEW_TOKEN)
gax_api = _GAXPublisherAPI(_list_topic_subscriptions_response=response)
api = self._makeOne(gax_api)
subscriptions, next_token = api.topic_list_subscriptions(
self.TOPIC_PATH, page_size=SIZE, page_token=TOKEN)
self.assertEqual(len(subscriptions), 1)
subscription = subscriptions[0]
self.assertIsInstance(subscription, dict)
self.assertEqual(subscription['name'], self.SUB_PATH)
self.assertEqual(subscription['topic'], self.TOPIC_PATH)
self.assertEqual(next_token, NEW_TOKEN)
name, page_size, options = (
gax_api._list_topic_subscriptions_called_with)
self.assertEqual(name, self.TOPIC_PATH)
self.assertEqual(page_size, SIZE)
self.assertEqual(options.page_token, TOKEN)
def test_topic_list_subscriptions_miss(self):
from google.gax import INITIAL_PAGE
from google.cloud.exceptions import NotFound
gax_api = _GAXPublisherAPI()
api = self._makeOne(gax_api)
with self.assertRaises(NotFound):
api.topic_list_subscriptions(self.TOPIC_PATH)
topic_path, page_size, options = (
gax_api._list_topic_subscriptions_called_with)
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertEqual(page_size, 0)
self.assertIs(options.page_token, INITIAL_PAGE)
def test_topic_list_subscriptions_error(self):
from google.gax import INITIAL_PAGE
from google.gax.errors import GaxError
gax_api = _GAXPublisherAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
with self.assertRaises(GaxError):
api.topic_list_subscriptions(self.TOPIC_PATH)
topic_path, page_size, options = (
gax_api._list_topic_subscriptions_called_with)
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertEqual(page_size, 0)
self.assertIs(options.page_token, INITIAL_PAGE)
@unittest.skipUnless(_HAVE_GAX, 'No gax-python')
class Test_SubscriberAPI(_Base, unittest.TestCase):
PUSH_ENDPOINT = 'https://api.example.com/push'
def _getTargetClass(self):
from google.cloud.pubsub._gax import _SubscriberAPI
return _SubscriberAPI
def test_ctor(self):
gax_api = _GAXSubscriberAPI()
api = self._makeOne(gax_api)
self.assertIs(api._gax_api, gax_api)
def test_list_subscriptions_no_paging(self):
from google.gax import INITIAL_PAGE
from google.cloud._testing import _GAXPageIterator
response = _GAXPageIterator([_SubscriptionPB(
self.SUB_PATH, self.TOPIC_PATH, self.PUSH_ENDPOINT, 0)], None)
gax_api = _GAXSubscriberAPI(_list_subscriptions_response=response)
api = self._makeOne(gax_api)
subscriptions, next_token = api.list_subscriptions(self.PROJECT)
self.assertEqual(len(subscriptions), 1)
subscription = subscriptions[0]
self.assertIsInstance(subscription, dict)
self.assertEqual(subscription['name'], self.SUB_PATH)
self.assertEqual(subscription['topic'], self.TOPIC_PATH)
self.assertEqual(subscription['pushConfig'],
{'pushEndpoint': self.PUSH_ENDPOINT})
self.assertEqual(subscription['ackDeadlineSeconds'], 0)
self.assertIsNone(next_token)
name, page_size, options = gax_api._list_subscriptions_called_with
self.assertEqual(name, self.PROJECT_PATH)
self.assertEqual(page_size, 0)
self.assertIs(options.page_token, INITIAL_PAGE)
def test_list_subscriptions_with_paging(self):
from google.cloud._testing import _GAXPageIterator
SIZE = 23
TOKEN = 'TOKEN'
NEW_TOKEN = 'NEW_TOKEN'
response = _GAXPageIterator([_SubscriptionPB(
self.SUB_PATH, self.TOPIC_PATH, self.PUSH_ENDPOINT, 0)], NEW_TOKEN)
gax_api = _GAXSubscriberAPI(_list_subscriptions_response=response)
api = self._makeOne(gax_api)
subscriptions, next_token = api.list_subscriptions(
self.PROJECT, page_size=SIZE, page_token=TOKEN)
self.assertEqual(len(subscriptions), 1)
subscription = subscriptions[0]
self.assertIsInstance(subscription, dict)
self.assertEqual(subscription['name'], self.SUB_PATH)
self.assertEqual(subscription['topic'], self.TOPIC_PATH)
self.assertEqual(subscription['pushConfig'],
{'pushEndpoint': self.PUSH_ENDPOINT})
self.assertEqual(subscription['ackDeadlineSeconds'], 0)
self.assertEqual(next_token, NEW_TOKEN)
name, page_size, options = gax_api._list_subscriptions_called_with
self.assertEqual(name, self.PROJECT_PATH)
self.assertEqual(page_size, 23)
self.assertEqual(options.page_token, TOKEN)
def test_subscription_create(self):
sub_pb = _SubscriptionPB(self.SUB_PATH, self.TOPIC_PATH, '', 0)
gax_api = _GAXSubscriberAPI(_create_subscription_response=sub_pb)
api = self._makeOne(gax_api)
resource = api.subscription_create(self.SUB_PATH, self.TOPIC_PATH)
expected = {
'name': self.SUB_PATH,
'topic': self.TOPIC_PATH,
'ackDeadlineSeconds': 0,
}
self.assertEqual(resource, expected)
name, topic, push_config, ack_deadline, options = (
gax_api._create_subscription_called_with)
self.assertEqual(name, self.SUB_PATH)
self.assertEqual(topic, self.TOPIC_PATH)
self.assertIsNone(push_config)
self.assertEqual(ack_deadline, 0)
self.assertIsNone(options)
def test_subscription_create_already_exists(self):
from google.cloud.exceptions import Conflict
DEADLINE = 600
gax_api = _GAXSubscriberAPI(_create_subscription_conflict=True)
api = self._makeOne(gax_api)
with self.assertRaises(Conflict):
api.subscription_create(
self.SUB_PATH, self.TOPIC_PATH, DEADLINE, self.PUSH_ENDPOINT)
name, topic, push_config, ack_deadline, options = (
gax_api._create_subscription_called_with)
self.assertEqual(name, self.SUB_PATH)
self.assertEqual(topic, self.TOPIC_PATH)
self.assertEqual(push_config.push_endpoint, self.PUSH_ENDPOINT)
self.assertEqual(ack_deadline, DEADLINE)
self.assertIsNone(options)
def test_subscription_create_error(self):
from google.gax.errors import GaxError
gax_api = _GAXSubscriberAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
with self.assertRaises(GaxError):
api.subscription_create(self.SUB_PATH, self.TOPIC_PATH)
name, topic, push_config, ack_deadline, options = (
gax_api._create_subscription_called_with)
self.assertEqual(name, self.SUB_PATH)
self.assertEqual(topic, self.TOPIC_PATH)
self.assertIsNone(push_config)
self.assertEqual(ack_deadline, 0)
self.assertIsNone(options)
def test_subscription_get_hit(self):
sub_pb = _SubscriptionPB(
self.SUB_PATH, self.TOPIC_PATH, self.PUSH_ENDPOINT, 0)
gax_api = _GAXSubscriberAPI(_get_subscription_response=sub_pb)
api = self._makeOne(gax_api)
resource = api.subscription_get(self.SUB_PATH)
expected = {
'name': self.SUB_PATH,
'topic': self.TOPIC_PATH,
'ackDeadlineSeconds': 0,
'pushConfig': {
'pushEndpoint': self.PUSH_ENDPOINT,
},
}
self.assertEqual(resource, expected)
sub_path, options = gax_api._get_subscription_called_with
self.assertEqual(sub_path, self.SUB_PATH)
self.assertIsNone(options)
def test_subscription_get_miss(self):
from google.cloud.exceptions import NotFound
gax_api = _GAXSubscriberAPI()
api = self._makeOne(gax_api)
with self.assertRaises(NotFound):
api.subscription_get(self.SUB_PATH)
sub_path, options = gax_api._get_subscription_called_with
self.assertEqual(sub_path, self.SUB_PATH)
self.assertIsNone(options)
def test_subscription_get_error(self):
from google.gax.errors import GaxError
gax_api = _GAXSubscriberAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
with self.assertRaises(GaxError):
api.subscription_get(self.SUB_PATH)
sub_path, options = gax_api._get_subscription_called_with
self.assertEqual(sub_path, self.SUB_PATH)
self.assertIsNone(options)
def test_subscription_delete_hit(self):
gax_api = _GAXSubscriberAPI(_delete_subscription_ok=True)
api = self._makeOne(gax_api)
api.subscription_delete(self.TOPIC_PATH)
sub_path, options = gax_api._delete_subscription_called_with
self.assertEqual(sub_path, self.TOPIC_PATH)
self.assertIsNone(options)
def test_subscription_delete_miss(self):
from google.cloud.exceptions import NotFound
gax_api = _GAXSubscriberAPI(_delete_subscription_ok=False)
api = self._makeOne(gax_api)
with self.assertRaises(NotFound):
api.subscription_delete(self.TOPIC_PATH)
sub_path, options = gax_api._delete_subscription_called_with
self.assertEqual(sub_path, self.TOPIC_PATH)
self.assertIsNone(options)
def test_subscription_delete_error(self):
from google.gax.errors import GaxError
gax_api = _GAXSubscriberAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
with self.assertRaises(GaxError):
api.subscription_delete(self.TOPIC_PATH)
sub_path, options = gax_api._delete_subscription_called_with
self.assertEqual(sub_path, self.TOPIC_PATH)
self.assertIsNone(options)
def test_subscription_modify_push_config_hit(self):
gax_api = _GAXSubscriberAPI(_modify_push_config_ok=True)
api = self._makeOne(gax_api)
api.subscription_modify_push_config(self.SUB_PATH, self.PUSH_ENDPOINT)
sub_path, config, options = gax_api._modify_push_config_called_with
self.assertEqual(sub_path, self.SUB_PATH)
self.assertEqual(config.push_endpoint, self.PUSH_ENDPOINT)
self.assertIsNone(options)
def test_subscription_modify_push_config_miss(self):
from google.cloud.exceptions import NotFound
gax_api = _GAXSubscriberAPI()
api = self._makeOne(gax_api)
with self.assertRaises(NotFound):
api.subscription_modify_push_config(
self.SUB_PATH, self.PUSH_ENDPOINT)
sub_path, config, options = gax_api._modify_push_config_called_with
self.assertEqual(sub_path, self.SUB_PATH)
self.assertEqual(config.push_endpoint, self.PUSH_ENDPOINT)
self.assertIsNone(options)
def test_subscription_modify_push_config_error(self):
from google.gax.errors import GaxError
gax_api = _GAXSubscriberAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
with self.assertRaises(GaxError):
api.subscription_modify_push_config(
self.SUB_PATH, self.PUSH_ENDPOINT)
sub_path, config, options = gax_api._modify_push_config_called_with
self.assertEqual(sub_path, self.SUB_PATH)
self.assertEqual(config.push_endpoint, self.PUSH_ENDPOINT)
self.assertIsNone(options)
def test_subscription_pull_explicit(self):
import base64
import datetime
from google.cloud._helpers import UTC
from google.cloud._helpers import _datetime_to_pb_timestamp
from google.cloud._helpers import _datetime_to_rfc3339
NOW = datetime.datetime.utcnow().replace(tzinfo=UTC)
NOW_PB = _datetime_to_pb_timestamp(NOW)
NOW_RFC3339 = _datetime_to_rfc3339(NOW)
PAYLOAD = b'This is the message text'
B64 = base64.b64encode(PAYLOAD).decode('ascii')
ACK_ID = 'DEADBEEF'
MSG_ID = 'BEADCAFE'
MESSAGE = {
'messageId': MSG_ID,
'data': B64,
'attributes': {'a': 'b'},
'publishTime': NOW_RFC3339,
}
RECEIVED = [{'ackId': ACK_ID, 'message': MESSAGE}]
message_pb = _PubsubMessagePB(MSG_ID, B64, {'a': 'b'}, NOW_PB)
response_pb = _PullResponsePB([_ReceivedMessagePB(ACK_ID, message_pb)])
gax_api = _GAXSubscriberAPI(_pull_response=response_pb)
api = self._makeOne(gax_api)
MAX_MESSAGES = 10
received = api.subscription_pull(
self.SUB_PATH, return_immediately=True, max_messages=MAX_MESSAGES)
self.assertEqual(received, RECEIVED)
sub_path, max_messages, return_immediately, options = (
gax_api._pull_called_with)
self.assertEqual(sub_path, self.SUB_PATH)
self.assertEqual(max_messages, MAX_MESSAGES)
self.assertTrue(return_immediately)
self.assertIsNone(options)
def test_subscription_pull_defaults_miss(self):
from google.cloud.exceptions import NotFound
gax_api = _GAXSubscriberAPI()
api = self._makeOne(gax_api)
with self.assertRaises(NotFound):
api.subscription_pull(self.SUB_PATH)
sub_path, max_messages, return_immediately, options = (
gax_api._pull_called_with)
self.assertEqual(sub_path, self.SUB_PATH)
self.assertEqual(max_messages, 1)
self.assertFalse(return_immediately)
self.assertIsNone(options)
def test_subscription_pull_defaults_error(self):
from google.gax.errors import GaxError
gax_api = _GAXSubscriberAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
with self.assertRaises(GaxError):
api.subscription_pull(self.SUB_PATH)
sub_path, max_messages, return_immediately, options = (
gax_api._pull_called_with)
self.assertEqual(sub_path, self.SUB_PATH)
self.assertEqual(max_messages, 1)
self.assertFalse(return_immediately)
self.assertIsNone(options)
def test_subscription_acknowledge_hit(self):
ACK_ID1 = 'DEADBEEF'
ACK_ID2 = 'BEADCAFE'
gax_api = _GAXSubscriberAPI(_acknowledge_ok=True)
api = self._makeOne(gax_api)
api.subscription_acknowledge(self.SUB_PATH, [ACK_ID1, ACK_ID2])
sub_path, ack_ids, options = gax_api._acknowledge_called_with
self.assertEqual(sub_path, self.SUB_PATH)
self.assertEqual(ack_ids, [ACK_ID1, ACK_ID2])
self.assertIsNone(options)
def test_subscription_acknowledge_miss(self):
from google.cloud.exceptions import NotFound
ACK_ID1 = 'DEADBEEF'
ACK_ID2 = 'BEADCAFE'
gax_api = _GAXSubscriberAPI()
api = self._makeOne(gax_api)
with self.assertRaises(NotFound):
api.subscription_acknowledge(self.SUB_PATH, [ACK_ID1, ACK_ID2])
sub_path, ack_ids, options = gax_api._acknowledge_called_with
self.assertEqual(sub_path, self.SUB_PATH)
self.assertEqual(ack_ids, [ACK_ID1, ACK_ID2])
self.assertIsNone(options)
def test_subscription_acknowledge_error(self):
from google.gax.errors import GaxError
ACK_ID1 = 'DEADBEEF'
ACK_ID2 = 'BEADCAFE'
gax_api = _GAXSubscriberAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
with self.assertRaises(GaxError):
api.subscription_acknowledge(self.SUB_PATH, [ACK_ID1, ACK_ID2])
sub_path, ack_ids, options = gax_api._acknowledge_called_with
self.assertEqual(sub_path, self.SUB_PATH)
self.assertEqual(ack_ids, [ACK_ID1, ACK_ID2])
self.assertIsNone(options)
def test_subscription_modify_ack_deadline_hit(self):
ACK_ID1 = 'DEADBEEF'
ACK_ID2 = 'BEADCAFE'
NEW_DEADLINE = 90
gax_api = _GAXSubscriberAPI(_modify_ack_deadline_ok=True)
api = self._makeOne(gax_api)
api.subscription_modify_ack_deadline(
self.SUB_PATH, [ACK_ID1, ACK_ID2], NEW_DEADLINE)
sub_path, ack_ids, deadline, options = (
gax_api._modify_ack_deadline_called_with)
self.assertEqual(sub_path, self.SUB_PATH)
self.assertEqual(ack_ids, [ACK_ID1, ACK_ID2])
self.assertEqual(deadline, NEW_DEADLINE)
self.assertIsNone(options)
def test_subscription_modify_ack_deadline_miss(self):
from google.cloud.exceptions import NotFound
ACK_ID1 = 'DEADBEEF'
ACK_ID2 = 'BEADCAFE'
NEW_DEADLINE = 90
gax_api = _GAXSubscriberAPI()
api = self._makeOne(gax_api)
with self.assertRaises(NotFound):
api.subscription_modify_ack_deadline(
self.SUB_PATH, [ACK_ID1, ACK_ID2], NEW_DEADLINE)
sub_path, ack_ids, deadline, options = (
gax_api._modify_ack_deadline_called_with)
self.assertEqual(sub_path, self.SUB_PATH)
self.assertEqual(ack_ids, [ACK_ID1, ACK_ID2])
self.assertEqual(deadline, NEW_DEADLINE)
self.assertIsNone(options)
def test_subscription_modify_ack_deadline_error(self):
from google.gax.errors import GaxError
ACK_ID1 = 'DEADBEEF'
ACK_ID2 = 'BEADCAFE'
NEW_DEADLINE = 90
gax_api = _GAXSubscriberAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
with self.assertRaises(GaxError):
api.subscription_modify_ack_deadline(
self.SUB_PATH, [ACK_ID1, ACK_ID2], NEW_DEADLINE)
sub_path, ack_ids, deadline, options = (
gax_api._modify_ack_deadline_called_with)
self.assertEqual(sub_path, self.SUB_PATH)
self.assertEqual(ack_ids, [ACK_ID1, ACK_ID2])
self.assertEqual(deadline, NEW_DEADLINE)
self.assertIsNone(options)
@unittest.skipUnless(_HAVE_GAX, 'No gax-python')
class Test_make_gax_publisher_api(_Base, unittest.TestCase):
def _callFUT(self, connection):
from google.cloud.pubsub._gax import make_gax_publisher_api
return make_gax_publisher_api(connection)
def test_live_api(self):
from google.cloud._testing import _Monkey
from google.cloud.pubsub import _gax as MUT
channels = []
mock_result = object()
def mock_publisher_api(channel):
channels.append(channel)
return mock_result
connection = _Connection(in_emulator=False)
with _Monkey(MUT, PublisherApi=mock_publisher_api):
result = self._callFUT(connection)
self.assertIs(result, mock_result)
self.assertEqual(channels, [None])
def test_emulator(self):
from google.cloud._testing import _Monkey
from google.cloud.pubsub import _gax as MUT
channels = []
mock_result = object()
insecure_args = []
mock_channel = object()
def mock_publisher_api(channel):
channels.append(channel)
return mock_result
def mock_insecure_channel(host):
insecure_args.append(host)
return mock_channel
host = 'CURR_HOST:1234'
connection = _Connection(in_emulator=True, host=host)
with _Monkey(MUT, PublisherApi=mock_publisher_api,
insecure_channel=mock_insecure_channel):
result = self._callFUT(connection)
self.assertIs(result, mock_result)
self.assertEqual(channels, [mock_channel])
self.assertEqual(insecure_args, [host])
@unittest.skipUnless(_HAVE_GAX, 'No gax-python')
class Test_make_gax_subscriber_api(_Base, unittest.TestCase):
def _callFUT(self, connection):
from google.cloud.pubsub._gax import make_gax_subscriber_api
return make_gax_subscriber_api(connection)
def test_live_api(self):
from google.cloud._testing import _Monkey
from google.cloud.pubsub import _gax as MUT
channels = []
mock_result = object()
def mock_subscriber_api(channel):
channels.append(channel)
return mock_result
connection = _Connection(in_emulator=False)
with _Monkey(MUT, SubscriberApi=mock_subscriber_api):
result = self._callFUT(connection)
self.assertIs(result, mock_result)
self.assertEqual(channels, [None])
def test_emulator(self):
from google.cloud._testing import _Monkey
from google.cloud.pubsub import _gax as MUT
channels = []
mock_result = object()
insecure_args = []
mock_channel = object()
def mock_subscriber_api(channel):
channels.append(channel)
return mock_result
def mock_insecure_channel(host):
insecure_args.append(host)
return mock_channel
host = 'CURR_HOST:1234'
connection = _Connection(in_emulator=True, host=host)
with _Monkey(MUT, SubscriberApi=mock_subscriber_api,
insecure_channel=mock_insecure_channel):
result = self._callFUT(connection)
self.assertIs(result, mock_result)
self.assertEqual(channels, [mock_channel])
self.assertEqual(insecure_args, [host])
class _GAXPublisherAPI(_GAXBaseAPI):
_create_topic_conflict = False
def list_topics(self, name, page_size, options):
self._list_topics_called_with = name, page_size, options
return self._list_topics_response
def create_topic(self, name, options=None):
from google.gax.errors import GaxError
self._create_topic_called_with = name, options
if self._random_gax_error:
raise GaxError('error')
if self._create_topic_conflict:
raise GaxError('conflict', self._make_grpc_failed_precondition())
return self._create_topic_response
def get_topic(self, name, options=None):
from google.gax.errors import GaxError
self._get_topic_called_with = name, options
if self._random_gax_error:
raise GaxError('error')
try:
return self._get_topic_response
except AttributeError:
raise GaxError('miss', self._make_grpc_not_found())
def delete_topic(self, name, options=None):
from google.gax.errors import GaxError
self._delete_topic_called_with = name, options
if self._random_gax_error:
raise GaxError('error')
if not self._delete_topic_ok:
raise GaxError('miss', self._make_grpc_not_found())
def publish(self, topic, messages, options=None):
from google.gax.errors import GaxError
self._publish_called_with = topic, messages, options
if self._random_gax_error:
raise GaxError('error')
try:
return self._publish_response
except AttributeError:
raise GaxError('miss', self._make_grpc_not_found())
def list_topic_subscriptions(self, topic, page_size, options=None):
from google.gax.errors import GaxError
self._list_topic_subscriptions_called_with = topic, page_size, options
if self._random_gax_error:
raise GaxError('error')
try:
return self._list_topic_subscriptions_response
except AttributeError:
raise GaxError('miss', self._make_grpc_not_found())
class _GAXSubscriberAPI(_GAXBaseAPI):
_create_subscription_conflict = False
_modify_push_config_ok = False
_acknowledge_ok = False
_modify_ack_deadline_ok = False
def list_subscriptions(self, project, page_size, options=None):
self._list_subscriptions_called_with = (project, page_size, options)
return self._list_subscriptions_response
def create_subscription(self, name, topic,
push_config, ack_deadline_seconds,
options=None):
from google.gax.errors import GaxError
self._create_subscription_called_with = (
name, topic, push_config, ack_deadline_seconds, options)
if self._random_gax_error:
raise GaxError('error')
if self._create_subscription_conflict:
raise GaxError('conflict', self._make_grpc_failed_precondition())
return self._create_subscription_response
def get_subscription(self, name, options=None):
from google.gax.errors import GaxError
self._get_subscription_called_with = name, options
if self._random_gax_error:
raise GaxError('error')
try:
return self._get_subscription_response
except AttributeError:
raise GaxError('miss', self._make_grpc_not_found())
def delete_subscription(self, name, options=None):
from google.gax.errors import GaxError
self._delete_subscription_called_with = name, options
if self._random_gax_error:
raise GaxError('error')
if not self._delete_subscription_ok:
raise GaxError('miss', self._make_grpc_not_found())
def modify_push_config(self, name, push_config, options=None):
from google.gax.errors import GaxError
self._modify_push_config_called_with = name, push_config, options
if self._random_gax_error:
raise GaxError('error')
if not self._modify_push_config_ok:
raise GaxError('miss', self._make_grpc_not_found())
def pull(self, name, max_messages, return_immediately, options=None):
from google.gax.errors import GaxError
self._pull_called_with = (
name, max_messages, return_immediately, options)
if self._random_gax_error:
raise GaxError('error')
try:
return self._pull_response
except AttributeError:
raise GaxError('miss', self._make_grpc_not_found())
def acknowledge(self, name, ack_ids, options=None):
from google.gax.errors import GaxError
self._acknowledge_called_with = name, ack_ids, options
if self._random_gax_error:
raise GaxError('error')
if not self._acknowledge_ok:
raise GaxError('miss', self._make_grpc_not_found())
def modify_ack_deadline(self, name, ack_ids, deadline, options=None):
from google.gax.errors import GaxError
self._modify_ack_deadline_called_with = (
name, ack_ids, deadline, options)
if self._random_gax_error:
raise GaxError('error')
if not self._modify_ack_deadline_ok:
raise GaxError('miss', self._make_grpc_not_found())
class _TopicPB(object):
def __init__(self, name):
self.name = name
class _PublishResponsePB(object):
def __init__(self, message_ids):