Skip to content

Commit a3cc3b1

Browse files
committed
Coverage for unexpected GaxError.
Addresses: #1764 (comment)
1 parent 4877f5b commit a3cc3b1

File tree

2 files changed

+83
-5
lines changed

2 files changed

+83
-5
lines changed

gcloud/pubsub/_gax.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def topic_create(self, topic_path):
7777
except GaxError as exc:
7878
if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION:
7979
raise Conflict(topic_path)
80-
raise # pragma: NO COVER
80+
raise
8181
return {'name': topic_pb.name}
8282

8383
def topic_get(self, topic_path):
@@ -100,7 +100,7 @@ def topic_get(self, topic_path):
100100
except GaxError as exc:
101101
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
102102
raise NotFound(topic_path)
103-
raise # pragma: NO COVER
103+
raise
104104
return {'name': topic_pb.name}
105105

106106
def topic_delete(self, topic_path):
@@ -121,7 +121,7 @@ def topic_delete(self, topic_path):
121121
except GaxError as exc:
122122
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
123123
raise NotFound(topic_path)
124-
raise # pragma: NO COVER
124+
raise
125125

126126
def topic_publish(self, topic_path, messages):
127127
"""API call: publish one or more messages to a topic
@@ -148,7 +148,7 @@ def topic_publish(self, topic_path, messages):
148148
except GaxError as exc:
149149
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
150150
raise NotFound(topic_path)
151-
raise # pragma: NO COVER
151+
raise
152152
return response.message_ids
153153

154154
def topic_list_subscriptions(self, topic_path):
@@ -174,7 +174,7 @@ def topic_list_subscriptions(self, topic_path):
174174
except GaxError as exc:
175175
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
176176
raise NotFound(topic_path)
177-
raise # pragma: NO COVER
177+
raise
178178
subs = [{'topic': topic_path, 'name': subscription}
179179
for subscription in response.subscriptions]
180180
return subs, response.next_page_token

gcloud/pubsub/test__gax.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

209276
class _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

Comments
 (0)