|
14 | 14 | pytestmark = pytest.mark.asyncio |
15 | 15 |
|
16 | 16 |
|
| 17 | +class TestRedisAutoReleaseConnectionPool: |
| 18 | + @pytest.fixture |
| 19 | + async def r(self, create_redis) -> aioredis.Redis: |
| 20 | + """This is necessary since r and r2 create ConnectionPools behind the scenes""" |
| 21 | + r = await create_redis() |
| 22 | + r.auto_close_connection_pool = True |
| 23 | + yield r |
| 24 | + |
| 25 | + @staticmethod |
| 26 | + def get_total_connected_connections(pool): |
| 27 | + return len(pool._available_connections) + len(pool._in_use_connections) |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + async def create_two_conn(r: aioredis.Redis): |
| 31 | + if not r.single_connection_client: # Single already initialized connection |
| 32 | + r.connection = await r.connection_pool.get_connection("_") |
| 33 | + return await r.connection_pool.get_connection("_") |
| 34 | + |
| 35 | + @staticmethod |
| 36 | + def has_no_connected_connections(pool: aioredis.ConnectionPool): |
| 37 | + return not any( |
| 38 | + x.is_connected |
| 39 | + for x in pool._available_connections + list(pool._in_use_connections) |
| 40 | + ) |
| 41 | + |
| 42 | + async def test_auto_disconnect_redis_created_pool(self, r: aioredis.Redis): |
| 43 | + new_conn = await self.create_two_conn(r) |
| 44 | + assert new_conn != r.connection |
| 45 | + assert self.get_total_connected_connections(r.connection_pool) == 2 |
| 46 | + await r.close() |
| 47 | + assert self.has_no_connected_connections(r.connection_pool) |
| 48 | + |
| 49 | + async def test_do_not_auto_disconnect_redis_created_pool(self, r2: aioredis.Redis): |
| 50 | + assert r2.auto_close_connection_pool is False, ( |
| 51 | + "The connection pool should not be disconnected as a manually created " |
| 52 | + "connection pool was passed in in conftest.py" |
| 53 | + ) |
| 54 | + new_conn = await self.create_two_conn(r2) |
| 55 | + assert self.get_total_connected_connections(r2.connection_pool) == 2 |
| 56 | + await r2.close() |
| 57 | + assert r2.connection_pool._in_use_connections == {new_conn} |
| 58 | + assert new_conn.is_connected |
| 59 | + assert len(r2.connection_pool._available_connections) == 1 |
| 60 | + assert r2.connection_pool._available_connections[0].is_connected |
| 61 | + |
| 62 | + async def test_auto_release_override_true_manual_created_pool( |
| 63 | + self, r: aioredis.Redis |
| 64 | + ): |
| 65 | + assert r.auto_close_connection_pool is True, "This is from the class fixture" |
| 66 | + await self.create_two_conn(r) |
| 67 | + await r.close() |
| 68 | + assert self.get_total_connected_connections(r.connection_pool) == 2, ( |
| 69 | + "The connection pool should not be disconnected as a manually created " |
| 70 | + "connection pool was passed in in conftest.py" |
| 71 | + ) |
| 72 | + assert self.has_no_connected_connections(r.connection_pool) |
| 73 | + |
| 74 | + @pytest.mark.parametrize("auto_close_conn_pool", [True, False]) |
| 75 | + async def test_close_override(self, r: aioredis.Redis, auto_close_conn_pool): |
| 76 | + r.auto_close_connection_pool = auto_close_conn_pool |
| 77 | + await self.create_two_conn(r) |
| 78 | + await r.close(close_connection_pool=True) |
| 79 | + assert self.has_no_connected_connections(r.connection_pool) |
| 80 | + |
| 81 | + @pytest.mark.parametrize("auto_close_conn_pool", [True, False]) |
| 82 | + async def test_negate_auto_close_client_pool( |
| 83 | + self, r: aioredis.Redis, auto_close_conn_pool |
| 84 | + ): |
| 85 | + r.auto_close_connection_pool = auto_close_conn_pool |
| 86 | + new_conn = await self.create_two_conn(r) |
| 87 | + await r.close(close_connection_pool=False) |
| 88 | + assert not self.has_no_connected_connections(r.connection_pool) |
| 89 | + assert r.connection_pool._in_use_connections == {new_conn} |
| 90 | + assert r.connection_pool._available_connections[0].is_connected |
| 91 | + assert self.get_total_connected_connections(r.connection_pool) == 2 |
| 92 | + |
| 93 | + |
17 | 94 | class DummyConnection(Connection): |
18 | 95 | description_format = "DummyConnection<>" |
19 | 96 |
|
|
0 commit comments