Skip to content

Commit 308f124

Browse files
committed
Add tests for the from_pool argument
1 parent a40f574 commit 308f124

File tree

1 file changed

+67
-6
lines changed

1 file changed

+67
-6
lines changed

tests/test_asyncio/test_connection.py

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
_AsyncRESP3Parser,
1212
_AsyncRESPBase,
1313
)
14-
from redis.asyncio import Redis
14+
from redis.asyncio import ConnectionPool, Redis
1515
from redis.asyncio.connection import Connection, UnixDomainSocketConnection, parse_url
1616
from redis.asyncio.retry import Retry
1717
from redis.backoff import NoBackoff
@@ -289,7 +289,7 @@ def test_create_single_connection_client_from_url():
289289
assert client.single_connection_client is True
290290

291291

292-
@pytest.mark.parametrize("from_url", (True, False))
292+
@pytest.mark.parametrize("from_url", (True, False), ids=("from_url", "from_args"))
293293
async def test_pool_auto_close(request, from_url):
294294
"""Verify that basic Redis instances have auto_close_connection_pool set to True"""
295295

@@ -306,20 +306,81 @@ async def get_redis_connection():
306306
await r1.close()
307307

308308

309-
@pytest.mark.parametrize("from_url", (True, False))
310-
async def test_pool_auto_close_disable(request, from_url):
309+
async def test_pool_auto_close_disable(request):
311310
"""Verify that auto_close_connection_pool can be disabled"""
312311

313312
url: str = request.config.getoption("--redis-url")
314313
url_args = parse_url(url)
315314

316315
async def get_redis_connection():
317-
if from_url:
318-
return Redis.from_url(url, auto_close_connection_pool=False)
319316
url_args["auto_close_connection_pool"] = False
320317
return Redis(**url_args)
321318

322319
r1 = await get_redis_connection()
323320
assert r1.auto_close_connection_pool is False
324321
await r1.connection_pool.disconnect()
325322
await r1.close()
323+
324+
325+
@pytest.mark.parametrize("from_url", (True, False), ids=("from_url", "from_args"))
326+
async def test_redis_connection_pool(request, from_url):
327+
"""Verify that basic Redis instances using `connection_pool`
328+
have auto_close_connection_pool set to False"""
329+
330+
url: str = request.config.getoption("--redis-url")
331+
url_args = parse_url(url)
332+
333+
pool = None
334+
335+
async def get_redis_connection():
336+
nonlocal pool
337+
if from_url:
338+
pool = ConnectionPool.from_url(url)
339+
else:
340+
pool = ConnectionPool(**url_args)
341+
return Redis(connection_pool=pool)
342+
343+
called = 0
344+
345+
async def mock_disconnect(_):
346+
nonlocal called
347+
called += 1
348+
349+
with patch.object(ConnectionPool, "disconnect", mock_disconnect):
350+
async with await get_redis_connection() as r1:
351+
assert r1.auto_close_connection_pool is False
352+
353+
assert called == 0
354+
await pool.disconnect()
355+
356+
357+
@pytest.mark.parametrize("from_url", (True, False), ids=("from_url", "from_args"))
358+
async def test_redis_from_pool(request, from_url):
359+
"""Verify that basic Redis instances using `from_pool`
360+
have auto_close_connection_pool set to True"""
361+
362+
url: str = request.config.getoption("--redis-url")
363+
url_args = parse_url(url)
364+
365+
pool = None
366+
367+
async def get_redis_connection():
368+
nonlocal pool
369+
if from_url:
370+
pool = ConnectionPool.from_url(url)
371+
else:
372+
pool = ConnectionPool(**url_args)
373+
return Redis(from_pool=pool)
374+
375+
called = 0
376+
377+
async def mock_disconnect(_):
378+
nonlocal called
379+
called += 1
380+
381+
with patch.object(ConnectionPool, "disconnect", mock_disconnect):
382+
async with await get_redis_connection() as r1:
383+
assert r1.auto_close_connection_pool is True
384+
385+
assert called == 1
386+
await pool.disconnect()

0 commit comments

Comments
 (0)