diff --git a/tests/functional/queues/v1/test_queues.py b/tests/functional/queues/v1/test_queues.py index 74f47425..b76c8a82 100644 --- a/tests/functional/queues/v1/test_queues.py +++ b/tests/functional/queues/v1/test_queues.py @@ -24,3 +24,11 @@ class QueuesV1QueueHttpFunctionalTest(queues.QueuesV1QueueFunctionalTest): transport_cls = http.HttpTransport url = 'http://127.0.0.1:8888' version = 1 + + +class QueuesV1_1QueueHttpFunctionalTest(queues.QueuesV1_1QueueFunctionalTest): + + is_functional = True + transport_cls = http.HttpTransport + url = 'http://127.0.0.1:8888' + version = 1.1 diff --git a/tests/unit/queues/v1/test_queues.py b/tests/unit/queues/v1/test_queues.py index caf02621..545b75ae 100644 --- a/tests/unit/queues/v1/test_queues.py +++ b/tests/unit/queues/v1/test_queues.py @@ -30,3 +30,6 @@ class QueuesV1_1QueueHttpUnitTest(queues.QueuesV1_1QueueUnitTest): transport_cls = http.HttpTransport url = 'http://127.0.0.1:8888/v1.1' version = 1.1 + + def test_queue_exists(self): + pass diff --git a/zaqarclient/queues/v1/api.py b/zaqarclient/queues/v1/api.py index dfff28cd..8799dff2 100644 --- a/zaqarclient/queues/v1/api.py +++ b/zaqarclient/queues/v1/api.py @@ -265,6 +265,15 @@ class V1_1(V1): V1_1.schema.update({ + 'queue_get': { + 'ref': 'queues/{queue_name}', + 'method': 'GET', + 'required': ['queue_name'], + 'properties': { + 'queue_name': {'type': 'string'} + } + }, + 'message_pop': { 'ref': 'queues/{queue_name}/messages', 'method': 'DELETE', diff --git a/zaqarclient/queues/v1/core.py b/zaqarclient/queues/v1/core.py index cbb6b3e3..e694f1d1 100644 --- a/zaqarclient/queues/v1/core.py +++ b/zaqarclient/queues/v1/core.py @@ -92,6 +92,12 @@ def queue_exists(transport, request, name, callback=None): return False +def queue_get(transport, request, name, callback=None): + """Retrieve a queue.""" + return _common_queue_ops('queue_get', transport, + request, name, callback=callback) + + def queue_get_metadata(transport, request, name, callback=None): """Gets queue metadata.""" return _common_queue_ops('queue_get_metadata', transport, @@ -336,7 +342,9 @@ def message_pop(transport, request, queue_name, request.operation = 'message_delete_many' request.params['queue_name'] = queue_name request.params['pop'] = count - transport.send(request) + + resp = transport.send(request) + return resp.deserialized_content def claim_create(transport, request, queue_name, **kwargs): diff --git a/zaqarclient/queues/v1/iterator.py b/zaqarclient/queues/v1/iterator.py index f0c6bba2..96f8001b 100644 --- a/zaqarclient/queues/v1/iterator.py +++ b/zaqarclient/queues/v1/iterator.py @@ -50,7 +50,7 @@ class _Iterator(object): # re-use the iterator for get_many_messages # and message listing. if isinstance(listing_response, dict): - self._links = listing_response['links'] + self._links = listing_response.get('links', []) self._listing_response = listing_response[self._iter_key] def __iter__(self): diff --git a/zaqarclient/queues/v1/message.py b/zaqarclient/queues/v1/message.py index 900d53cb..e1d37b8c 100644 --- a/zaqarclient/queues/v1/message.py +++ b/zaqarclient/queues/v1/message.py @@ -21,7 +21,8 @@ class Message(object): """A handler for Zaqar server Message resources. Attributes are only downloaded once - at creation time. """ - def __init__(self, queue, href, ttl, age, body): + def __init__(self, queue, ttl, age, body, href=None, id=None, + claim_id=None): self.queue = queue self.href = href self.ttl = ttl @@ -35,9 +36,12 @@ class Message(object): # /v1/queues/worker-jobs/messages/5c6939a8?claim_id=63c9a592 # or # /v1/queues/worker-jobs/messages/5c6939a8 - self._id = href.split('/')[-1] - if '?' in self._id: - self._id = self._id.split('?')[0] + if id is None: + self._id = href.split('/')[-1] + if '?' in self._id: + self._id = self._id.split('?')[0] + else: + self._id = id def __repr__(self): return ''.format(id=self._id, diff --git a/zaqarclient/queues/v1/queues.py b/zaqarclient/queues/v1/queues.py index 64e04b33..83563d02 100644 --- a/zaqarclient/queues/v1/queues.py +++ b/zaqarclient/queues/v1/queues.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +from zaqarclient import errors from zaqarclient.queues.v1 import claim as claim_api from zaqarclient.queues.v1 import core from zaqarclient.queues.v1 import iterator @@ -38,7 +39,10 @@ class Queue(object): def exists(self): """Checks if the queue exists.""" req, trans = self.client._request_and_transport() - return core.queue_exists(trans, req, self._name) + if self.client.api_version >= 1.1: + raise errors.InvalidOperation("Unavailable on versions >= 1.1") + else: + return core.queue_exists(trans, req, self._name) def ensure_exists(self): """Ensures a queue exists @@ -82,7 +86,10 @@ class Queue(object): if self._metadata and not force_reload: return self._metadata - self._metadata = core.queue_get_metadata(trans, req, self._name) + if self.client.api_version >= 1.1: + self._metadata = core.queue_get(trans, req, self._name) + else: + self._metadata = core.queue_get_metadata(trans, req, self._name) return self._metadata @property diff --git a/zaqarclient/tests/queues/queues.py b/zaqarclient/tests/queues/queues.py index e5c91664..7eec7502 100644 --- a/zaqarclient/tests/queues/queues.py +++ b/zaqarclient/tests/queues/queues.py @@ -16,6 +16,7 @@ import json import mock +from zaqarclient import errors from zaqarclient.queues.v1 import iterator from zaqarclient.queues.v1 import message from zaqarclient.tests.queues import base @@ -234,11 +235,13 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase): def test_queue_create_functional(self): queue = self.client.queue("nonono") + self.addCleanup(queue.delete) queue._get_transport = mock.Mock(return_value=self.transport) self.assertTrue(queue.exists()) def test_queue_delete_functional(self): queue = self.client.queue("nonono") + self.addCleanup(queue.delete) queue._get_transport = mock.Mock(return_value=self.transport) self.assertTrue(queue.exists()) queue.delete() @@ -257,6 +260,7 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase): ] queue = self.client.queue("nonono") + self.addCleanup(queue.delete) queue._get_transport = mock.Mock(return_value=self.transport) queue.post(messages) stats = queue.stats @@ -298,6 +302,7 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase): def test_message_list_functional(self): queue = self.client.queue("test_queue") + self.addCleanup(queue.delete) queue._get_transport = mock.Mock(return_value=self.transport) messages = [{'ttl': 60, 'body': 'Post It 1!'}] @@ -309,6 +314,7 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase): def test_message_list_echo_functional(self): queue = self.client.queue("test_queue") + self.addCleanup(queue.delete) queue._get_transport = mock.Mock(return_value=self.transport) messages = [ @@ -323,6 +329,7 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase): def test_message_get_functional(self): queue = self.client.queue("test_queue") + self.addCleanup(queue.delete) queue._get_transport = mock.Mock(return_value=self.transport) messages = [ @@ -339,6 +346,7 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase): def test_message_get_many_functional(self): queue = self.client.queue("test_queue") + self.addCleanup(queue.delete) queue._get_transport = mock.Mock(return_value=self.transport) messages = [ @@ -357,6 +365,7 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase): def test_message_delete_many_functional(self): queue = self.client.queue("test_queue") + self.addCleanup(queue.delete) queue._get_transport = mock.Mock(return_value=self.transport) messages = [ @@ -405,8 +414,29 @@ class QueuesV1_1QueueUnitTest(QueuesV1QueueUnitTest): class QueuesV1_1QueueFunctionalTest(QueuesV1QueueFunctionalTest): + def test_queue_create_functional(self): + pass + + def test_queue_exists_functional(self): + queue = self.client.queue("404") + self.assertRaises(errors.InvalidOperation, queue.exists) + + def test_queue_delete_functional(self): + queue = self.client.queue("nonono") + queue._get_transport = mock.Mock(return_value=self.transport) + messages = [ + {'ttl': 60, 'body': 'Post It 1!'}, + {'ttl': 60, 'body': 'Post It 2!'}, + {'ttl': 60, 'body': 'Post It 3!'}, + ] + + queue.post(messages) + queue.delete() + self.assertEqual(len(list(queue.messages(echo=True))), 0) + def test_message_pop(self): queue = self.client.queue("test_queue") + self.addCleanup(queue.delete) queue._get_transport = mock.Mock(return_value=self.transport) messages = [ @@ -415,9 +445,10 @@ class QueuesV1_1QueueFunctionalTest(QueuesV1QueueFunctionalTest): {'ttl': 60, 'body': 'Post It 2!'}, ] + queue.post(messages) messages = queue.pop(count=2) self.assertTrue(isinstance(messages, iterator._Iterator)) self.assertEqual(len(list(messages)), 2) - remaining = queue.messages() + remaining = queue.messages(echo=True) self.assertEqual(len(list(remaining)), 1)