|
5 | 5 |
|
6 | 6 | import pytest
|
7 | 7 | import redis
|
| 8 | +from redis import ConnectionPool, Redis |
8 | 9 | from redis._parsers import _HiredisParser, _RESP2Parser, _RESP3Parser
|
9 | 10 | from redis.backoff import NoBackoff
|
10 |
| -from redis.connection import Connection, SSLConnection, UnixDomainSocketConnection |
| 11 | +from redis.connection import ( |
| 12 | + Connection, |
| 13 | + SSLConnection, |
| 14 | + UnixDomainSocketConnection, |
| 15 | + parse_url, |
| 16 | +) |
11 | 17 | from redis.exceptions import ConnectionError, InvalidResponse, TimeoutError
|
12 | 18 | from redis.retry import Retry
|
13 | 19 | from redis.utils import HIREDIS_AVAILABLE
|
@@ -209,3 +215,84 @@ def test_create_single_connection_client_from_url():
|
209 | 215 | "redis://localhost:6379/0?", single_connection_client=True
|
210 | 216 | )
|
211 | 217 | assert client.connection is not None
|
| 218 | + |
| 219 | + |
| 220 | +@pytest.mark.parametrize("from_url", (True, False), ids=("from_url", "from_args")) |
| 221 | +def test_pool_auto_close(request, from_url): |
| 222 | + """Verify that basic Redis instances have auto_close_connection_pool set to True""" |
| 223 | + |
| 224 | + url: str = request.config.getoption("--redis-url") |
| 225 | + url_args = parse_url(url) |
| 226 | + |
| 227 | + def get_redis_connection(): |
| 228 | + if from_url: |
| 229 | + return Redis.from_url(url) |
| 230 | + return Redis(**url_args) |
| 231 | + |
| 232 | + r1 = get_redis_connection() |
| 233 | + assert r1.auto_close_connection_pool is True |
| 234 | + r1.close() |
| 235 | + |
| 236 | + |
| 237 | +@pytest.mark.parametrize("from_url", (True, False), ids=("from_url", "from_args")) |
| 238 | +def test_redis_connection_pool(request, from_url): |
| 239 | + """Verify that basic Redis instances using `connection_pool` |
| 240 | + have auto_close_connection_pool set to False""" |
| 241 | + |
| 242 | + url: str = request.config.getoption("--redis-url") |
| 243 | + url_args = parse_url(url) |
| 244 | + |
| 245 | + pool = None |
| 246 | + |
| 247 | + def get_redis_connection(): |
| 248 | + nonlocal pool |
| 249 | + if from_url: |
| 250 | + pool = ConnectionPool.from_url(url) |
| 251 | + else: |
| 252 | + pool = ConnectionPool(**url_args) |
| 253 | + return Redis(connection_pool=pool) |
| 254 | + |
| 255 | + called = 0 |
| 256 | + |
| 257 | + def mock_disconnect(_): |
| 258 | + nonlocal called |
| 259 | + called += 1 |
| 260 | + |
| 261 | + with patch.object(ConnectionPool, "disconnect", mock_disconnect): |
| 262 | + with get_redis_connection() as r1: |
| 263 | + assert r1.auto_close_connection_pool is False |
| 264 | + |
| 265 | + assert called == 0 |
| 266 | + pool.disconnect() |
| 267 | + |
| 268 | + |
| 269 | +@pytest.mark.parametrize("from_url", (True, False), ids=("from_url", "from_args")) |
| 270 | +def test_redis_from_pool(request, from_url): |
| 271 | + """Verify that basic Redis instances using `from_pool` |
| 272 | + have auto_close_connection_pool set to True""" |
| 273 | + |
| 274 | + url: str = request.config.getoption("--redis-url") |
| 275 | + url_args = parse_url(url) |
| 276 | + |
| 277 | + pool = None |
| 278 | + |
| 279 | + def get_redis_connection(): |
| 280 | + nonlocal pool |
| 281 | + if from_url: |
| 282 | + pool = ConnectionPool.from_url(url) |
| 283 | + else: |
| 284 | + pool = ConnectionPool(**url_args) |
| 285 | + return Redis(from_pool=pool) |
| 286 | + |
| 287 | + called = 0 |
| 288 | + |
| 289 | + def mock_disconnect(_): |
| 290 | + nonlocal called |
| 291 | + called += 1 |
| 292 | + |
| 293 | + with patch.object(ConnectionPool, "disconnect", mock_disconnect): |
| 294 | + with get_redis_connection() as r1: |
| 295 | + assert r1.auto_close_connection_pool is True |
| 296 | + |
| 297 | + assert called == 1 |
| 298 | + pool.disconnect() |
0 commit comments