Skip to content

Commit

Permalink
Coverage for unexpected GaxError.
Browse files Browse the repository at this point in the history
Addresses:
#1764 (comment)
  • Loading branch information
tseaver committed May 12, 2016
1 parent 4877f5b commit a3cc3b1
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
10 changes: 5 additions & 5 deletions gcloud/pubsub/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def topic_create(self, topic_path):
except GaxError as exc:
if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION:
raise Conflict(topic_path)
raise # pragma: NO COVER
raise
return {'name': topic_pb.name}

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

def topic_delete(self, topic_path):
Expand All @@ -121,7 +121,7 @@ def topic_delete(self, topic_path):
except GaxError as exc:
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
raise NotFound(topic_path)
raise # pragma: NO COVER
raise

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

def topic_list_subscriptions(self, topic_path):
Expand All @@ -174,7 +174,7 @@ def topic_list_subscriptions(self, topic_path):
except GaxError as exc:
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
raise NotFound(topic_path)
raise # pragma: NO COVER
raise
subs = [{'topic': topic_path, 'name': subscription}
for subscription in response.subscriptions]
return subs, response.next_page_token
Expand Down
78 changes: 78 additions & 0 deletions gcloud/pubsub/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ def test_topic_create_already_exists(self):
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertEqual(options, None)

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.assertEqual(options, None)

def test_topic_get_hit(self):
topic_pb = _TopicPB(self.TOPIC_PATH)
gax_api = _GAXPublisherAPI(_get_topic_response=topic_pb)
Expand All @@ -113,6 +125,18 @@ def test_topic_get_miss(self):
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertEqual(options, None)

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.assertEqual(options, None)

def test_topic_delete_hit(self):
gax_api = _GAXPublisherAPI(_delete_topic_ok=True)
api = self._makeOne(gax_api)
Expand All @@ -135,6 +159,18 @@ def test_topic_delete_miss(self):
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertEqual(options, None)

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.assertEqual(options, None)

def test_topic_publish_hit(self):
import base64
PAYLOAD = b'This is the message text'
Expand Down Expand Up @@ -174,6 +210,25 @@ def test_topic_publish_miss_w_attrs_w_bytes_payload(self):
self.assertEqual(message_pb.attributes, {'foo': 'bar'})
self.assertEqual(options, None)

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, B64)
self.assertEqual(message_pb.attributes, {})
self.assertEqual(options, None)

def test_topic_list_subscriptions_no_paging(self):
response = _ListTopicSubscriptionsResponsePB([self.SUB_PATH])
gax_api = _GAXPublisherAPI(_list_topic_subscriptions_response=response)
Expand Down Expand Up @@ -205,10 +260,23 @@ def test_topic_list_subscriptions_miss(self):
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertFalse(options.is_page_streaming)

def test_topic_list_subscriptions_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_list_subscriptions(self.TOPIC_PATH)

topic_path, options = gax_api._list_topic_subscriptions_called_with
self.assertEqual(topic_path, self.TOPIC_PATH)
self.assertFalse(options.is_page_streaming)


class _GAXPublisherAPI(object):

_create_topic_conflict = False
_random_gax_error = False

def __init__(self, **kw):
self.__dict__.update(kw)
Expand Down Expand Up @@ -239,13 +307,17 @@ def _make_grpc_failed_precondition(self):
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:
Expand All @@ -254,12 +326,16 @@ def get_topic(self, name, options=None):
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:
Expand All @@ -268,6 +344,8 @@ def publish(self, topic, messages, options=None):
def list_topic_subscriptions(self, topic, options=None):
from google.gax.errors import GaxError
self._list_topic_subscriptions_called_with = topic, options
if self._random_gax_error:
raise GaxError('error')
try:
return self._list_topic_subscriptions_response
except AttributeError:
Expand Down

0 comments on commit a3cc3b1

Please sign in to comment.