Skip to content

Commit 10f3a4b

Browse files
Adding unit text fixes to improve compatibility with MacOS. (redis#3486)
* Adding unit text fixes to improve compatibility with MacOS. * Applying review comments * Unifying the exception msg validation pattern for both test_connection.py files --------- Co-authored-by: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com>
1 parent c60c41c commit 10f3a4b

File tree

4 files changed

+27
-28
lines changed

4 files changed

+27
-28
lines changed

docker-compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ services:
103103
- all
104104

105105
redis-stack:
106-
image: ${REDIS_STACK_IMAGE:-redis/redis-stack-server:edge}
106+
image: ${REDIS_STACK_IMAGE:-redis/redis-stack-server:latest}
107107
container_name: redis-stack
108108
ports:
109109
- 6479:6379
@@ -112,6 +112,7 @@ services:
112112
profiles:
113113
- standalone
114114
- all-stack
115+
- all
115116

116117
redis-stack-graph:
117118
image: redis/redis-stack-server:6.2.6-v15

tests/test_asyncio/test_connection.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import socket
33
import types
4+
from errno import ECONNREFUSED
45
from unittest.mock import patch
56

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

3839
parser: _AsyncRESPBase = r.connection._parser
39-
with mock.patch.object(parser, "_stream", fake_stream):
40-
with pytest.raises(InvalidResponse) as cm:
41-
await parser.read_response()
40+
4241
if isinstance(parser, _AsyncRESPBase):
43-
assert str(cm.value) == f"Protocol Error: {raw!r}"
42+
exp_err = f"Protocol Error: {raw!r}"
4443
else:
45-
assert (
46-
str(cm.value) == f'Protocol error, got "{raw.decode()}" as reply type byte'
47-
)
44+
exp_err = f'Protocol error, got "{raw.decode()}" as reply type byte'
45+
46+
with mock.patch.object(parser, "_stream", fake_stream):
47+
with pytest.raises(InvalidResponse, match=exp_err):
48+
await parser.read_response()
49+
4850
await r.connection.disconnect()
4951

5052

@@ -170,10 +172,9 @@ async def test_connect_timeout_error_without_retry():
170172
conn._connect = mock.AsyncMock()
171173
conn._connect.side_effect = socket.timeout
172174

173-
with pytest.raises(TimeoutError) as e:
175+
with pytest.raises(TimeoutError, match="Timeout connecting to server"):
174176
await conn.connect()
175177
assert conn._connect.call_count == 1
176-
assert str(e.value) == "Timeout connecting to server"
177178

178179

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

532533

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

539540

540541
async def test_unix_socket_connection_failure():
541-
with pytest.raises(ConnectionError) as e:
542+
exp_err = "Error 2 connecting to unix:///tmp/a.sock. No such file or directory."
543+
with pytest.raises(ConnectionError, match=exp_err):
542544
redis = Redis(unix_socket_path="unix:///tmp/a.sock")
543545
await redis.set("a", "b")
544-
assert (
545-
str(e.value)
546-
== "Error 2 connecting to unix:///tmp/a.sock. No such file or directory."
547-
)

tests/test_connection.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import threading
66
import types
7+
from errno import ECONNREFUSED
78
from typing import Any
89
from unittest import mock
910
from unittest.mock import call, patch
@@ -44,9 +45,8 @@ def test_invalid_response(r):
4445
raw = b"x"
4546
parser = r.connection._parser
4647
with mock.patch.object(parser._buffer, "readline", return_value=raw):
47-
with pytest.raises(InvalidResponse) as cm:
48+
with pytest.raises(InvalidResponse, match=f"Protocol Error: {raw!r}"):
4849
parser.read_response()
49-
assert str(cm.value) == f"Protocol Error: {raw!r}"
5050

5151

5252
@skip_if_server_version_lt("4.0.0")
@@ -141,10 +141,9 @@ def test_connect_timeout_error_without_retry(self):
141141
conn._connect = mock.Mock()
142142
conn._connect.side_effect = socket.timeout
143143

144-
with pytest.raises(TimeoutError) as e:
144+
with pytest.raises(TimeoutError, match="Timeout connecting to server"):
145145
conn.connect()
146146
assert conn._connect.call_count == 1
147-
assert str(e.value) == "Timeout connecting to server"
148147
self.clear(conn)
149148

150149

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

350349

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

357356

358357
def test_unix_socket_connection_failure():
359-
with pytest.raises(ConnectionError) as e:
358+
exp_err = "Error 2 connecting to unix:///tmp/a.sock. No such file or directory."
359+
with pytest.raises(ConnectionError, match=exp_err):
360360
redis = Redis(unix_socket_path="unix:///tmp/a.sock")
361361
redis.set("a", "b")
362-
assert (
363-
str(e.value)
364-
== "Error 2 connecting to unix:///tmp/a.sock. No such file or directory."
365-
)
366362

367363

368364
class TestUnitConnectionPool:

tests/test_multiprocessing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import contextlib
22
import multiprocessing
3+
import sys
34

45
import pytest
56
import redis
@@ -8,6 +9,9 @@
89

910
from .conftest import _get_client
1011

12+
if sys.platform == "darwin":
13+
multiprocessing.set_start_method("fork", force=True)
14+
1115

1216
@contextlib.contextmanager
1317
def exit_callback(callback, *args):

0 commit comments

Comments
 (0)