2014-04-14 23:03:31 +00:00
|
|
|
import unittest
|
|
|
|
import json
|
|
|
|
import time
|
|
|
|
|
2014-05-23 18:16:26 +00:00
|
|
|
from functools import wraps
|
|
|
|
|
2014-05-22 16:13:41 +00:00
|
|
|
from app import app
|
2014-04-14 23:03:31 +00:00
|
|
|
from initdb import setup_database_for_testing, finished_database_for_testing
|
|
|
|
from data.queue import WorkQueue
|
2016-12-06 00:08:20 +00:00
|
|
|
from datetime import timedelta
|
2014-04-14 23:03:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
QUEUE_NAME = 'testqueuename'
|
|
|
|
|
|
|
|
|
2014-05-21 23:50:37 +00:00
|
|
|
class SaveLastCountReporter(object):
|
|
|
|
def __init__(self):
|
2014-05-23 18:16:26 +00:00
|
|
|
self.currently_processing = None
|
|
|
|
self.running_count = None
|
2014-05-21 23:50:37 +00:00
|
|
|
self.total = None
|
|
|
|
|
2014-05-23 18:16:26 +00:00
|
|
|
def __call__(self, currently_processing, running_count, total_jobs):
|
|
|
|
self.currently_processing = currently_processing
|
|
|
|
self.running_count = running_count
|
2014-05-21 23:50:37 +00:00
|
|
|
self.total = total_jobs
|
|
|
|
|
|
|
|
|
2014-05-23 18:16:26 +00:00
|
|
|
class AutoUpdatingQueue(object):
|
|
|
|
def __init__(self, queue_to_wrap):
|
|
|
|
self._queue = queue_to_wrap
|
|
|
|
|
|
|
|
def _wrapper(self, func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
to_return = func(*args, **kwargs)
|
|
|
|
self._queue.update_metrics()
|
|
|
|
return to_return
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
def __getattr__(self, attr_name):
|
|
|
|
method_or_attr = getattr(self._queue, attr_name)
|
|
|
|
if callable(method_or_attr):
|
|
|
|
return self._wrapper(method_or_attr)
|
|
|
|
else:
|
|
|
|
return method_or_attr
|
|
|
|
|
|
|
|
|
2014-04-14 23:03:31 +00:00
|
|
|
class QueueTestCase(unittest.TestCase):
|
|
|
|
TEST_MESSAGE_1 = json.dumps({'data': 1})
|
|
|
|
TEST_MESSAGE_2 = json.dumps({'data': 2})
|
2016-05-24 21:42:11 +00:00
|
|
|
TEST_MESSAGES = [json.dumps({'data': str(i)}) for i in range(1, 101)]
|
2014-04-14 23:03:31 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2014-05-21 23:50:37 +00:00
|
|
|
self.reporter = SaveLastCountReporter()
|
2014-05-22 16:13:41 +00:00
|
|
|
self.transaction_factory = app.config['DB_TRANSACTION_FACTORY']
|
2014-05-23 18:16:26 +00:00
|
|
|
self.queue = AutoUpdatingQueue(WorkQueue(QUEUE_NAME, self.transaction_factory,
|
|
|
|
reporter=self.reporter))
|
2014-04-14 23:03:31 +00:00
|
|
|
setup_database_for_testing(self)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
finished_database_for_testing(self)
|
|
|
|
|
|
|
|
|
|
|
|
class TestQueue(QueueTestCase):
|
|
|
|
def test_same_canonical_names(self):
|
2014-05-23 18:16:26 +00:00
|
|
|
self.assertEqual(self.reporter.currently_processing, None)
|
|
|
|
self.assertEqual(self.reporter.running_count, None)
|
2014-05-21 23:50:37 +00:00
|
|
|
self.assertEqual(self.reporter.total, None)
|
|
|
|
|
2016-07-14 21:11:04 +00:00
|
|
|
self.queue.put(['abc', 'def'], self.TEST_MESSAGE_1, available_after=-1)
|
|
|
|
self.queue.put(['abc', 'def'], self.TEST_MESSAGE_2, available_after=-1)
|
2014-05-23 18:16:26 +00:00
|
|
|
self.assertEqual(self.reporter.currently_processing, False)
|
|
|
|
self.assertEqual(self.reporter.running_count, 0)
|
2014-05-21 23:50:37 +00:00
|
|
|
self.assertEqual(self.reporter.total, 1)
|
2014-04-14 23:03:31 +00:00
|
|
|
|
2016-05-24 21:42:11 +00:00
|
|
|
one = self.queue.get(ordering_required=True)
|
2014-04-14 23:03:31 +00:00
|
|
|
self.assertNotEqual(None, one)
|
|
|
|
self.assertEqual(self.TEST_MESSAGE_1, one.body)
|
2014-05-23 18:16:26 +00:00
|
|
|
self.assertEqual(self.reporter.currently_processing, True)
|
|
|
|
self.assertEqual(self.reporter.running_count, 1)
|
2014-05-21 23:50:37 +00:00
|
|
|
self.assertEqual(self.reporter.total, 1)
|
2014-04-14 23:03:31 +00:00
|
|
|
|
2016-05-24 21:42:11 +00:00
|
|
|
two_fail = self.queue.get(ordering_required=True)
|
2014-04-14 23:03:31 +00:00
|
|
|
self.assertEqual(None, two_fail)
|
2014-05-23 18:16:26 +00:00
|
|
|
self.assertEqual(self.reporter.running_count, 1)
|
2014-05-21 23:50:37 +00:00
|
|
|
self.assertEqual(self.reporter.total, 1)
|
2014-04-14 23:03:31 +00:00
|
|
|
|
|
|
|
self.queue.complete(one)
|
2014-05-23 18:16:26 +00:00
|
|
|
self.assertEqual(self.reporter.currently_processing, False)
|
|
|
|
self.assertEqual(self.reporter.running_count, 0)
|
2014-05-21 23:50:37 +00:00
|
|
|
self.assertEqual(self.reporter.total, 1)
|
2014-04-14 23:03:31 +00:00
|
|
|
|
2016-05-24 21:42:11 +00:00
|
|
|
two = self.queue.get(ordering_required=True)
|
2014-04-14 23:03:31 +00:00
|
|
|
self.assertNotEqual(None, two)
|
2014-05-23 18:16:26 +00:00
|
|
|
self.assertEqual(self.reporter.currently_processing, True)
|
2014-04-14 23:03:31 +00:00
|
|
|
self.assertEqual(self.TEST_MESSAGE_2, two.body)
|
2014-05-23 18:16:26 +00:00
|
|
|
self.assertEqual(self.reporter.running_count, 1)
|
2014-05-21 23:50:37 +00:00
|
|
|
self.assertEqual(self.reporter.total, 1)
|
2014-04-14 23:03:31 +00:00
|
|
|
|
|
|
|
def test_different_canonical_names(self):
|
2016-07-14 21:11:04 +00:00
|
|
|
self.queue.put(['abc', 'def'], self.TEST_MESSAGE_1, available_after=-1)
|
|
|
|
self.queue.put(['abc', 'ghi'], self.TEST_MESSAGE_2, available_after=-1)
|
2014-05-23 18:16:26 +00:00
|
|
|
self.assertEqual(self.reporter.running_count, 0)
|
2014-05-21 23:50:37 +00:00
|
|
|
self.assertEqual(self.reporter.total, 2)
|
2014-04-14 23:03:31 +00:00
|
|
|
|
2016-05-24 21:42:11 +00:00
|
|
|
one = self.queue.get(ordering_required=True)
|
2014-04-14 23:03:31 +00:00
|
|
|
self.assertNotEqual(None, one)
|
|
|
|
self.assertEqual(self.TEST_MESSAGE_1, one.body)
|
2014-05-23 18:16:26 +00:00
|
|
|
self.assertEqual(self.reporter.running_count, 1)
|
2014-05-21 23:50:37 +00:00
|
|
|
self.assertEqual(self.reporter.total, 2)
|
2014-04-14 23:03:31 +00:00
|
|
|
|
2016-05-24 21:42:11 +00:00
|
|
|
two = self.queue.get(ordering_required=True)
|
2014-04-14 23:03:31 +00:00
|
|
|
self.assertNotEqual(None, two)
|
|
|
|
self.assertEqual(self.TEST_MESSAGE_2, two.body)
|
2014-05-23 18:16:26 +00:00
|
|
|
self.assertEqual(self.reporter.running_count, 2)
|
2014-05-21 23:50:37 +00:00
|
|
|
self.assertEqual(self.reporter.total, 2)
|
2014-04-14 23:03:31 +00:00
|
|
|
|
|
|
|
def test_canonical_name(self):
|
2016-07-14 21:11:04 +00:00
|
|
|
self.queue.put(['abc', 'def'], self.TEST_MESSAGE_1, available_after=-1)
|
|
|
|
self.queue.put(['abc', 'def', 'ghi'], self.TEST_MESSAGE_1, available_after=-1)
|
2014-04-14 23:03:31 +00:00
|
|
|
|
2016-05-24 21:42:11 +00:00
|
|
|
one = self.queue.get(ordering_required=True)
|
2014-04-14 23:03:31 +00:00
|
|
|
self.assertNotEqual(QUEUE_NAME + '/abc/def/', one)
|
|
|
|
|
2016-05-24 21:42:11 +00:00
|
|
|
two = self.queue.get(ordering_required=True)
|
2014-04-14 23:03:31 +00:00
|
|
|
self.assertNotEqual(QUEUE_NAME + '/abc/def/ghi/', two)
|
|
|
|
|
|
|
|
def test_expiration(self):
|
2016-07-14 21:11:04 +00:00
|
|
|
self.queue.put(['abc', 'def'], self.TEST_MESSAGE_1, available_after=-1)
|
2014-05-23 18:16:26 +00:00
|
|
|
self.assertEqual(self.reporter.running_count, 0)
|
2014-05-21 23:50:37 +00:00
|
|
|
self.assertEqual(self.reporter.total, 1)
|
2014-04-14 23:03:31 +00:00
|
|
|
|
2016-05-24 21:42:11 +00:00
|
|
|
one = self.queue.get(processing_time=0.5, ordering_required=True)
|
2014-04-14 23:03:31 +00:00
|
|
|
self.assertNotEqual(None, one)
|
2014-05-23 18:16:26 +00:00
|
|
|
self.assertEqual(self.reporter.running_count, 1)
|
2014-05-21 23:50:37 +00:00
|
|
|
self.assertEqual(self.reporter.total, 1)
|
2014-04-14 23:03:31 +00:00
|
|
|
|
2016-05-24 21:42:11 +00:00
|
|
|
one_fail = self.queue.get(ordering_required=True)
|
2014-04-14 23:03:31 +00:00
|
|
|
self.assertEqual(None, one_fail)
|
|
|
|
|
|
|
|
time.sleep(1)
|
2014-05-21 23:50:37 +00:00
|
|
|
self.queue.update_metrics()
|
2014-05-23 18:16:26 +00:00
|
|
|
self.assertEqual(self.reporter.running_count, 0)
|
2014-05-21 23:50:37 +00:00
|
|
|
self.assertEqual(self.reporter.total, 1)
|
2014-04-14 23:03:31 +00:00
|
|
|
|
2016-05-24 21:42:11 +00:00
|
|
|
one_again = self.queue.get(ordering_required=True)
|
2014-04-14 23:03:31 +00:00
|
|
|
self.assertNotEqual(None, one_again)
|
2014-05-23 18:16:26 +00:00
|
|
|
self.assertEqual(self.reporter.running_count, 1)
|
2014-05-21 23:50:37 +00:00
|
|
|
self.assertEqual(self.reporter.total, 1)
|
2014-04-14 23:03:31 +00:00
|
|
|
|
2016-12-06 00:08:20 +00:00
|
|
|
def test_alive(self):
|
|
|
|
# No queue item = not alive.
|
|
|
|
self.assertFalse(self.queue.alive(['abc', 'def']))
|
|
|
|
|
|
|
|
# Add a queue item.
|
|
|
|
self.queue.put(['abc', 'def'], self.TEST_MESSAGE_1, available_after=-1)
|
|
|
|
self.assertTrue(self.queue.alive(['abc', 'def']))
|
|
|
|
|
|
|
|
# Retrieve the queue item.
|
|
|
|
queue_item = self.queue.get()
|
|
|
|
self.assertIsNotNone(queue_item)
|
|
|
|
self.assertTrue(self.queue.alive(['abc', 'def']))
|
|
|
|
|
|
|
|
# Make sure it is running by trying to retrieve it again.
|
|
|
|
self.assertIsNone(self.queue.get())
|
|
|
|
|
|
|
|
# Delete the queue item.
|
|
|
|
self.queue.complete(queue_item)
|
|
|
|
self.assertFalse(self.queue.alive(['abc', 'def']))
|
|
|
|
|
2014-04-14 23:03:31 +00:00
|
|
|
def test_specialized_queue(self):
|
2016-07-14 21:11:04 +00:00
|
|
|
self.queue.put(['abc', 'def'], self.TEST_MESSAGE_1, available_after=-1)
|
|
|
|
self.queue.put(['def', 'def'], self.TEST_MESSAGE_2, available_after=-1)
|
2014-04-14 23:03:31 +00:00
|
|
|
|
2014-05-23 18:16:26 +00:00
|
|
|
my_queue = AutoUpdatingQueue(WorkQueue(QUEUE_NAME, self.transaction_factory, ['def']))
|
2014-04-14 23:03:31 +00:00
|
|
|
|
2016-05-24 21:42:11 +00:00
|
|
|
two = my_queue.get(ordering_required=True)
|
2014-04-14 23:03:31 +00:00
|
|
|
self.assertNotEqual(None, two)
|
|
|
|
self.assertEqual(self.TEST_MESSAGE_2, two.body)
|
|
|
|
|
2016-05-24 21:42:11 +00:00
|
|
|
one_fail = my_queue.get(ordering_required=True)
|
2014-04-14 23:03:31 +00:00
|
|
|
self.assertEqual(None, one_fail)
|
|
|
|
|
2016-05-24 21:42:11 +00:00
|
|
|
one = self.queue.get(ordering_required=True)
|
2014-04-14 23:03:31 +00:00
|
|
|
self.assertNotEqual(None, one)
|
|
|
|
self.assertEqual(self.TEST_MESSAGE_1, one.body)
|
2015-01-29 23:40:41 +00:00
|
|
|
|
2016-05-24 21:42:11 +00:00
|
|
|
def test_random_queue_no_duplicates(self):
|
|
|
|
for msg in self.TEST_MESSAGES:
|
2016-07-14 21:11:04 +00:00
|
|
|
self.queue.put(['abc', 'def'], msg, available_after=-1)
|
2016-05-24 21:42:11 +00:00
|
|
|
seen = set()
|
|
|
|
|
|
|
|
for _ in range(1, 101):
|
|
|
|
item = self.queue.get()
|
|
|
|
json_body = json.loads(item.body)
|
|
|
|
msg = str(json_body['data'])
|
|
|
|
self.assertTrue(msg not in seen)
|
|
|
|
seen.add(msg)
|
|
|
|
|
|
|
|
for body in self.TEST_MESSAGES:
|
|
|
|
json_body = json.loads(body)
|
|
|
|
msg = str(json_body['data'])
|
|
|
|
self.assertIn(msg, seen)
|
|
|
|
|
2015-01-29 23:40:41 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|