2023-05-05 07:25:20 +00:00
|
|
|
from redis import ConnectionPool, Redis, SSLConnection, UnixDomainSocketConnection
|
2020-01-31 12:28:32 +00:00
|
|
|
|
2023-04-25 12:38:44 +00:00
|
|
|
from rq.connections import parse_connection
|
2024-02-21 00:37:15 +00:00
|
|
|
from tests import RQTestCase
|
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
|
|
|
|
|
|
|
|
|
|
|
class TestConnectionInheritance(RQTestCase):
|
2023-04-25 12:38:44 +00:00
|
|
|
def test_parse_connection(self):
|
2023-05-05 07:25:20 +00:00
|
|
|
"""Test parsing the connection"""
|
|
|
|
conn_class, pool_class, pool_kwargs = parse_connection(Redis(ssl=True))
|
|
|
|
self.assertEqual(conn_class, Redis)
|
|
|
|
self.assertEqual(pool_class, SSLConnection)
|
|
|
|
|
2023-04-25 12:38:44 +00:00
|
|
|
path = '/tmp/redis.sock'
|
|
|
|
pool = ConnectionPool(connection_class=UnixDomainSocketConnection, path=path)
|
2023-05-05 07:25:20 +00:00
|
|
|
conn_class, pool_class, pool_kwargs = parse_connection(Redis(connection_pool=pool))
|
|
|
|
self.assertEqual(conn_class, Redis)
|
|
|
|
self.assertEqual(pool_class, UnixDomainSocketConnection)
|
2024-11-20 08:34:27 +00:00
|
|
|
self.assertEqual(pool_kwargs, {'path': path})
|