2014-05-05 08:49:29 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import (absolute_import, division, print_function,
|
|
|
|
unicode_literals)
|
|
|
|
|
2020-01-31 12:28:32 +00:00
|
|
|
from redis import Redis
|
|
|
|
|
2017-08-31 11:23:17 +00:00
|
|
|
from rq import Connection, Queue, use_connection, get_current_connection, pop_connection
|
|
|
|
from rq.connections import NoRedisConnectionException
|
2014-05-05 08:49:29 +00:00
|
|
|
|
|
|
|
from tests import find_empty_redis_database, 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
|
|
|
from tests.fixtures import do_nothing
|
|
|
|
|
|
|
|
|
|
|
|
def new_connection():
|
|
|
|
return find_empty_redis_database()
|
|
|
|
|
|
|
|
|
|
|
|
class TestConnectionInheritance(RQTestCase):
|
|
|
|
def test_connection_detection(self):
|
|
|
|
"""Automatic detection of the connection."""
|
|
|
|
q = Queue()
|
2015-11-09 04:34:27 +00:00
|
|
|
self.assertEqual(q.connection, self.testconn)
|
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
|
|
|
|
|
|
|
def test_connection_stacking(self):
|
|
|
|
"""Connection stacking."""
|
2020-01-31 12:28:32 +00:00
|
|
|
conn1 = Redis(db=4)
|
|
|
|
conn2 = Redis(db=5)
|
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
|
|
|
|
|
|
|
with Connection(conn1):
|
|
|
|
q1 = Queue()
|
|
|
|
with Connection(conn2):
|
|
|
|
q2 = Queue()
|
2015-11-09 04:34:27 +00:00
|
|
|
self.assertNotEqual(q1.connection, q2.connection)
|
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
|
|
|
|
|
|
|
def test_connection_pass_thru(self):
|
|
|
|
"""Connection passed through from queues to jobs."""
|
|
|
|
q1 = Queue()
|
|
|
|
with Connection(new_connection()):
|
|
|
|
q2 = Queue()
|
|
|
|
job1 = q1.enqueue(do_nothing)
|
|
|
|
job2 = q2.enqueue(do_nothing)
|
2015-11-09 04:34:27 +00:00
|
|
|
self.assertEqual(q1.connection, job1.connection)
|
|
|
|
self.assertEqual(q2.connection, job2.connection)
|
2017-08-31 11:23:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestConnectionHelpers(RQTestCase):
|
|
|
|
def test_use_connection(self):
|
|
|
|
"""Test function use_connection works as expected."""
|
|
|
|
conn = new_connection()
|
|
|
|
use_connection(conn)
|
|
|
|
|
|
|
|
self.assertEqual(conn, get_current_connection())
|
|
|
|
|
|
|
|
use_connection()
|
|
|
|
|
|
|
|
self.assertNotEqual(conn, get_current_connection())
|
|
|
|
|
|
|
|
use_connection(self.testconn) # Restore RQTestCase connection
|
|
|
|
|
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
with Connection(new_connection()):
|
|
|
|
use_connection()
|
|
|
|
with Connection(new_connection()):
|
|
|
|
use_connection()
|
|
|
|
|
|
|
|
def test_resolve_connection_raises_on_no_connection(self):
|
|
|
|
"""Test function resolve_connection raises if there is no connection."""
|
|
|
|
pop_connection()
|
|
|
|
with self.assertRaises(NoRedisConnectionException):
|
|
|
|
Queue()
|