@@ -89,6 +89,18 @@ def test_topic_create_already_exists(self):
8989 self .assertEqual (topic_path , self .TOPIC_PATH )
9090 self .assertEqual (options , None )
9191
92+ def test_topic_create_error (self ):
93+ from google .gax .errors import GaxError
94+ gax_api = _GAXPublisherAPI (_random_gax_error = True )
95+ api = self ._makeOne (gax_api )
96+
97+ with self .assertRaises (GaxError ):
98+ api .topic_create (self .TOPIC_PATH )
99+
100+ topic_path , options = gax_api ._create_topic_called_with
101+ self .assertEqual (topic_path , self .TOPIC_PATH )
102+ self .assertEqual (options , None )
103+
92104 def test_topic_get_hit (self ):
93105 topic_pb = _TopicPB (self .TOPIC_PATH )
94106 gax_api = _GAXPublisherAPI (_get_topic_response = topic_pb )
@@ -113,6 +125,18 @@ def test_topic_get_miss(self):
113125 self .assertEqual (topic_path , self .TOPIC_PATH )
114126 self .assertEqual (options , None )
115127
128+ def test_topic_get_error (self ):
129+ from google .gax .errors import GaxError
130+ gax_api = _GAXPublisherAPI (_random_gax_error = True )
131+ api = self ._makeOne (gax_api )
132+
133+ with self .assertRaises (GaxError ):
134+ api .topic_get (self .TOPIC_PATH )
135+
136+ topic_path , options = gax_api ._get_topic_called_with
137+ self .assertEqual (topic_path , self .TOPIC_PATH )
138+ self .assertEqual (options , None )
139+
116140 def test_topic_delete_hit (self ):
117141 gax_api = _GAXPublisherAPI (_delete_topic_ok = True )
118142 api = self ._makeOne (gax_api )
@@ -135,6 +159,18 @@ def test_topic_delete_miss(self):
135159 self .assertEqual (topic_path , self .TOPIC_PATH )
136160 self .assertEqual (options , None )
137161
162+ def test_topic_delete_error (self ):
163+ from google .gax .errors import GaxError
164+ gax_api = _GAXPublisherAPI (_random_gax_error = True )
165+ api = self ._makeOne (gax_api )
166+
167+ with self .assertRaises (GaxError ):
168+ api .topic_delete (self .TOPIC_PATH )
169+
170+ topic_path , options = gax_api ._delete_topic_called_with
171+ self .assertEqual (topic_path , self .TOPIC_PATH )
172+ self .assertEqual (options , None )
173+
138174 def test_topic_publish_hit (self ):
139175 import base64
140176 PAYLOAD = b'This is the message text'
@@ -174,6 +210,25 @@ def test_topic_publish_miss_w_attrs_w_bytes_payload(self):
174210 self .assertEqual (message_pb .attributes , {'foo' : 'bar' })
175211 self .assertEqual (options , None )
176212
213+ def test_topic_publish_error (self ):
214+ import base64
215+ from google .gax .errors import GaxError
216+ PAYLOAD = b'This is the message text'
217+ B64 = base64 .b64encode (PAYLOAD ).decode ('ascii' )
218+ MESSAGE = {'data' : B64 , 'attributes' : {}}
219+ gax_api = _GAXPublisherAPI (_random_gax_error = True )
220+ api = self ._makeOne (gax_api )
221+
222+ with self .assertRaises (GaxError ):
223+ api .topic_publish (self .TOPIC_PATH , [MESSAGE ])
224+
225+ topic_path , message_pbs , options = gax_api ._publish_called_with
226+ self .assertEqual (topic_path , self .TOPIC_PATH )
227+ message_pb , = message_pbs
228+ self .assertEqual (message_pb .data , B64 )
229+ self .assertEqual (message_pb .attributes , {})
230+ self .assertEqual (options , None )
231+
177232 def test_topic_list_subscriptions_no_paging (self ):
178233 response = _ListTopicSubscriptionsResponsePB ([self .SUB_PATH ])
179234 gax_api = _GAXPublisherAPI (_list_topic_subscriptions_response = response )
@@ -205,10 +260,23 @@ def test_topic_list_subscriptions_miss(self):
205260 self .assertEqual (topic_path , self .TOPIC_PATH )
206261 self .assertFalse (options .is_page_streaming )
207262
263+ def test_topic_list_subscriptions_error (self ):
264+ from google .gax .errors import GaxError
265+ gax_api = _GAXPublisherAPI (_random_gax_error = True )
266+ api = self ._makeOne (gax_api )
267+
268+ with self .assertRaises (GaxError ):
269+ api .topic_list_subscriptions (self .TOPIC_PATH )
270+
271+ topic_path , options = gax_api ._list_topic_subscriptions_called_with
272+ self .assertEqual (topic_path , self .TOPIC_PATH )
273+ self .assertFalse (options .is_page_streaming )
274+
208275
209276class _GAXPublisherAPI (object ):
210277
211278 _create_topic_conflict = False
279+ _random_gax_error = False
212280
213281 def __init__ (self , ** kw ):
214282 self .__dict__ .update (kw )
@@ -239,13 +307,17 @@ def _make_grpc_failed_precondition(self):
239307 def create_topic (self , name , options = None ):
240308 from google .gax .errors import GaxError
241309 self ._create_topic_called_with = name , options
310+ if self ._random_gax_error :
311+ raise GaxError ('error' )
242312 if self ._create_topic_conflict :
243313 raise GaxError ('conflict' , self ._make_grpc_failed_precondition ())
244314 return self ._create_topic_response
245315
246316 def get_topic (self , name , options = None ):
247317 from google .gax .errors import GaxError
248318 self ._get_topic_called_with = name , options
319+ if self ._random_gax_error :
320+ raise GaxError ('error' )
249321 try :
250322 return self ._get_topic_response
251323 except AttributeError :
@@ -254,12 +326,16 @@ def get_topic(self, name, options=None):
254326 def delete_topic (self , name , options = None ):
255327 from google .gax .errors import GaxError
256328 self ._delete_topic_called_with = name , options
329+ if self ._random_gax_error :
330+ raise GaxError ('error' )
257331 if not self ._delete_topic_ok :
258332 raise GaxError ('miss' , self ._make_grpc_not_found ())
259333
260334 def publish (self , topic , messages , options = None ):
261335 from google .gax .errors import GaxError
262336 self ._publish_called_with = topic , messages , options
337+ if self ._random_gax_error :
338+ raise GaxError ('error' )
263339 try :
264340 return self ._publish_response
265341 except AttributeError :
@@ -268,6 +344,8 @@ def publish(self, topic, messages, options=None):
268344 def list_topic_subscriptions (self , topic , options = None ):
269345 from google .gax .errors import GaxError
270346 self ._list_topic_subscriptions_called_with = topic , options
347+ if self ._random_gax_error :
348+ raise GaxError ('error' )
271349 try :
272350 return self ._list_topic_subscriptions_response
273351 except AttributeError :
0 commit comments