Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove outdated compatibility code #3533

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ flake8-isort
flake8
flynt~=0.69.0
invoke==2.2.0
mock
mock~=5.1.0
packaging>=20.4
pytest
pytest-asyncio>=0.23.0,<0.24.0
5 changes: 2 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -6,12 +6,11 @@
from datetime import datetime, timezone
from enum import Enum
from typing import Callable, TypeVar, Union
from unittest import mock
from unittest.mock import Mock
from urllib.parse import urlparse

import pytest
import redis
from mock.mock import Mock, patch
from packaging.version import Version
from redis import Sentinel
from redis.auth.idp import IdentityProviderInterface
@@ -510,7 +509,7 @@ def _gen_cluster_mock_resp(r, response):
connection = Mock(spec=Connection)
connection.retry = Retry(NoBackoff(), 0)
connection.read_response.return_value = response
with mock.patch.object(r, "connection", connection):
with patch.object(r, "connection", connection):
yield r


12 changes: 0 additions & 12 deletions tests/test_asyncio/compat.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import asyncio
from unittest import mock

try:
mock.AsyncMock
except AttributeError:
from unittest import mock

try:
from contextlib import aclosing
except ImportError:
@@ -17,7 +9,3 @@ async def aclosing(thing):
yield thing
finally:
await thing.aclose()


def create_task(coroutine):
return asyncio.create_task(coroutine)
35 changes: 3 additions & 32 deletions tests/test_asyncio/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import os
import random
from contextlib import asynccontextmanager as _asynccontextmanager
from datetime import datetime, timezone
from enum import Enum
from typing import Union

import pytest
import pytest_asyncio
import redis.asyncio as redis
from mock.mock import Mock
from mock.mock import AsyncMock, Mock, patch
from packaging.version import Version
from redis.asyncio import Sentinel
from redis.asyncio.client import Monitor
@@ -37,8 +36,6 @@
)
from tests.conftest import REDIS_INFO

from .compat import mock


class AuthType(Enum):
MANAGED_IDENTITY = "managed_identity"
@@ -172,10 +169,10 @@ async def master(request, sentinel_setup):


def _gen_cluster_mock_resp(r, response):
connection = mock.AsyncMock(spec=Connection)
connection = AsyncMock(spec=Connection)
connection.retry = Retry(NoBackoff(), 0)
connection.read_response.return_value = response
with mock.patch.object(r, "connection", connection):
with patch.object(r, "connection", connection):
yield r


@@ -403,32 +400,6 @@ async def wait_for_command(
return None


# python 3.6 doesn't have the asynccontextmanager decorator. Provide it here.
class AsyncContextManager:
def __init__(self, async_generator):
self.gen = async_generator

async def __aenter__(self):
try:
return await self.gen.__anext__()
except StopAsyncIteration as err:
raise RuntimeError("Pickles") from err

async def __aexit__(self, exc_type, exc_inst, tb):
if exc_type:
await self.gen.athrow(exc_type, exc_inst, tb)
return True
try:
await self.gen.__anext__()
except StopAsyncIteration:
return
raise RuntimeError("More pickles")


def asynccontextmanager(func):
return _asynccontextmanager(func)


# helpers to get the connection arguments for this run
@pytest.fixture()
def redis_url(request):
3 changes: 2 additions & 1 deletion tests/test_asyncio/test_cluster.py
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
import pytest
import pytest_asyncio
from _pytest.fixtures import FixtureRequest
from mock import mock
from redis._parsers import AsyncCommandsParser
from redis.asyncio.cluster import ClusterNode, NodesManager, RedisCluster
from redis.asyncio.connection import Connection, SSLConnection, async_timeout
@@ -38,7 +39,7 @@
)

from ..ssl_utils import get_tls_certificates
from .compat import aclosing, mock
from .compat import aclosing

pytestmark = pytest.mark.onlycluster

29 changes: 14 additions & 15 deletions tests/test_asyncio/test_connection.py
Original file line number Diff line number Diff line change
@@ -2,10 +2,10 @@
import socket
import types
from errno import ECONNREFUSED
from unittest.mock import patch

import pytest
import redis
from mock import mock
from redis._parsers import (
_AsyncHiredisParser,
_AsyncRESP2Parser,
@@ -25,7 +25,6 @@
from redis.utils import HIREDIS_AVAILABLE
from tests.conftest import skip_if_server_version_lt

from .compat import mock
from .mocks import MockStream


@@ -43,7 +42,7 @@ async def test_invalid_response(create_redis):
else:
exp_err = f'Protocol error, got "{raw.decode()}" as reply type byte'

with mock.patch.object(parser, "_stream", fake_stream):
with mock.mock.patch.object(parser, "_stream", fake_stream):
with pytest.raises(InvalidResponse, match=exp_err):
await parser.read_response()

@@ -86,8 +85,8 @@ async def get_conn():
init_call_count += 1
return mock_conn

with mock.patch.object(r.connection_pool, "get_connection", get_conn):
with mock.patch.object(r.connection_pool, "release"):
with mock.mock.patch.object(r.connection_pool, "get_connection", get_conn):
with mock.mock.patch.object(r.connection_pool, "release"):
await asyncio.gather(r.set("a", "b"), r.set("c", "d"))

assert init_call_count == 1
@@ -157,7 +156,7 @@ async def mock_connect():

async def test_connect_without_retry_on_os_error():
"""Test that the _connect function is not being retried in case of a OSError"""
with patch.object(Connection, "_connect") as _connect:
with mock.patch.object(Connection, "_connect") as _connect:
_connect.side_effect = OSError("")
conn = Connection(retry_on_timeout=True, retry=Retry(NoBackoff(), 2))
with pytest.raises(ConnectionError):
@@ -281,9 +280,9 @@ async def dummy_method(*args, **kwargs):
pass

# get dummy stream objects for the connection
with patch.object(asyncio, "open_connection", open_connection):
with mock.patch.object(asyncio, "open_connection", open_connection):
# disable the initial version handshake
with patch.multiple(
with mock.patch.multiple(
conn, send_command=dummy_method, read_response=dummy_method
):
await conn.connect()
@@ -325,7 +324,7 @@ async def mock_aclose(self):

url: str = request.config.getoption("--redis-url")
r1 = await Redis.from_url(url)
with patch.object(r1, "aclose", mock_aclose):
with mock.patch.object(r1, "aclose", mock_aclose):
with pytest.deprecated_call():
await r1.close()
assert calls == 1
@@ -382,7 +381,7 @@ async def mock_disconnect(_):
nonlocal called
called += 1

with patch.object(ConnectionPool, "disconnect", mock_disconnect):
with mock.patch.object(ConnectionPool, "disconnect", mock_disconnect):
async with await get_redis_connection() as r1:
assert r1.auto_close_connection_pool is False

@@ -414,7 +413,7 @@ async def mock_disconnect(_):
nonlocal called
called += 1

with patch.object(ConnectionPool, "disconnect", mock_disconnect):
with mock.patch.object(ConnectionPool, "disconnect", mock_disconnect):
async with await get_redis_connection() as r1:
assert r1.auto_close_connection_pool is True

@@ -441,7 +440,7 @@ async def mock_disconnect(_):
nonlocal called
called += 1

with patch.object(ConnectionPool, "disconnect", mock_disconnect):
with mock.patch.object(ConnectionPool, "disconnect", mock_disconnect):
async with await get_redis_connection() as r1:
assert r1.auto_close_connection_pool is False

@@ -461,7 +460,7 @@ async def test_client_garbage_collection(request):
# create a client with a connection from the pool
client = Redis(connection_pool=pool, single_connection_client=True)
await client.initialize()
with mock.patch.object(client, "connection") as a:
with mock.mock.patch.object(client, "connection") as a:
# we cannot, in unittests, or from asyncio, reliably trigger garbage collection
# so we must just invoke the handler
with pytest.warns(ResourceWarning):
@@ -486,8 +485,8 @@ async def test_connection_garbage_collection(request):
await client.initialize()
conn = client.connection

with mock.patch.object(conn, "_reader"):
with mock.patch.object(conn, "_writer") as a:
with mock.mock.patch.object(conn, "_reader"):
with mock.mock.patch.object(conn, "_writer") as a:
# we cannot, in unittests, or from asyncio, reliably trigger
# garbage collection so we must just invoke the handler
with pytest.warns(ResourceWarning):
5 changes: 3 additions & 2 deletions tests/test_asyncio/test_connection_pool.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import asyncio
import re
from contextlib import asynccontextmanager

import pytest
import pytest_asyncio
import redis.asyncio as redis
from mock import mock
from redis.asyncio.connection import Connection, to_bool
from redis.auth.token import TokenInterface
from tests.conftest import skip_if_redis_enterprise, skip_if_server_version_lt

from .compat import aclosing, mock
from .conftest import asynccontextmanager
from .compat import aclosing
from .test_pubsub import wait_for_message


5 changes: 3 additions & 2 deletions tests/test_asyncio/test_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import pytest
import redis
from mock.mock import patch
from tests.conftest import skip_if_server_version_lt

from .compat import aclosing, mock
from .compat import aclosing
from .conftest import wait_for_command


@@ -295,7 +296,7 @@ async def mock_reset():
nonlocal called
called += 1

with mock.patch.object(pipe, "reset", mock_reset):
with patch.object(pipe, "reset", mock_reset):
await pipe.aclose()
assert called == 1

25 changes: 10 additions & 15 deletions tests/test_asyncio/test_pubsub.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,8 @@
import socket
import sys
from typing import Optional
from unittest.mock import patch

from mock.mock import patch

# 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
@@ -20,7 +21,7 @@
from redis.utils import HIREDIS_AVAILABLE
from tests.conftest import get_protocol_version, skip_if_server_version_lt

from .compat import aclosing, create_task, mock
from .compat import aclosing


def with_timeout(t):
@@ -733,7 +734,7 @@ async def loop_step():
await messages.put(message)
break

task = asyncio.get_running_loop().create_task(loop())
task = asyncio.create_task(loop())
# get the initial connect message
async with async_timeout(1):
message = await messages.get()
@@ -782,7 +783,7 @@ def callback(message):
messages = asyncio.Queue()
p = pubsub
await self._subscribe(p, foo=callback)
task = asyncio.get_running_loop().create_task(p.run())
task = asyncio.create_task(p.run())
await r.publish("foo", "bar")
message = await messages.get()
task.cancel()
@@ -805,8 +806,8 @@ def exception_handler_callback(e, pubsub) -> None:
exceptions = asyncio.Queue()
p = pubsub
await self._subscribe(p, foo=lambda x: None)
with mock.patch.object(p, "get_message", side_effect=Exception("error")):
task = asyncio.get_running_loop().create_task(
with patch.object(p, "get_message", side_effect=Exception("error")):
task = asyncio.create_task(
p.run(exception_handler=exception_handler_callback)
)
e = await exceptions.get()
@@ -823,7 +824,7 @@ def callback(message):

messages = asyncio.Queue()
p = pubsub
task = asyncio.get_running_loop().create_task(p.run())
task = asyncio.create_task(p.run())
# wait until loop gets settled. Add a subscription
await asyncio.sleep(0.1)
await p.subscribe(foo=callback)
@@ -867,7 +868,7 @@ async def mysetup(self, r, method):
else:
self.get_message = self.loop_step_listen

self.task = create_task(self.loop())
self.task = asyncio.create_task(self.loop())
# get the initial connect message
message = await self.messages.get()
assert message == {
@@ -903,7 +904,7 @@ async def test_reconnect_socket_error(self, r: redis.Redis, method):
async with self.cond:
assert self.state == 0
self.state = 1
with mock.patch.object(self.pubsub.connection, "_parser") as m:
with patch.object(self.pubsub.connection, "_parser") as m:
m.read_response.side_effect = socket.error
m.can_read_destructive.side_effect = socket.error
# wait until task noticies the disconnect until we
@@ -989,9 +990,6 @@ async def loop_step_listen(self):

@pytest.mark.onlynoncluster
class TestBaseException:
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="requires python 3.8 or higher"
)
async def test_outer_timeout(self, r: redis.Redis):
"""
Using asyncio_timeout manually outside the inner method timeouts works.
@@ -1023,9 +1021,6 @@ async def get_msg_or_timeout(timeout=0.1):
# the timeout on the read should not cause disconnect
assert pubsub.connection.is_connected

@pytest.mark.skipif(
sys.version_info < (3, 8), reason="requires python 3.8 or higher"
)
async def test_base_exception(self, r: redis.Redis):
"""
Manually trigger a BaseException inside the parser's .read_response method
Loading
Loading