| 1 |
import sys |
| 2 |
from unittest import mock, skipIf |
| 3 |
from unittest.mock import MagicMock, patch |
| 4 |
|
| 5 |
from django.conf import settings |
| 6 |
from django.test import TestCase, override_settings |
| 7 |
|
| 8 |
from django_rq.connection_utils import ( |
| 9 |
get_connection, |
| 10 |
get_connection_by_index, |
| 11 |
get_redis_connection, |
| 12 |
get_unique_connection_configs, |
| 13 |
) |
| 14 |
from django_rq.queues import get_queue |
| 15 |
from tests.base import DjangoRQTestCase |
| 16 |
from tests.fixtures import access_self |
| 17 |
from tests.redis_config import REDIS_CONFIG_1, REDIS_CONFIG_2, REDIS_CONFIG_3 |
| 18 |
|
| 19 |
QUEUES = settings.RQ_QUEUES |
| 20 |
|
| 21 |
|
| 22 |
@override_settings(RQ={'AUTOCOMMIT': True}) |
| 23 |
class ConnectionTest(DjangoRQTestCase): |
| 24 |
def test_get_connection_default(self): |
| 25 |
""" |
| 26 |
Test that get_connection returns the right connection based for |
| 27 |
`default` queue. |
| 28 |
""" |
| 29 |
config = QUEUES['default'] |
| 30 |
connection = get_connection() |
| 31 |
self.assert_connection_kwargs(connection, config) |
| 32 |
|
| 33 |
def test_get_connection_test(self): |
| 34 |
""" |
| 35 |
Test that get_connection returns the right connection based for |
| 36 |
`test` queue. |
| 37 |
""" |
| 38 |
config = QUEUES['test'] |
| 39 |
connection = get_connection('test') |
| 40 |
self.assert_connection_kwargs(connection, config) |
| 41 |
|
| 42 |
@patch('django_rq.connection_utils.Sentinel') |
| 43 |
def test_get_connection_sentinel(self, sentinel_class_mock): |
| 44 |
""" |
| 45 |
Test that get_connection returns the right connection based for |
| 46 |
`sentinel` queue. |
| 47 |
""" |
| 48 |
sentinel_mock = MagicMock() |
| 49 |
sentinel_mock.master_for.return_value = sentinel_mock |
| 50 |
sentinel_class_mock.side_effect = [sentinel_mock] |
| 51 |
|
| 52 |
config = QUEUES['sentinel'] |
| 53 |
connection = get_connection('sentinel') |
| 54 |
|
| 55 |
self.assertEqual(connection, sentinel_mock) |
| 56 |
self.assertEqual(sentinel_mock.master_for.call_count, 1) |
| 57 |
self.assertEqual(sentinel_class_mock.call_count, 1) |
| 58 |
|
| 59 |
sentinel_instances = sentinel_class_mock.call_args[0][0] |
| 60 |
self.assertListEqual(config['SENTINELS'], sentinel_instances) |
| 61 |
|
| 62 |
connection_kwargs = sentinel_mock.master_for.call_args[1] |
| 63 |
self.assertEqual(connection_kwargs['service_name'], config['MASTER_NAME']) |
| 64 |
|
| 65 |
@patch('django_rq.connection_utils.Sentinel') |
| 66 |
def test_sentinel_class_initialized_with_kw_args(self, sentinel_class_mock): |
| 67 |
""" |
| 68 |
Test that Sentinel object is initialized with proper connection kwargs. |
| 69 |
""" |
| 70 |
config = { |
| 71 |
'SENTINELS': [], |
| 72 |
'MASTER_NAME': 'test_master', |
| 73 |
'SOCKET_TIMEOUT': 0.2, |
| 74 |
'DB': 0, |
| 75 |
'USERNAME': 'redis-user', |
| 76 |
'PASSWORD': 'redis-pass', |
| 77 |
'CONNECTION_KWARGS': {'ssl': False}, |
| 78 |
'SENTINEL_KWARGS': {'username': 'sentinel-user', 'password': 'sentinel-pass', 'socket_timeout': 0.3}, |
| 79 |
} |
| 80 |
get_redis_connection(config) |
| 81 |
sentinel_init_sentinel_kwargs = sentinel_class_mock.call_args[1] |
| 82 |
self.assertDictEqual( |
| 83 |
sentinel_init_sentinel_kwargs, |
| 84 |
{ |
| 85 |
'db': 0, |
| 86 |
'username': 'redis-user', |
| 87 |
'password': 'redis-pass', |
| 88 |
'socket_timeout': 0.2, |
| 89 |
'ssl': False, |
| 90 |
'sentinel_kwargs': {'username': 'sentinel-user', 'password': 'sentinel-pass', 'socket_timeout': 0.3}, |
| 91 |
}, |
| 92 |
) |
| 93 |
|
| 94 |
def test_get_unique_connection_configs(self): |
| 95 |
connection_params_1 = { |
| 96 |
'HOST': REDIS_CONFIG_1.host, |
| 97 |
'PORT': REDIS_CONFIG_1.port, |
| 98 |
'DB': REDIS_CONFIG_1.db, |
| 99 |
} |
| 100 |
connection_params_2 = { |
| 101 |
'HOST': REDIS_CONFIG_2.host, |
| 102 |
'PORT': REDIS_CONFIG_2.port, |
| 103 |
'DB': REDIS_CONFIG_2.db, |
| 104 |
} |
| 105 |
config = {'default': connection_params_1, 'test': connection_params_2} |
| 106 |
unique_configs = get_unique_connection_configs(config) |
| 107 |
self.assertEqual(len(unique_configs), 2) |
| 108 |
self.assertIn(connection_params_1, unique_configs) |
| 109 |
self.assertIn(connection_params_2, unique_configs) |
| 110 |
|
| 111 |
# self.assertEqual(get_unique_connection_configs(config), |
| 112 |
# [connection_params_1, connection_params_2]) |
| 113 |
config = {'default': connection_params_1, 'test': connection_params_1} |
| 114 |
# Should return one connection config since it filters out duplicates |
| 115 |
self.assertEqual(get_unique_connection_configs(config), [connection_params_1]) |
| 116 |
|
| 117 |
def test_get_unique_connection_configs_with_different_timeout(self): |
| 118 |
connection_params_1 = { |
| 119 |
'HOST': REDIS_CONFIG_1.host, |
| 120 |
'PORT': REDIS_CONFIG_1.port, |
| 121 |
'DB': REDIS_CONFIG_1.db, |
| 122 |
} |
| 123 |
connection_params_2 = { |
| 124 |
'HOST': REDIS_CONFIG_2.host, |
| 125 |
'PORT': REDIS_CONFIG_2.port, |
| 126 |
'DB': REDIS_CONFIG_2.db, |
| 127 |
} |
| 128 |
queue_params_a = dict(connection_params_1) |
| 129 |
queue_params_b = dict(connection_params_2) |
| 130 |
queue_params_c = dict(connection_params_2) |
| 131 |
queue_params_c["DEFAULT_TIMEOUT"] = 1 |
| 132 |
config = { |
| 133 |
'default': queue_params_a, |
| 134 |
'test_b': queue_params_b, |
| 135 |
'test_c': queue_params_c, |
| 136 |
} |
| 137 |
unique_configs = get_unique_connection_configs(config) |
| 138 |
self.assertEqual(len(unique_configs), 2) |
| 139 |
self.assertIn(connection_params_1, unique_configs) |
| 140 |
self.assertIn(connection_params_2, unique_configs) |
| 141 |
|
| 142 |
def test_get_unique_connection_configs_deterministic_order(self): |
| 143 |
""" |
| 144 |
Test that get_unique_connection_configs returns configs in deterministic order |
| 145 |
based on sorted queue names, regardless of input dict key order. |
| 146 |
This allows using connection_index reliably. |
| 147 |
""" |
| 148 |
import random |
| 149 |
|
| 150 |
connection_params_1 = { |
| 151 |
'HOST': REDIS_CONFIG_1.host, |
| 152 |
'PORT': REDIS_CONFIG_1.port, |
| 153 |
'DB': REDIS_CONFIG_1.db, |
| 154 |
} |
| 155 |
connection_params_2 = { |
| 156 |
'HOST': REDIS_CONFIG_2.host, |
| 157 |
'PORT': REDIS_CONFIG_2.port, |
| 158 |
'DB': REDIS_CONFIG_2.db, |
| 159 |
} |
| 160 |
connection_params_3 = { |
| 161 |
'HOST': REDIS_CONFIG_3.host, |
| 162 |
'PORT': REDIS_CONFIG_3.port, |
| 163 |
'DB': REDIS_CONFIG_3.db, |
| 164 |
} |
| 165 |
|
| 166 |
# Test with duplicates: zebra and alpha share connection_params_1 |
| 167 |
queue_items = [ |
| 168 |
('zebra', connection_params_3), |
| 169 |
('alpha', connection_params_1), |
| 170 |
('charlie', connection_params_2), |
| 171 |
('delta', connection_params_1), # duplicate of alpha |
| 172 |
] |
| 173 |
|
| 174 |
results = [] |
| 175 |
# Call get_unique_connection_configs 5 times with reshuffled key order |
| 176 |
for _ in range(5): |
| 177 |
# Reshuffle the order of items to prove ordering doesn't matter |
| 178 |
random.shuffle(queue_items) |
| 179 |
config = dict(queue_items) |
| 180 |
|
| 181 |
result = get_unique_connection_configs(config) |
| 182 |
results.append(result) |
| 183 |
|
| 184 |
# All results should be identical despite different input dict orderings |
| 185 |
first_result = results[0] |
| 186 |
for result in results: |
| 187 |
self.assertEqual(result, first_result) |
| 188 |
|
| 189 |
# Results should be in order based on sorted queue names |
| 190 |
# Sorted order: alpha (params_1), charlie (params_2), delta (params_1 dup), zebra (params_3) |
| 191 |
# Unique configs in order: params_1 (from alpha), params_2 (from charlie), params_3 (from zebra) |
| 192 |
self.assertEqual(first_result, [connection_params_1, connection_params_2, connection_params_3]) |
| 193 |
|
| 194 |
def test_get_connection_by_index(self): |
| 195 |
""" |
| 196 |
Test that get_connection_by_index returns the correct connection for valid indices. |
| 197 |
""" |
| 198 |
# Get all unique configs to know how many we have |
| 199 |
unique_configs = get_unique_connection_configs() |
| 200 |
|
| 201 |
# Test all valid indices and verify connections are correct |
| 202 |
for i in range(len(unique_configs)): |
| 203 |
expected_config = unique_configs[i] |
| 204 |
|
| 205 |
# Skip configs that might not be valid Redis connections (cache backends) |
| 206 |
if 'USE_REDIS_CACHE' in expected_config: |
| 207 |
continue |
| 208 |
|
| 209 |
connection = get_connection_by_index(i) |
| 210 |
|
| 211 |
# Verify connection matches expected config |
| 212 |
if 'HOST' in expected_config: |
| 213 |
# Regular HOST/PORT/DB config |
| 214 |
self.assert_connection_kwargs(connection, expected_config) |
| 215 |
elif 'URL' in expected_config: |
| 216 |
# URL-based config - verify DB if specified |
| 217 |
if 'DB' in expected_config: |
| 218 |
connection_kwargs = connection.connection_pool.connection_kwargs |
| 219 |
self.assertEqual(connection_kwargs['db'], expected_config['DB']) |
| 220 |
elif 'SENTINELS' in expected_config: |
| 221 |
# Sentinel config - connection pool type is different |
| 222 |
from redis.sentinel import SentinelConnectionPool |
| 223 |
|
| 224 |
self.assertIsInstance(connection.connection_pool, SentinelConnectionPool) |
| 225 |
|
| 226 |
|
| 227 |
class RedisCacheTest(TestCase): |
| 228 |
@skipIf(settings.REDIS_CACHE_TYPE != 'django-redis', 'django-redis not installed') |
| 229 |
@patch('django_redis.get_redis_connection') |
| 230 |
def test_get_queue_django_redis(self, mocked): |
| 231 |
""" |
| 232 |
Test that the USE_REDIS_CACHE option for configuration works. |
| 233 |
""" |
| 234 |
queue = get_queue('django-redis') |
| 235 |
queue.enqueue(access_self) |
| 236 |
self.assertEqual(len(queue), 1) |
| 237 |
self.assertEqual(mocked.call_count, 1) |
| 238 |
|
| 239 |
@skipIf(settings.REDIS_CACHE_TYPE != 'django-redis-cache', 'django-redis-cache not installed') |
| 240 |
def test_get_queue_django_redis_cache(self): |
| 241 |
""" |
| 242 |
Test that the USE_REDIS_CACHE option for configuration works. |
| 243 |
""" |
| 244 |
queueName = 'django-redis-cache' |
| 245 |
queue = get_queue(queueName) |
| 246 |
self.assertEqual(queue.name, queueName) |
| 247 |
|
| 248 |
cacheHost = settings.CACHES[queueName]['LOCATION'].split(':')[0] |
| 249 |
cachePort = settings.CACHES[queueName]['LOCATION'].split(':')[1] |
| 250 |
cacheDBNum = settings.CACHES[queueName]['OPTIONS']['DB'] |
| 251 |
|
| 252 |
self.assert_connection_kwargs(queue.connection, { |
| 253 |
'host': cacheHost, |
| 254 |
'port': int(cachePort), |
| 255 |
'db': int(cacheDBNum), |
| 256 |
'password': None |
| 257 |
}) |