|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import (absolute_import, division, print_function,
|
|
|
|
unicode_literals)
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from redis import Redis
|
|
|
|
from rq import pop_connection, push_connection
|
|
|
|
from rq.compat import is_python_version
|
|
|
|
|
|
|
|
if is_python_version((2, 7), (3, 2)):
|
|
|
|
import unittest
|
|
|
|
else:
|
|
|
|
import unittest2 as unittest # noqa
|
|
|
|
|
|
|
|
|
|
|
|
def find_empty_redis_database():
|
|
|
|
"""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):
|
|
|
|
testconn = Redis(db=dbnum)
|
|
|
|
empty = testconn.dbsize() == 0
|
|
|
|
if empty:
|
|
|
|
return testconn
|
|
|
|
assert False, 'No empty Redis database found to run tests in.'
|
|
|
|
|
|
|
|
|
|
|
|
def slow(f):
|
|
|
|
import os
|
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
@wraps(f)
|
|
|
|
def _inner(*args, **kwargs):
|
|
|
|
if os.environ.get('RUN_SLOW_TESTS_TOO'):
|
|
|
|
f(*args, **kwargs)
|
|
|
|
|
|
|
|
return _inner
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
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())
13 years ago
|
|
|
push_connection(testconn)
|
|
|
|
|
|
|
|
# Store the connection (for sanity checking)
|
|
|
|
cls.testconn = testconn
|
|
|
|
|
|
|
|
# Shut up logging
|
|
|
|
logging.disable(logging.ERROR)
|
|
|
|
|
|
|
|
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())
13 years ago
|
|
|
self.testconn.flushdb()
|
|
|
|
|
|
|
|
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())
13 years ago
|
|
|
self.testconn.flushdb()
|
|
|
|
|
|
|
|
# Implement assertIsNotNone for Python runtimes < 2.7 or < 3.1
|
|
|
|
if not hasattr(unittest.TestCase, 'assertIsNotNone'):
|
|
|
|
def assertIsNotNone(self, value, *args): # noqa
|
|
|
|
self.assertNotEqual(value, None, *args)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
# 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())
13 years ago
|
|
|
testconn = pop_connection()
|
|
|
|
assert testconn == cls.testconn, \
|
|
|
|
'Wow, something really nasty happened to the Redis connection stack. Check your setup.'
|