diff --git a/zaqarclient/queues/v2/subscription.py b/zaqarclient/queues/v2/subscription.py index 6bf0727e..b007c283 100644 --- a/zaqarclient/queues/v2/subscription.py +++ b/zaqarclient/queues/v2/subscription.py @@ -14,7 +14,6 @@ # limitations under the License. from zaqarclient.queues.v2 import core -from zaqarclient.transport import errors class Subscription(object): @@ -45,16 +44,12 @@ class Subscription(object): 'ttl': self.ttl, 'options': self.options } - try: - subscription = core.subscription_create(trans, req, - self.queue_name, - subscription_data) + subscription = core.subscription_create(trans, req, + self.queue_name, + subscription_data) - if subscription and 'subscription_id' in subscription: - self.id = subscription['subscription_id'] - except errors.ConflictError: - # ConflictError means the subscription already exists. - print('The subscriber has been existed already.') + if subscription and 'subscription_id' in subscription: + self.id = subscription['subscription_id'] if self.id: sub = core.subscription_get(trans, req, self.queue_name, self.id) diff --git a/zaqarclient/tests/queues/subscriptions.py b/zaqarclient/tests/queues/subscriptions.py index 5a5b76ae..dca7fdd5 100644 --- a/zaqarclient/tests/queues/subscriptions.py +++ b/zaqarclient/tests/queues/subscriptions.py @@ -46,6 +46,19 @@ class QueuesV2SubscriptionUnitTest(base.QueuesTestBase): self.assertEqual(3600, subscription.ttl) self.assertEqual('fake_id', subscription.id) + def test_subscription_create_duplicate_throws_conflicterror(self): + subscription_data = {'subscriber': 'http://trigger.me', + 'ttl': 3600} + + with mock.patch.object(self.transport, 'send', + autospec=True) as send_method: + + create_resp = response.Response(None, None, status_code=409) + send_method.return_value = create_resp + + self.assertRaises(errors.ConflictError, self.client.subscription, + 'beijing', **subscription_data) + def test_subscription_update(self): subscription_data = {'subscriber': 'http://trigger.me', 'ttl': 3600} @@ -187,6 +200,11 @@ class QueuesV2SubscriptionFunctionalTest(base.QueuesTestBase): self.assertEqual('http://trigger.he', self.subscription_2.subscriber) self.assertEqual(7200, self.subscription_2.ttl) + def test_subscription_create_duplicate_throws_conflicterror(self): + subscription_data_1 = {'subscriber': 'http://trigger.me', 'ttl': 3600} + self.assertRaises(errors.ConflictError, self.client.subscription, + 'beijing', **subscription_data_1) + def test_subscription_update(self): sub = self.client.subscription(self.queue_name, auto_create=False, **{'id': self.subscription_1.id})