diff --git a/redis/asyncio/client.py b/redis/asyncio/client.py index 7c17938714..4254441073 100644 --- a/redis/asyncio/client.py +++ b/redis/asyncio/client.py @@ -375,7 +375,7 @@ async def initialize(self: _RedisT) -> _RedisT: if self.single_connection_client: async with self._single_conn_lock: if self.connection is None: - self.connection = await self.connection_pool.get_connection("_") + self.connection = await self.connection_pool.get_connection() self._event_dispatcher.dispatch( AfterSingleConnectionInstantiationEvent( @@ -638,7 +638,7 @@ async def execute_command(self, *args, **options): await self.initialize() pool = self.connection_pool command_name = args[0] - conn = self.connection or await pool.get_connection(command_name, **options) + conn = self.connection or await pool.get_connection() if self.single_connection_client: await self._single_conn_lock.acquire() @@ -712,7 +712,7 @@ def __init__(self, connection_pool: ConnectionPool): async def connect(self): if self.connection is None: - self.connection = await self.connection_pool.get_connection("MONITOR") + self.connection = await self.connection_pool.get_connection() async def __aenter__(self): await self.connect() @@ -900,9 +900,7 @@ async def connect(self): Ensure that the PubSub is connected """ if self.connection is None: - self.connection = await self.connection_pool.get_connection( - "pubsub", self.shard_hint - ) + self.connection = await self.connection_pool.get_connection() # register a callback that re-subscribes to any channels we # were listening to when we were disconnected self.connection.register_connect_callback(self.on_connect) @@ -1370,9 +1368,7 @@ async def immediate_execute_command(self, *args, **options): conn = self.connection # if this is the first call, we need a connection if not conn: - conn = await self.connection_pool.get_connection( - command_name, self.shard_hint - ) + conn = await self.connection_pool.get_connection() self.connection = conn return await conn.retry.call_with_retry( @@ -1568,7 +1564,7 @@ async def execute(self, raise_on_error: bool = True) -> List[Any]: conn = self.connection if not conn: - conn = await self.connection_pool.get_connection("MULTI", self.shard_hint) + conn = await self.connection_pool.get_connection() # assign to self.connection so reset() releases the connection # back to the pool after we're done self.connection = conn diff --git a/redis/asyncio/connection.py b/redis/asyncio/connection.py index 4a743ff374..e67dc5b207 100644 --- a/redis/asyncio/connection.py +++ b/redis/asyncio/connection.py @@ -29,7 +29,7 @@ from ..auth.token import TokenInterface from ..event import AsyncAfterConnectionReleasedEvent, EventDispatcher -from ..utils import format_error_message +from ..utils import deprecated_args, format_error_message # the functionality is available in 3.11.x but has a major issue before # 3.11.3. See https://github.com/redis/redis-py/issues/2633 @@ -1087,7 +1087,12 @@ def can_get_connection(self) -> bool: or len(self._in_use_connections) < self.max_connections ) - async def get_connection(self, command_name, *keys, **options): + @deprecated_args( + args_to_warn=["*"], + reason="Use get_connection() without args instead", + version="5.0.3", + ) + async def get_connection(self, command_name=None, *keys, **options): async with self._lock: """Get a connected connection from the pool""" connection = self.get_available_connection() @@ -1255,7 +1260,12 @@ def __init__( self._condition = asyncio.Condition() self.timeout = timeout - async def get_connection(self, command_name, *keys, **options): + @deprecated_args( + args_to_warn=["*"], + reason="Use get_connection() without args instead", + version="5.0.3", + ) + async def get_connection(self, command_name=None, *keys, **options): """Gets a connection from the pool, blocking until one is available""" try: async with self._condition: diff --git a/redis/client.py b/redis/client.py index 5a9f4fafb5..fc535c8ca0 100755 --- a/redis/client.py +++ b/redis/client.py @@ -366,7 +366,7 @@ def __init__( self.connection = None self._single_connection_client = single_connection_client if self._single_connection_client: - self.connection = self.connection_pool.get_connection("_") + self.connection = self.connection_pool.get_connection() self._event_dispatcher.dispatch( AfterSingleConnectionInstantiationEvent( self.connection, ClientType.SYNC, self.single_connection_lock @@ -608,7 +608,7 @@ def _execute_command(self, *args, **options): """Execute a command and return a parsed response""" pool = self.connection_pool command_name = args[0] - conn = self.connection or pool.get_connection(command_name, **options) + conn = self.connection or pool.get_connection() if self._single_connection_client: self.single_connection_lock.acquire() @@ -667,7 +667,7 @@ class Monitor: def __init__(self, connection_pool): self.connection_pool = connection_pool - self.connection = self.connection_pool.get_connection("MONITOR") + self.connection = self.connection_pool.get_connection() def __enter__(self): self.connection.send_command("MONITOR") @@ -840,9 +840,7 @@ def execute_command(self, *args): # subscribed to one or more channels if self.connection is None: - self.connection = self.connection_pool.get_connection( - "pubsub", self.shard_hint - ) + self.connection = self.connection_pool.get_connection() # register a callback that re-subscribes to any channels we # were listening to when we were disconnected self.connection.register_connect_callback(self.on_connect) @@ -1397,7 +1395,7 @@ def immediate_execute_command(self, *args, **options): conn = self.connection # if this is the first call, we need a connection if not conn: - conn = self.connection_pool.get_connection(command_name, self.shard_hint) + conn = self.connection_pool.get_connection() self.connection = conn return conn.retry.call_with_retry( @@ -1583,7 +1581,7 @@ def execute(self, raise_on_error: bool = True) -> List[Any]: conn = self.connection if not conn: - conn = self.connection_pool.get_connection("MULTI", self.shard_hint) + conn = self.connection_pool.get_connection() # assign to self.connection so reset() releases the connection # back to the pool after we're done self.connection = conn diff --git a/redis/cluster.py b/redis/cluster.py index db866ce2bf..c184838a9b 100644 --- a/redis/cluster.py +++ b/redis/cluster.py @@ -40,6 +40,7 @@ from redis.retry import Retry from redis.utils import ( HIREDIS_AVAILABLE, + deprecated_args, dict_merge, list_keys_to_dict, merge_result, @@ -52,10 +53,13 @@ def get_node_name(host: str, port: Union[str, int]) -> str: return f"{host}:{port}" +@deprecated_args( + allowed_args=["redis_node"], + reason="Use get_connection(redis_node) instead", + version="5.0.3", +) def get_connection(redis_node, *args, **options): - return redis_node.connection or redis_node.connection_pool.get_connection( - args[0], **options - ) + return redis_node.connection or redis_node.connection_pool.get_connection() def parse_scan_result(command, res, **options): @@ -1151,7 +1155,7 @@ def _execute_command(self, target_node, *args, **kwargs): moved = False redis_node = self.get_redis_connection(target_node) - connection = get_connection(redis_node, *args, **kwargs) + connection = get_connection(redis_node) if asking: connection.send_command("ASKING") redis_node.parse_response(connection, "ASKING", **kwargs) @@ -1822,9 +1826,7 @@ def execute_command(self, *args): self.node = node redis_connection = self.cluster.get_redis_connection(node) self.connection_pool = redis_connection.connection_pool - self.connection = self.connection_pool.get_connection( - "pubsub", self.shard_hint - ) + self.connection = self.connection_pool.get_connection() # register a callback that re-subscribes to any channels we # were listening to when we were disconnected self.connection.register_connect_callback(self.on_connect) @@ -2184,7 +2186,7 @@ def _send_cluster_commands( if node_name not in nodes: redis_node = self.get_redis_connection(node) try: - connection = get_connection(redis_node, c.args) + connection = get_connection(redis_node) except (ConnectionError, TimeoutError): for n in nodes.values(): n.connection_pool.release(n.connection) diff --git a/redis/connection.py b/redis/connection.py index c4ff9b7b17..d59a9b069b 100644 --- a/redis/connection.py +++ b/redis/connection.py @@ -42,6 +42,7 @@ HIREDIS_AVAILABLE, SSL_AVAILABLE, compare_versions, + deprecated_args, ensure_string, format_error_message, get_lib_version, @@ -1461,8 +1462,14 @@ def _checkpid(self) -> None: finally: self._fork_lock.release() - def get_connection(self, command_name: str, *keys, **options) -> "Connection": + @deprecated_args( + args_to_warn=["*"], + reason="Use get_connection() without args instead", + version="5.0.3", + ) + def get_connection(self, command_name=None, *keys, **options) -> "Connection": "Get a connection from the pool" + self._checkpid() with self._lock: try: @@ -1683,7 +1690,12 @@ def make_connection(self): self._connections.append(connection) return connection - def get_connection(self, command_name, *keys, **options): + @deprecated_args( + args_to_warn=["*"], + reason="Use get_connection() without args instead", + version="5.0.3", + ) + def get_connection(self, command_name=None, *keys, **options): """ Get a connection, blocking for ``self.timeout`` until a connection is available from the pool. diff --git a/redis/utils.py b/redis/utils.py index 8693fb3c8f..66465636a1 100644 --- a/redis/utils.py +++ b/redis/utils.py @@ -122,6 +122,71 @@ def wrapper(*args, **kwargs): return decorator +def warn_deprecated_arg_usage( + arg_name: Union[list, str], + function_name: str, + reason: str = "", + version: str = "", + stacklevel: int = 2, +): + import warnings + + msg = ( + f"Call to '{function_name}' function with deprecated" + f" usage of input argument/s '{arg_name}'." + ) + if reason: + msg += f" ({reason})" + if version: + msg += f" -- Deprecated since version {version}." + warnings.warn(msg, category=DeprecationWarning, stacklevel=stacklevel) + + +def deprecated_args( + args_to_warn: list = ["*"], + allowed_args: list = [], + reason: str = "", + version: str = "", +): + """ + Decorator to mark specified args of a function as deprecated. + If '*' is in args_to_warn, all arguments will be marked as deprecated. + """ + + def decorator(func): + @wraps(func) + def wrapper(*args, **kwargs): + # Get function argument names + arg_names = func.__code__.co_varnames[: func.__code__.co_argcount] + + provided_args = dict(zip(arg_names, args)) + provided_args.update(kwargs) + + provided_args.pop("self", None) + for allowed_arg in allowed_args: + provided_args.pop(allowed_arg, None) + + for arg in args_to_warn: + if arg == "*" and len(provided_args) > 0: + warn_deprecated_arg_usage( + list(provided_args.keys()), + func.__name__, + reason, + version, + stacklevel=3, + ) + elif arg in provided_args: + warn_deprecated_arg_usage( + arg, func.__name__, reason, version, stacklevel=3 + ) + + return func(*args, **kwargs) + + return wrapper + + return decorator + + def _set_info_logger(): """ Set up a logger that log info logs to stdout. diff --git a/tests/test_asyncio/test_connection.py b/tests/test_asyncio/test_connection.py index d4956f16e9..38764d30cd 100644 --- a/tests/test_asyncio/test_connection.py +++ b/tests/test_asyncio/test_connection.py @@ -78,7 +78,7 @@ async def call_with_retry(self, _, __): mock_conn = mock.AsyncMock(spec=Connection) mock_conn.retry = Retry_() - async def get_conn(_): + async def get_conn(): # Validate only one client is created in single-client mode when # concurrent requests are made nonlocal init_call_count diff --git a/tests/test_asyncio/test_connection_pool.py b/tests/test_asyncio/test_connection_pool.py index 83545b4ede..3d120e4ca7 100644 --- a/tests/test_asyncio/test_connection_pool.py +++ b/tests/test_asyncio/test_connection_pool.py @@ -29,8 +29,8 @@ def get_total_connected_connections(pool): @staticmethod async def create_two_conn(r: redis.Redis): if not r.single_connection_client: # Single already initialized connection - r.connection = await r.connection_pool.get_connection("_") - return await r.connection_pool.get_connection("_") + r.connection = await r.connection_pool.get_connection() + return await r.connection_pool.get_connection() @staticmethod def has_no_connected_connections(pool: redis.ConnectionPool): @@ -138,7 +138,7 @@ async def test_connection_creation(self): async with self.get_pool( connection_kwargs=connection_kwargs, connection_class=DummyConnection ) as pool: - connection = await pool.get_connection("_") + connection = await pool.get_connection() assert isinstance(connection, DummyConnection) assert connection.kwargs == connection_kwargs @@ -155,8 +155,8 @@ async def test_aclosing(self): async def test_multiple_connections(self, master_host): connection_kwargs = {"host": master_host[0]} async with self.get_pool(connection_kwargs=connection_kwargs) as pool: - c1 = await pool.get_connection("_") - c2 = await pool.get_connection("_") + c1 = await pool.get_connection() + c2 = await pool.get_connection() assert c1 != c2 async def test_max_connections(self, master_host): @@ -164,17 +164,17 @@ async def test_max_connections(self, master_host): async with self.get_pool( max_connections=2, connection_kwargs=connection_kwargs ) as pool: - await pool.get_connection("_") - await pool.get_connection("_") + await pool.get_connection() + await pool.get_connection() with pytest.raises(redis.ConnectionError): - await pool.get_connection("_") + await pool.get_connection() async def test_reuse_previously_released_connection(self, master_host): connection_kwargs = {"host": master_host[0]} async with self.get_pool(connection_kwargs=connection_kwargs) as pool: - c1 = await pool.get_connection("_") + c1 = await pool.get_connection() await pool.release(c1) - c2 = await pool.get_connection("_") + c2 = await pool.get_connection() assert c1 == c2 async def test_repr_contains_db_info_tcp(self): @@ -223,7 +223,7 @@ async def test_connection_creation(self, master_host): "port": master_host[1], } async with self.get_pool(connection_kwargs=connection_kwargs) as pool: - connection = await pool.get_connection("_") + connection = await pool.get_connection() assert isinstance(connection, DummyConnection) assert connection.kwargs == connection_kwargs @@ -236,14 +236,14 @@ async def test_disconnect(self, master_host): "port": master_host[1], } async with self.get_pool(connection_kwargs=connection_kwargs) as pool: - await pool.get_connection("_") + await pool.get_connection() await pool.disconnect() async def test_multiple_connections(self, master_host): connection_kwargs = {"host": master_host[0], "port": master_host[1]} async with self.get_pool(connection_kwargs=connection_kwargs) as pool: - c1 = await pool.get_connection("_") - c2 = await pool.get_connection("_") + c1 = await pool.get_connection() + c2 = await pool.get_connection() assert c1 != c2 async def test_connection_pool_blocks_until_timeout(self, master_host): @@ -252,11 +252,11 @@ async def test_connection_pool_blocks_until_timeout(self, master_host): async with self.get_pool( max_connections=1, timeout=0.1, connection_kwargs=connection_kwargs ) as pool: - c1 = await pool.get_connection("_") + c1 = await pool.get_connection() start = asyncio.get_running_loop().time() with pytest.raises(redis.ConnectionError): - await pool.get_connection("_") + await pool.get_connection() # we should have waited at least some period of time assert asyncio.get_running_loop().time() - start >= 0.05 @@ -271,23 +271,23 @@ async def test_connection_pool_blocks_until_conn_available(self, master_host): async with self.get_pool( max_connections=1, timeout=2, connection_kwargs=connection_kwargs ) as pool: - c1 = await pool.get_connection("_") + c1 = await pool.get_connection() async def target(): await asyncio.sleep(0.1) await pool.release(c1) start = asyncio.get_running_loop().time() - await asyncio.gather(target(), pool.get_connection("_")) + await asyncio.gather(target(), pool.get_connection()) stop = asyncio.get_running_loop().time() assert (stop - start) <= 0.2 async def test_reuse_previously_released_connection(self, master_host): connection_kwargs = {"host": master_host[0]} async with self.get_pool(connection_kwargs=connection_kwargs) as pool: - c1 = await pool.get_connection("_") + c1 = await pool.get_connection() await pool.release(c1) - c2 = await pool.get_connection("_") + c2 = await pool.get_connection() assert c1 == c2 def test_repr_contains_db_info_tcp(self): @@ -552,23 +552,23 @@ def test_cert_reqs_options(self): import ssl class DummyConnectionPool(redis.ConnectionPool): - def get_connection(self, *args, **kwargs): + def get_connection(self): return self.make_connection() pool = DummyConnectionPool.from_url("rediss://?ssl_cert_reqs=none") - assert pool.get_connection("_").cert_reqs == ssl.CERT_NONE + assert pool.get_connection().cert_reqs == ssl.CERT_NONE pool = DummyConnectionPool.from_url("rediss://?ssl_cert_reqs=optional") - assert pool.get_connection("_").cert_reqs == ssl.CERT_OPTIONAL + assert pool.get_connection().cert_reqs == ssl.CERT_OPTIONAL pool = DummyConnectionPool.from_url("rediss://?ssl_cert_reqs=required") - assert pool.get_connection("_").cert_reqs == ssl.CERT_REQUIRED + assert pool.get_connection().cert_reqs == ssl.CERT_REQUIRED pool = DummyConnectionPool.from_url("rediss://?ssl_check_hostname=False") - assert pool.get_connection("_").check_hostname is False + assert pool.get_connection().check_hostname is False pool = DummyConnectionPool.from_url("rediss://?ssl_check_hostname=True") - assert pool.get_connection("_").check_hostname is True + assert pool.get_connection().check_hostname is True class TestConnection: @@ -756,7 +756,7 @@ async def test_health_check_not_invoked_within_interval(self, r): async def test_health_check_in_pipeline(self, r): async with r.pipeline(transaction=False) as pipe: - pipe.connection = await pipe.connection_pool.get_connection("_") + pipe.connection = await pipe.connection_pool.get_connection() pipe.connection.next_health_check = 0 with mock.patch.object( pipe.connection, "send_command", wraps=pipe.connection.send_command @@ -767,7 +767,7 @@ async def test_health_check_in_pipeline(self, r): async def test_health_check_in_transaction(self, r): async with r.pipeline(transaction=True) as pipe: - pipe.connection = await pipe.connection_pool.get_connection("_") + pipe.connection = await pipe.connection_pool.get_connection() pipe.connection.next_health_check = 0 with mock.patch.object( pipe.connection, "send_command", wraps=pipe.connection.send_command @@ -779,7 +779,7 @@ async def test_health_check_in_transaction(self, r): async def test_health_check_in_watched_pipeline(self, r): await r.set("foo", "bar") async with r.pipeline(transaction=False) as pipe: - pipe.connection = await pipe.connection_pool.get_connection("_") + pipe.connection = await pipe.connection_pool.get_connection() pipe.connection.next_health_check = 0 with mock.patch.object( pipe.connection, "send_command", wraps=pipe.connection.send_command @@ -803,7 +803,7 @@ async def test_health_check_in_watched_pipeline(self, r): async def test_health_check_in_pubsub_before_subscribe(self, r): """A health check happens before the first [p]subscribe""" p = r.pubsub() - p.connection = await p.connection_pool.get_connection("_") + p.connection = await p.connection_pool.get_connection() p.connection.next_health_check = 0 with mock.patch.object( p.connection, "send_command", wraps=p.connection.send_command @@ -825,7 +825,7 @@ async def test_health_check_in_pubsub_after_subscribed(self, r): connection health """ p = r.pubsub() - p.connection = await p.connection_pool.get_connection("_") + p.connection = await p.connection_pool.get_connection() p.connection.next_health_check = 0 with mock.patch.object( p.connection, "send_command", wraps=p.connection.send_command @@ -865,7 +865,7 @@ async def test_health_check_in_pubsub_poll(self, r): check the connection's health. """ p = r.pubsub() - p.connection = await p.connection_pool.get_connection("_") + p.connection = await p.connection_pool.get_connection() with mock.patch.object( p.connection, "send_command", wraps=p.connection.send_command ) as m: diff --git a/tests/test_asyncio/test_credentials.py b/tests/test_asyncio/test_credentials.py index ca42d19090..1eb988ce71 100644 --- a/tests/test_asyncio/test_credentials.py +++ b/tests/test_asyncio/test_credentials.py @@ -274,7 +274,7 @@ async def test_change_username_password_on_existing_connection( await init_acl_user(r, username, password) r2 = await create_redis(flushdb=False, username=username, password=password) assert await r2.ping() is True - conn = await r2.connection_pool.get_connection("_") + conn = await r2.connection_pool.get_connection() await conn.send_command("PING") assert str_if_bytes(await conn.read_response()) == "PONG" assert conn.username == username diff --git a/tests/test_asyncio/test_encoding.py b/tests/test_asyncio/test_encoding.py index 162ccb367d..74a9f28b2d 100644 --- a/tests/test_asyncio/test_encoding.py +++ b/tests/test_asyncio/test_encoding.py @@ -74,7 +74,7 @@ class TestMemoryviewsAreNotPacked: async def test_memoryviews_are_not_packed(self, r): arg = memoryview(b"some_arg") arg_list = ["SOME_COMMAND", arg] - c = r.connection or await r.connection_pool.get_connection("_") + c = r.connection or await r.connection_pool.get_connection() cmd = c.pack_command(*arg_list) assert cmd[1] is arg cmds = c.pack_commands([arg_list, arg_list]) diff --git a/tests/test_asyncio/test_retry.py b/tests/test_asyncio/test_retry.py index 8bc71c1479..cd251a986f 100644 --- a/tests/test_asyncio/test_retry.py +++ b/tests/test_asyncio/test_retry.py @@ -126,13 +126,13 @@ async def test_get_set_retry_object(self, request): assert r.get_retry()._retries == retry._retries assert isinstance(r.get_retry()._backoff, NoBackoff) new_retry_policy = Retry(ExponentialBackoff(), 3) - exiting_conn = await r.connection_pool.get_connection("_") + exiting_conn = await r.connection_pool.get_connection() r.set_retry(new_retry_policy) assert r.get_retry()._retries == new_retry_policy._retries assert isinstance(r.get_retry()._backoff, ExponentialBackoff) assert exiting_conn.retry._retries == new_retry_policy._retries await r.connection_pool.release(exiting_conn) - new_conn = await r.connection_pool.get_connection("_") + new_conn = await r.connection_pool.get_connection() assert new_conn.retry._retries == new_retry_policy._retries await r.connection_pool.release(new_conn) await r.aclose() diff --git a/tests/test_asyncio/test_sentinel.py b/tests/test_asyncio/test_sentinel.py index e553fdb00b..a27ba92bb8 100644 --- a/tests/test_asyncio/test_sentinel.py +++ b/tests/test_asyncio/test_sentinel.py @@ -269,7 +269,7 @@ async def mock_disconnect(): @pytest.mark.onlynoncluster async def test_repr_correctly_represents_connection_object(sentinel): pool = SentinelConnectionPool("mymaster", sentinel) - connection = await pool.get_connection("PING") + connection = await pool.get_connection() assert ( str(connection) diff --git a/tests/test_cache.py b/tests/test_cache.py index 67733dc9af..7010baff5f 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -159,7 +159,7 @@ def test_cache_clears_on_disconnect(self, r, cache): == b"bar" ) # Force disconnection - r.connection_pool.get_connection("_").disconnect() + r.connection_pool.get_connection().disconnect() # Make sure cache is empty assert cache.size == 0 @@ -429,7 +429,7 @@ def test_cache_clears_on_disconnect(self, r, r2): # Force disconnection r.nodes_manager.get_node_from_slot( 12000 - ).redis_connection.connection_pool.get_connection("_").disconnect() + ).redis_connection.connection_pool.get_connection().disconnect() # Make sure cache is empty assert cache.size == 0 @@ -667,7 +667,7 @@ def test_cache_clears_on_disconnect(self, master, cache): == b"bar" ) # Force disconnection - master.connection_pool.get_connection("_").disconnect() + master.connection_pool.get_connection().disconnect() # Make sure cache_data is empty assert cache.size == 0 diff --git a/tests/test_cluster.py b/tests/test_cluster.py index 1b9b9969c5..908ac26211 100644 --- a/tests/test_cluster.py +++ b/tests/test_cluster.py @@ -845,7 +845,7 @@ def test_cluster_get_set_retry_object(self, request): assert node.redis_connection.get_retry()._retries == retry._retries assert isinstance(node.redis_connection.get_retry()._backoff, NoBackoff) rand_node = r.get_random_node() - existing_conn = rand_node.redis_connection.connection_pool.get_connection("_") + existing_conn = rand_node.redis_connection.connection_pool.get_connection() # Change retry policy new_retry = Retry(ExponentialBackoff(), 3) r.set_retry(new_retry) @@ -857,7 +857,7 @@ def test_cluster_get_set_retry_object(self, request): node.redis_connection.get_retry()._backoff, ExponentialBackoff ) assert existing_conn.retry._retries == new_retry._retries - new_conn = rand_node.redis_connection.connection_pool.get_connection("_") + new_conn = rand_node.redis_connection.connection_pool.get_connection() assert new_conn.retry._retries == new_retry._retries def test_cluster_retry_object(self, r) -> None: diff --git a/tests/test_connection_pool.py b/tests/test_connection_pool.py index 118294ee1b..387a0f4565 100644 --- a/tests/test_connection_pool.py +++ b/tests/test_connection_pool.py @@ -54,7 +54,7 @@ def test_connection_creation(self): pool = self.get_pool( connection_kwargs=connection_kwargs, connection_class=DummyConnection ) - connection = pool.get_connection("_") + connection = pool.get_connection() assert isinstance(connection, DummyConnection) assert connection.kwargs == connection_kwargs @@ -71,24 +71,24 @@ def test_closing(self): def test_multiple_connections(self, master_host): connection_kwargs = {"host": master_host[0], "port": master_host[1]} pool = self.get_pool(connection_kwargs=connection_kwargs) - c1 = pool.get_connection("_") - c2 = pool.get_connection("_") + c1 = pool.get_connection() + c2 = pool.get_connection() assert c1 != c2 def test_max_connections(self, master_host): connection_kwargs = {"host": master_host[0], "port": master_host[1]} pool = self.get_pool(max_connections=2, connection_kwargs=connection_kwargs) - pool.get_connection("_") - pool.get_connection("_") + pool.get_connection() + pool.get_connection() with pytest.raises(redis.ConnectionError): - pool.get_connection("_") + pool.get_connection() def test_reuse_previously_released_connection(self, master_host): connection_kwargs = {"host": master_host[0], "port": master_host[1]} pool = self.get_pool(connection_kwargs=connection_kwargs) - c1 = pool.get_connection("_") + c1 = pool.get_connection() pool.release(c1) - c2 = pool.get_connection("_") + c2 = pool.get_connection() assert c1 == c2 def test_repr_contains_db_info_tcp(self): @@ -133,15 +133,15 @@ def test_connection_creation(self, master_host): "port": master_host[1], } pool = self.get_pool(connection_kwargs=connection_kwargs) - connection = pool.get_connection("_") + connection = pool.get_connection() assert isinstance(connection, DummyConnection) assert connection.kwargs == connection_kwargs def test_multiple_connections(self, master_host): connection_kwargs = {"host": master_host[0], "port": master_host[1]} pool = self.get_pool(connection_kwargs=connection_kwargs) - c1 = pool.get_connection("_") - c2 = pool.get_connection("_") + c1 = pool.get_connection() + c2 = pool.get_connection() assert c1 != c2 def test_connection_pool_blocks_until_timeout(self, master_host): @@ -150,11 +150,11 @@ def test_connection_pool_blocks_until_timeout(self, master_host): pool = self.get_pool( max_connections=1, timeout=0.1, connection_kwargs=connection_kwargs ) - pool.get_connection("_") + pool.get_connection() start = time.time() with pytest.raises(redis.ConnectionError): - pool.get_connection("_") + pool.get_connection() # we should have waited at least 0.1 seconds assert time.time() - start >= 0.1 @@ -167,7 +167,7 @@ def test_connection_pool_blocks_until_conn_available(self, master_host): pool = self.get_pool( max_connections=1, timeout=2, connection_kwargs=connection_kwargs ) - c1 = pool.get_connection("_") + c1 = pool.get_connection() def target(): time.sleep(0.1) @@ -175,15 +175,15 @@ def target(): start = time.time() Thread(target=target).start() - pool.get_connection("_") + pool.get_connection() assert time.time() - start >= 0.1 def test_reuse_previously_released_connection(self, master_host): connection_kwargs = {"host": master_host[0], "port": master_host[1]} pool = self.get_pool(connection_kwargs=connection_kwargs) - c1 = pool.get_connection("_") + c1 = pool.get_connection() pool.release(c1) - c2 = pool.get_connection("_") + c2 = pool.get_connection() assert c1 == c2 def test_repr_contains_db_info_tcp(self): @@ -214,7 +214,7 @@ def test_initialise_pool_with_cache(self, master_host): protocol=3, cache_config=CacheConfig(), ) - assert isinstance(pool.get_connection("_"), CacheProxyConnection) + assert isinstance(pool.get_connection(), CacheProxyConnection) class TestConnectionPoolURLParsing: @@ -489,23 +489,23 @@ def test_cert_reqs_options(self): import ssl class DummyConnectionPool(redis.ConnectionPool): - def get_connection(self, *args, **kwargs): + def get_connection(self): return self.make_connection() pool = DummyConnectionPool.from_url("rediss://?ssl_cert_reqs=none") - assert pool.get_connection("_").cert_reqs == ssl.CERT_NONE + assert pool.get_connection().cert_reqs == ssl.CERT_NONE pool = DummyConnectionPool.from_url("rediss://?ssl_cert_reqs=optional") - assert pool.get_connection("_").cert_reqs == ssl.CERT_OPTIONAL + assert pool.get_connection().cert_reqs == ssl.CERT_OPTIONAL pool = DummyConnectionPool.from_url("rediss://?ssl_cert_reqs=required") - assert pool.get_connection("_").cert_reqs == ssl.CERT_REQUIRED + assert pool.get_connection().cert_reqs == ssl.CERT_REQUIRED pool = DummyConnectionPool.from_url("rediss://?ssl_check_hostname=False") - assert pool.get_connection("_").check_hostname is False + assert pool.get_connection().check_hostname is False pool = DummyConnectionPool.from_url("rediss://?ssl_check_hostname=True") - assert pool.get_connection("_").check_hostname is True + assert pool.get_connection().check_hostname is True class TestConnection: @@ -701,7 +701,7 @@ def test_health_check_not_invoked_within_interval(self, r): def test_health_check_in_pipeline(self, r): with r.pipeline(transaction=False) as pipe: - pipe.connection = pipe.connection_pool.get_connection("_") + pipe.connection = pipe.connection_pool.get_connection() pipe.connection.next_health_check = 0 with mock.patch.object( pipe.connection, "send_command", wraps=pipe.connection.send_command @@ -712,7 +712,7 @@ def test_health_check_in_pipeline(self, r): def test_health_check_in_transaction(self, r): with r.pipeline(transaction=True) as pipe: - pipe.connection = pipe.connection_pool.get_connection("_") + pipe.connection = pipe.connection_pool.get_connection() pipe.connection.next_health_check = 0 with mock.patch.object( pipe.connection, "send_command", wraps=pipe.connection.send_command @@ -724,7 +724,7 @@ def test_health_check_in_transaction(self, r): def test_health_check_in_watched_pipeline(self, r): r.set("foo", "bar") with r.pipeline(transaction=False) as pipe: - pipe.connection = pipe.connection_pool.get_connection("_") + pipe.connection = pipe.connection_pool.get_connection() pipe.connection.next_health_check = 0 with mock.patch.object( pipe.connection, "send_command", wraps=pipe.connection.send_command @@ -748,7 +748,7 @@ def test_health_check_in_watched_pipeline(self, r): def test_health_check_in_pubsub_before_subscribe(self, r): "A health check happens before the first [p]subscribe" p = r.pubsub() - p.connection = p.connection_pool.get_connection("_") + p.connection = p.connection_pool.get_connection() p.connection.next_health_check = 0 with mock.patch.object( p.connection, "send_command", wraps=p.connection.send_command @@ -770,7 +770,7 @@ def test_health_check_in_pubsub_after_subscribed(self, r): connection health """ p = r.pubsub() - p.connection = p.connection_pool.get_connection("_") + p.connection = p.connection_pool.get_connection() p.connection.next_health_check = 0 with mock.patch.object( p.connection, "send_command", wraps=p.connection.send_command @@ -810,7 +810,7 @@ def test_health_check_in_pubsub_poll(self, r): check the connection's health. """ p = r.pubsub() - p.connection = p.connection_pool.get_connection("_") + p.connection = p.connection_pool.get_connection() with mock.patch.object( p.connection, "send_command", wraps=p.connection.send_command ) as m: diff --git a/tests/test_credentials.py b/tests/test_credentials.py index b0b79d305f..95ec5577cc 100644 --- a/tests/test_credentials.py +++ b/tests/test_credentials.py @@ -252,7 +252,7 @@ def teardown(): redis.Redis, request, flushdb=False, username=username, password=password ) assert r2.ping() is True - conn = r2.connection_pool.get_connection("_") + conn = r2.connection_pool.get_connection() conn.send_command("PING") assert str_if_bytes(conn.read_response()) == "PONG" assert conn.username == username diff --git a/tests/test_multiprocessing.py b/tests/test_multiprocessing.py index 116d20dab0..0e8e8958c5 100644 --- a/tests/test_multiprocessing.py +++ b/tests/test_multiprocessing.py @@ -95,7 +95,7 @@ def test_pool(self, max_connections, master_host): max_connections=max_connections, ) - conn = pool.get_connection("ping") + conn = pool.get_connection() main_conn_pid = conn.pid with exit_callback(pool.release, conn): conn.send_command("ping") @@ -103,7 +103,7 @@ def test_pool(self, max_connections, master_host): def target(pool): with exit_callback(pool.disconnect): - conn = pool.get_connection("ping") + conn = pool.get_connection() assert conn.pid != main_conn_pid with exit_callback(pool.release, conn): assert conn.send_command("ping") is None @@ -116,7 +116,7 @@ def target(pool): # Check that connection is still alive after fork process has exited # and disconnected the connections in its pool - conn = pool.get_connection("ping") + conn = pool.get_connection() with exit_callback(pool.release, conn): assert conn.send_command("ping") is None assert conn.read_response() == b"PONG" @@ -132,12 +132,12 @@ def test_close_pool_in_main(self, max_connections, master_host): max_connections=max_connections, ) - conn = pool.get_connection("ping") + conn = pool.get_connection() assert conn.send_command("ping") is None assert conn.read_response() == b"PONG" def target(pool, disconnect_event): - conn = pool.get_connection("ping") + conn = pool.get_connection() with exit_callback(pool.release, conn): assert conn.send_command("ping") is None assert conn.read_response() == b"PONG" diff --git a/tests/test_retry.py b/tests/test_retry.py index 183807386d..e1e4c414a4 100644 --- a/tests/test_retry.py +++ b/tests/test_retry.py @@ -206,7 +206,7 @@ def test_client_retry_on_timeout(self, request): def test_get_set_retry_object(self, request): retry = Retry(NoBackoff(), 2) r = _get_client(Redis, request, retry_on_timeout=True, retry=retry) - exist_conn = r.connection_pool.get_connection("_") + exist_conn = r.connection_pool.get_connection() assert r.get_retry()._retries == retry._retries assert isinstance(r.get_retry()._backoff, NoBackoff) new_retry_policy = Retry(ExponentialBackoff(), 3) @@ -214,5 +214,5 @@ def test_get_set_retry_object(self, request): assert r.get_retry()._retries == new_retry_policy._retries assert isinstance(r.get_retry()._backoff, ExponentialBackoff) assert exist_conn.retry._retries == new_retry_policy._retries - new_conn = r.connection_pool.get_connection("_") + new_conn = r.connection_pool.get_connection() assert new_conn.retry._retries == new_retry_policy._retries diff --git a/tests/test_sentinel.py b/tests/test_sentinel.py index 54b9647098..93455f3290 100644 --- a/tests/test_sentinel.py +++ b/tests/test_sentinel.py @@ -101,7 +101,7 @@ def test_discover_master_error(sentinel): @pytest.mark.onlynoncluster def test_dead_pool(sentinel): master = sentinel.master_for("mymaster", db=9) - conn = master.connection_pool.get_connection("_") + conn = master.connection_pool.get_connection() conn.disconnect() del master conn.connect()