2012-09-02 09:26:31 +00:00
|
|
|
import logging
|
2020-11-27 00:27:30 +00:00
|
|
|
import os
|
2014-05-05 08:49:29 +00:00
|
|
|
|
2018-12-03 00:28:36 +00:00
|
|
|
from redis import Redis
|
2014-05-05 08:49:29 +00:00
|
|
|
from rq import pop_connection, push_connection
|
|
|
|
|
2021-06-22 03:30:46 +00:00
|
|
|
import unittest
|
2012-09-02 09:26:31 +00:00
|
|
|
|
2012-02-24 10:36:33 +00:00
|
|
|
|
2020-11-27 00:27:30 +00:00
|
|
|
def find_empty_redis_database(ssl=False):
|
2012-02-13 08:06:39 +00:00
|
|
|
"""Tries to connect to a random Redis database (starting from 4), and
|
|
|
|
will use/connect it when no keys are in there.
|
|
|
|
"""
|
|
|
|
for dbnum in range(4, 17):
|
2021-06-22 03:30:46 +00:00
|
|
|
connection_kwargs = {'db': dbnum}
|
2020-11-27 00:27:30 +00:00
|
|
|
if ssl:
|
|
|
|
connection_kwargs['port'] = 9736
|
|
|
|
connection_kwargs['ssl'] = True
|
2021-06-22 03:30:46 +00:00
|
|
|
connection_kwargs['ssl_cert_reqs'] = None # disable certificate validation
|
2020-11-27 00:27:30 +00:00
|
|
|
testconn = Redis(**connection_kwargs)
|
2018-05-01 00:20:45 +00:00
|
|
|
empty = testconn.dbsize() == 0
|
2012-02-13 08:06:39 +00:00
|
|
|
if empty:
|
|
|
|
return testconn
|
|
|
|
assert False, 'No empty Redis database found to run tests in.'
|
|
|
|
|
|
|
|
|
2012-02-24 06:39:44 +00:00
|
|
|
def slow(f):
|
2020-11-27 00:27:30 +00:00
|
|
|
return unittest.skipUnless(os.environ.get('RUN_SLOW_TESTS_TOO'), "Slow tests disabled")(f)
|
2012-02-24 06:39:44 +00:00
|
|
|
|
2021-06-22 03:30:46 +00:00
|
|
|
|
2020-11-27 00:27:30 +00:00
|
|
|
def ssl_test(f):
|
|
|
|
return unittest.skipUnless(os.environ.get('RUN_SSL_TESTS'), "SSL tests disabled")(f)
|
2012-02-24 06:39:44 +00:00
|
|
|
|
2021-06-22 03:30:46 +00:00
|
|
|
|
2012-01-28 06:58:40 +00:00
|
|
|
class RQTestCase(unittest.TestCase):
|
|
|
|
"""Base class to inherit test cases from for RQ.
|
|
|
|
|
|
|
|
It sets up the Redis connection (available via self.testconn), turns off
|
|
|
|
logging to the terminal and flushes the Redis database before and after
|
|
|
|
running each test.
|
|
|
|
|
|
|
|
Also offers assertQueueContains(queue, that_func) assertion method.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
# Set up connection to Redis
|
2012-02-13 08:06:39 +00:00
|
|
|
testconn = find_empty_redis_database()
|
New connection management.
Connections can now be set explicitly on Queues, Workers, and Jobs.
Jobs that are implicitly created by Queue or Worker API calls now
inherit the connection of their creator's.
For all RQ object instances that are created now holds that the
"current" connection is used if none is passed in explicitly. The
"current" connection is thus hold on to at creation time and won't be
changed for the lifetime of the object.
Effectively, this means that, given a default Redis connection, say you
create a queue Q1, then push another Redis connection onto the
connection stack, then create Q2. In that case, Q1 means a queue on the
first connection and Q2 on the second connection.
This is way more clear than it used to be.
Also, I've removed the `use_redis()` call, which was named ugly.
Instead, some new alternatives for connection management now exist.
You can push/pop connections now:
>>> my_conn = Redis()
>>> push_connection(my_conn)
>>> q = Queue()
>>> q.connection == my_conn
True
>>> pop_connection() == my_conn
Also, you can stack them syntactically:
>>> conn1 = Redis()
>>> conn2 = Redis('example.org', 1234)
>>> with Connection(conn1):
... q = Queue()
... with Connection(conn2):
... q2 = Queue()
... q3 = Queue()
>>> q.connection == conn1
True
>>> q2.connection == conn2
True
>>> q3.connection == conn1
True
Or, if you only require a single connection to Redis (for most uses):
>>> use_connection(Redis())
2012-03-23 13:33:49 +00:00
|
|
|
push_connection(testconn)
|
2012-01-28 06:58:40 +00:00
|
|
|
|
|
|
|
# Store the connection (for sanity checking)
|
|
|
|
cls.testconn = testconn
|
2022-11-19 08:17:35 +00:00
|
|
|
cls.connection = testconn
|
2012-01-28 06:58:40 +00:00
|
|
|
|
2012-09-02 09:26:31 +00:00
|
|
|
# Shut up logging
|
2013-02-14 14:53:58 +00:00
|
|
|
logging.disable(logging.ERROR)
|
2012-01-28 06:58:40 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# Flush beforewards (we like our hygiene)
|
New connection management.
Connections can now be set explicitly on Queues, Workers, and Jobs.
Jobs that are implicitly created by Queue or Worker API calls now
inherit the connection of their creator's.
For all RQ object instances that are created now holds that the
"current" connection is used if none is passed in explicitly. The
"current" connection is thus hold on to at creation time and won't be
changed for the lifetime of the object.
Effectively, this means that, given a default Redis connection, say you
create a queue Q1, then push another Redis connection onto the
connection stack, then create Q2. In that case, Q1 means a queue on the
first connection and Q2 on the second connection.
This is way more clear than it used to be.
Also, I've removed the `use_redis()` call, which was named ugly.
Instead, some new alternatives for connection management now exist.
You can push/pop connections now:
>>> my_conn = Redis()
>>> push_connection(my_conn)
>>> q = Queue()
>>> q.connection == my_conn
True
>>> pop_connection() == my_conn
Also, you can stack them syntactically:
>>> conn1 = Redis()
>>> conn2 = Redis('example.org', 1234)
>>> with Connection(conn1):
... q = Queue()
... with Connection(conn2):
... q2 = Queue()
... q3 = Queue()
>>> q.connection == conn1
True
>>> q2.connection == conn2
True
>>> q3.connection == conn1
True
Or, if you only require a single connection to Redis (for most uses):
>>> use_connection(Redis())
2012-03-23 13:33:49 +00:00
|
|
|
self.testconn.flushdb()
|
2012-01-28 06:58:40 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# Flush afterwards
|
New connection management.
Connections can now be set explicitly on Queues, Workers, and Jobs.
Jobs that are implicitly created by Queue or Worker API calls now
inherit the connection of their creator's.
For all RQ object instances that are created now holds that the
"current" connection is used if none is passed in explicitly. The
"current" connection is thus hold on to at creation time and won't be
changed for the lifetime of the object.
Effectively, this means that, given a default Redis connection, say you
create a queue Q1, then push another Redis connection onto the
connection stack, then create Q2. In that case, Q1 means a queue on the
first connection and Q2 on the second connection.
This is way more clear than it used to be.
Also, I've removed the `use_redis()` call, which was named ugly.
Instead, some new alternatives for connection management now exist.
You can push/pop connections now:
>>> my_conn = Redis()
>>> push_connection(my_conn)
>>> q = Queue()
>>> q.connection == my_conn
True
>>> pop_connection() == my_conn
Also, you can stack them syntactically:
>>> conn1 = Redis()
>>> conn2 = Redis('example.org', 1234)
>>> with Connection(conn1):
... q = Queue()
... with Connection(conn2):
... q2 = Queue()
... q3 = Queue()
>>> q.connection == conn1
True
>>> q2.connection == conn2
True
>>> q3.connection == conn1
True
Or, if you only require a single connection to Redis (for most uses):
>>> use_connection(Redis())
2012-03-23 13:33:49 +00:00
|
|
|
self.testconn.flushdb()
|
2012-01-28 06:58:40 +00:00
|
|
|
|
2012-05-18 06:35:23 +00:00
|
|
|
# Implement assertIsNotNone for Python runtimes < 2.7 or < 3.1
|
|
|
|
if not hasattr(unittest.TestCase, 'assertIsNotNone'):
|
2017-09-08 09:28:10 +00:00
|
|
|
def assertIsNotNone(self, value, *args): # noqa
|
2012-05-18 06:35:23 +00:00
|
|
|
self.assertNotEqual(value, None, *args)
|
|
|
|
|
2012-01-28 06:58:40 +00:00
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2012-09-02 09:26:31 +00:00
|
|
|
logging.disable(logging.NOTSET)
|
2012-01-28 06:58:40 +00:00
|
|
|
|
|
|
|
# Pop the connection to Redis
|
New connection management.
Connections can now be set explicitly on Queues, Workers, and Jobs.
Jobs that are implicitly created by Queue or Worker API calls now
inherit the connection of their creator's.
For all RQ object instances that are created now holds that the
"current" connection is used if none is passed in explicitly. The
"current" connection is thus hold on to at creation time and won't be
changed for the lifetime of the object.
Effectively, this means that, given a default Redis connection, say you
create a queue Q1, then push another Redis connection onto the
connection stack, then create Q2. In that case, Q1 means a queue on the
first connection and Q2 on the second connection.
This is way more clear than it used to be.
Also, I've removed the `use_redis()` call, which was named ugly.
Instead, some new alternatives for connection management now exist.
You can push/pop connections now:
>>> my_conn = Redis()
>>> push_connection(my_conn)
>>> q = Queue()
>>> q.connection == my_conn
True
>>> pop_connection() == my_conn
Also, you can stack them syntactically:
>>> conn1 = Redis()
>>> conn2 = Redis('example.org', 1234)
>>> with Connection(conn1):
... q = Queue()
... with Connection(conn2):
... q2 = Queue()
... q3 = Queue()
>>> q.connection == conn1
True
>>> q2.connection == conn2
True
>>> q3.connection == conn1
True
Or, if you only require a single connection to Redis (for most uses):
>>> use_connection(Redis())
2012-03-23 13:33:49 +00:00
|
|
|
testconn = pop_connection()
|
2014-05-05 08:49:43 +00:00
|
|
|
assert testconn == cls.testconn, \
|
|
|
|
'Wow, something really nasty happened to the Redis connection stack. Check your setup.'
|