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

Adding unit test fixes to improve compatibility with MacOS. #3486

Merged
merged 4 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ services:
- all

redis-stack:
image: ${REDIS_STACK_IMAGE:-redis/redis-stack-server:edge}
image: ${REDIS_STACK_IMAGE:-redis/redis-stack-server:latest}
container_name: redis-stack
ports:
- 6479:6379
Expand All @@ -112,6 +112,7 @@ services:
profiles:
- standalone
- all-stack
- all

redis-stack-graph:
image: redis/redis-stack-server:6.2.6-v15
Expand Down
30 changes: 14 additions & 16 deletions tests/test_asyncio/test_connection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import socket
import types
from errno import ECONNREFUSED
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -36,15 +37,16 @@ async def test_invalid_response(create_redis):
fake_stream = MockStream(raw + b"\r\n")

parser: _AsyncRESPBase = r.connection._parser
with mock.patch.object(parser, "_stream", fake_stream):
with pytest.raises(InvalidResponse) as cm:
await parser.read_response()

if isinstance(parser, _AsyncRESPBase):
assert str(cm.value) == f"Protocol Error: {raw!r}"
exp_err = f"Protocol Error: {raw!r}"
else:
assert (
str(cm.value) == f'Protocol error, got "{raw.decode()}" as reply type byte'
)
exp_err = f'Protocol error, got "{raw.decode()}" as reply type byte'

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

await r.connection.disconnect()


Expand Down Expand Up @@ -170,10 +172,9 @@ async def test_connect_timeout_error_without_retry():
conn._connect = mock.AsyncMock()
conn._connect.side_effect = socket.timeout

with pytest.raises(TimeoutError) as e:
with pytest.raises(TimeoutError, match="Timeout connecting to server"):
await conn.connect()
assert conn._connect.call_count == 1
assert str(e.value) == "Timeout connecting to server"


@pytest.mark.onlynoncluster
Expand Down Expand Up @@ -531,17 +532,14 @@ async def test_format_error_message(conn, error, expected_message):


async def test_network_connection_failure():
with pytest.raises(ConnectionError) as e:
exp_err = rf"^Error {ECONNREFUSED} connecting to 127.0.0.1:9999.(.+)$"
with pytest.raises(ConnectionError, match=exp_err):
redis = Redis(host="127.0.0.1", port=9999)
await redis.set("a", "b")
assert str(e.value).startswith("Error 111 connecting to 127.0.0.1:9999. Connect")


async def test_unix_socket_connection_failure():
with pytest.raises(ConnectionError) as e:
exp_err = "Error 2 connecting to unix:///tmp/a.sock. No such file or directory."
with pytest.raises(ConnectionError, match=exp_err):
redis = Redis(unix_socket_path="unix:///tmp/a.sock")
await redis.set("a", "b")
assert (
str(e.value)
== "Error 2 connecting to unix:///tmp/a.sock. No such file or directory."
)
18 changes: 7 additions & 11 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import threading
import types
from errno import ECONNREFUSED
from typing import Any
from unittest import mock
from unittest.mock import call, patch
Expand Down Expand Up @@ -44,9 +45,8 @@ def test_invalid_response(r):
raw = b"x"
parser = r.connection._parser
with mock.patch.object(parser._buffer, "readline", return_value=raw):
with pytest.raises(InvalidResponse) as cm:
with pytest.raises(InvalidResponse, match=f"Protocol Error: {raw!r}"):
parser.read_response()
assert str(cm.value) == f"Protocol Error: {raw!r}"


@skip_if_server_version_lt("4.0.0")
Expand Down Expand Up @@ -141,10 +141,9 @@ def test_connect_timeout_error_without_retry(self):
conn._connect = mock.Mock()
conn._connect.side_effect = socket.timeout

with pytest.raises(TimeoutError) as e:
with pytest.raises(TimeoutError, match="Timeout connecting to server"):
conn.connect()
assert conn._connect.call_count == 1
assert str(e.value) == "Timeout connecting to server"
self.clear(conn)


Expand Down Expand Up @@ -349,20 +348,17 @@ def test_format_error_message(conn, error, expected_message):


def test_network_connection_failure():
with pytest.raises(ConnectionError) as e:
exp_err = f"Error {ECONNREFUSED} connecting to localhost:9999. Connection refused."
with pytest.raises(ConnectionError, match=exp_err):
redis = Redis(port=9999)
redis.set("a", "b")
assert str(e.value) == "Error 111 connecting to localhost:9999. Connection refused."


def test_unix_socket_connection_failure():
with pytest.raises(ConnectionError) as e:
exp_err = "Error 2 connecting to unix:///tmp/a.sock. No such file or directory."
with pytest.raises(ConnectionError, match=exp_err):
redis = Redis(unix_socket_path="unix:///tmp/a.sock")
redis.set("a", "b")
assert (
str(e.value)
== "Error 2 connecting to unix:///tmp/a.sock. No such file or directory."
)


class TestUnitConnectionPool:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_multiprocessing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import contextlib
import multiprocessing
import sys

import pytest
import redis
Expand All @@ -8,6 +9,9 @@

from .conftest import _get_client

if sys.platform == "darwin":
multiprocessing.set_start_method("fork", force=True)


@contextlib.contextmanager
def exit_callback(callback, *args):
Expand Down
Loading