Merge "Expose the stats attribute on a queue"

This commit is contained in:
Jenkins 2014-02-07 13:34:56 +00:00 committed by Gerrit Code Review
commit 301ac519a3
4 changed files with 71 additions and 10 deletions

View File

@ -87,6 +87,11 @@ def queue_set_metadata(transport, request, name, metadata, callback=None):
transport.send(request) 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): def queue_delete(transport, request, name, callback=None):
"""Deletes queue.""" """Deletes queue."""
return _common_queue_ops('queue_delete', transport, return _common_queue_ops('queue_delete', transport,

View File

@ -72,6 +72,11 @@ class Queue(object):
self._metadata = core.queue_get_metadata(trans, req, self._name) self._metadata = core.queue_get_metadata(trans, req, self._name)
return self._metadata 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): def delete(self):
req, trans = self.client._request_and_transport() req, trans = self.client._request_and_transport()
core.queue_delete(trans, req, self._name) core.queue_delete(trans, req, self._name)

View File

@ -74,6 +74,34 @@ class QueuesV1QueueUnitTest(base.QueuesTestBase):
# just checking our way down to the transport # just checking our way down to the transport
# doesn't crash. # 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): def test_message_post(self):
messages = [{'ttl': 30, 'body': 'Post It!'}] messages = [{'ttl': 30, 'body': 'Post It!'}]
@ -189,6 +217,19 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase):
queue._get_transport = mock.Mock(return_value=self.transport) queue._get_transport = mock.Mock(return_value=self.transport)
self.assertFalse(queue.exists()) 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): def test_queue_metadata_functional(self):
test_metadata = {'type': 'Bank Accounts'} test_metadata = {'type': 'Bank Accounts'}
queue = self.client.queue("meta-test") queue = self.client.queue("meta-test")
@ -231,8 +272,8 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase):
queue.post(messages) queue.post(messages)
messages = queue.messages() messages = queue.messages()
self.assertTrue(isinstance(messages, list)) self.assertTrue(isinstance(messages, message._MessageIterator))
self.assertGreaterEqual(len(messages), 0) self.assertGreaterEqual(len(list(messages)), 0)
def test_message_list_echo_functional(self): def test_message_list_echo_functional(self):
queue = self.client.queue("test_queue") queue = self.client.queue("test_queue")
@ -245,8 +286,8 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase):
] ]
queue.post(messages) queue.post(messages)
messages = queue.messages(echo=True) messages = queue.messages(echo=True)
self.assertTrue(isinstance(messages, list)) self.assertTrue(isinstance(messages, message._MessageIterator))
self.assertGreaterEqual(len(messages), 3) self.assertGreaterEqual(len(list(messages)), 3)
def test_message_get_functional(self): def test_message_get_functional(self):
queue = self.client.queue("test_queue") queue = self.client.queue("test_queue")
@ -260,9 +301,9 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase):
res = queue.post(messages)['resources'] res = queue.post(messages)['resources']
msg_id = res[0].split('/')[-1] msg_id = res[0].split('/')[-1]
message = queue.message(msg_id) msg = queue.message(msg_id)
self.assertTrue(isinstance(message, dict)) self.assertTrue(isinstance(msg, message.Message))
self.assertEqual(message['href'], res[0]) self.assertEqual(msg.href, res[0])
def test_message_get_many_functional(self): def test_message_get_many_functional(self):
queue = self.client.queue("test_queue") queue = self.client.queue("test_queue")
@ -280,5 +321,5 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase):
res = queue.post(messages)['resources'] res = queue.post(messages)['resources']
msgs_id = [ref.split('/')[-1] for ref in res] msgs_id = [ref.split('/')[-1] for ref in res]
messages = queue.messages(*msgs_id) messages = queue.messages(*msgs_id)
self.assertTrue(isinstance(messages, list)) self.assertTrue(isinstance(messages, message._MessageIterator))
self.assertEqual(len(messages), 1) self.assertEqual(len(list(messages)), 1)

View File

@ -19,7 +19,7 @@ import mock
from marconiclient.queues.v1 import core from marconiclient.queues.v1 import core
from marconiclient.tests import base from marconiclient.tests import base
from marconiclient.tests.transport import dummy 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 request
from marconiclient.transport import response from marconiclient.transport import response
@ -88,6 +88,16 @@ class TestV1Core(base.TestBase):
core.queue_exists(self.transport, req, update_data, 'test') core.queue_exists(self.transport, req, update_data, 'test')
self.assertIn('queue_name', req.params) 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): def test_message_post(self):
messages = [{'ttl': 30, 'body': 'Post It!'}] messages = [{'ttl': 30, 'body': 'Post It!'}]