From ea24c49b12cc0bcda061d0315a74e903c5b5f3c1 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 18 Dec 2013 14:36:49 -0800 Subject: [PATCH] Expose the stats attribute on a queue The patch fixes other 2 issues caused by the implementation of Message objects. Co-Authored: Alex Gaynor Change-Id: I0e9abd3e197ecc2d73f16395becd479a3157c6b4 --- marconiclient/queues/v1/core.py | 5 +++ marconiclient/queues/v1/queues.py | 5 +++ marconiclient/tests/queues/queues.py | 59 +++++++++++++++++++++++----- tests/unit/queues/v1/test_core.py | 12 +++++- 4 files changed, 71 insertions(+), 10 deletions(-) diff --git a/marconiclient/queues/v1/core.py b/marconiclient/queues/v1/core.py index a26b2703..fa6fa89f 100644 --- a/marconiclient/queues/v1/core.py +++ b/marconiclient/queues/v1/core.py @@ -87,6 +87,11 @@ def queue_set_metadata(transport, request, name, metadata, callback=None): transport.send(request) +def queue_get_stats(transport, request, name): + return _common_queue_ops('queue_get_stats', transport, + request, name) + + def queue_delete(transport, request, name, callback=None): """Deletes queue.""" return _common_queue_ops('queue_delete', transport, diff --git a/marconiclient/queues/v1/queues.py b/marconiclient/queues/v1/queues.py index 62fbef80..9cc9f7a4 100644 --- a/marconiclient/queues/v1/queues.py +++ b/marconiclient/queues/v1/queues.py @@ -72,6 +72,11 @@ class Queue(object): self._metadata = core.queue_get_metadata(trans, req, self._name) return self._metadata + @property + def stats(self): + req, trans = self.client._request_and_transport() + return core.queue_get_stats(trans, req, self._name) + def delete(self): req, trans = self.client._request_and_transport() core.queue_delete(trans, req, self._name) diff --git a/marconiclient/tests/queues/queues.py b/marconiclient/tests/queues/queues.py index d820f991..f8f8e798 100644 --- a/marconiclient/tests/queues/queues.py +++ b/marconiclient/tests/queues/queues.py @@ -74,6 +74,34 @@ class QueuesV1QueueUnitTest(base.QueuesTestBase): # just checking our way down to the transport # doesn't crash. + def test_queue_stats(self): + result = { + "messages": { + "free": 146929, + "claimed": 2409, + "total": 149338, + "oldest": { + "href": "/v1/queues/qq/messages/50b68a50d6f5b8c8a7c62b01", + "age": 63, + "created": "2013-08-12T20:44:55Z" + }, + "newest": { + "href": "/v1/queues/qq/messages/50b68a50d6f5b8c8a7c62b01", + "age": 12, + "created": "2013-08-12T20:45:46Z" + } + } + } + + with mock.patch.object(self.transport, 'send', + autospec=True) as send_method: + + resp = response.Response(None, json.dumps(result)) + send_method.return_value = resp + + stats = self.queue.stats + self.assertEqual(result, stats) + def test_message_post(self): messages = [{'ttl': 30, 'body': 'Post It!'}] @@ -189,6 +217,19 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase): queue._get_transport = mock.Mock(return_value=self.transport) self.assertFalse(queue.exists()) + def test_queue_stats_functional(self): + messages = [ + {'ttl': 60, 'body': 'Post It!'}, + {'ttl': 60, 'body': 'Post It!'}, + {'ttl': 60, 'body': 'Post It!'}, + ] + + queue = self.client.queue("nonono") + queue._get_transport = mock.Mock(return_value=self.transport) + queue.post(messages) + stats = queue.stats + self.assertEqual(stats["messages"]["free"], 3) + def test_queue_metadata_functional(self): test_metadata = {'type': 'Bank Accounts'} queue = self.client.queue("meta-test") @@ -231,8 +272,8 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase): queue.post(messages) messages = queue.messages() - self.assertTrue(isinstance(messages, list)) - self.assertGreaterEqual(len(messages), 0) + self.assertTrue(isinstance(messages, message._MessageIterator)) + self.assertGreaterEqual(len(list(messages)), 0) def test_message_list_echo_functional(self): queue = self.client.queue("test_queue") @@ -245,8 +286,8 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase): ] queue.post(messages) messages = queue.messages(echo=True) - self.assertTrue(isinstance(messages, list)) - self.assertGreaterEqual(len(messages), 3) + self.assertTrue(isinstance(messages, message._MessageIterator)) + self.assertGreaterEqual(len(list(messages)), 3) def test_message_get_functional(self): queue = self.client.queue("test_queue") @@ -260,9 +301,9 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase): res = queue.post(messages)['resources'] msg_id = res[0].split('/')[-1] - message = queue.message(msg_id) - self.assertTrue(isinstance(message, dict)) - self.assertEqual(message['href'], res[0]) + msg = queue.message(msg_id) + self.assertTrue(isinstance(msg, message.Message)) + self.assertEqual(msg.href, res[0]) def test_message_get_many_functional(self): queue = self.client.queue("test_queue") @@ -280,5 +321,5 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase): res = queue.post(messages)['resources'] msgs_id = [ref.split('/')[-1] for ref in res] messages = queue.messages(*msgs_id) - self.assertTrue(isinstance(messages, list)) - self.assertEqual(len(messages), 1) + self.assertTrue(isinstance(messages, message._MessageIterator)) + self.assertEqual(len(list(messages)), 1) diff --git a/tests/unit/queues/v1/test_core.py b/tests/unit/queues/v1/test_core.py index 35f2a02f..0a958f97 100644 --- a/tests/unit/queues/v1/test_core.py +++ b/tests/unit/queues/v1/test_core.py @@ -19,7 +19,7 @@ import mock from marconiclient.queues.v1 import core from marconiclient.tests import base from marconiclient.tests.transport import dummy -import marconiclient.transport.errors as errors +from marconiclient.transport import errors from marconiclient.transport import request from marconiclient.transport import response @@ -88,6 +88,16 @@ class TestV1Core(base.TestBase): core.queue_exists(self.transport, req, update_data, 'test') self.assertIn('queue_name', req.params) + def test_queue_get_stats(self): + with mock.patch.object(self.transport, 'send', + autospec=True) as send_method: + resp = response.Response(None, '{}') + send_method.return_value = resp + + req = request.Request() + result = core.queue_get_stats(self.transport, req, 'test') + self.assertEqual(result, {}) + def test_message_post(self): messages = [{'ttl': 30, 'body': 'Post It!'}]