Skip to content

Commit d640f01

Browse files
committed
mock can_read_destructive for parser
1 parent 534829e commit d640f01

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
lines changed

redis/asyncio/sentinel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ async def _connect_retry(self):
7171
# If address is fixed, it means that the connection
7272
# is not rotating to the next slave (if the connection pool is in replica mode)
7373
if self._is_address_fixed:
74-
self.connect_to(self.host, self.port)
74+
await self.connect_to((self.host, self.port))
7575
return
7676
await self._connect_to_sentinel()
77-
77+
7878
async def _connect_to_sentinel(self):
7979
# If same_server is False, connnect to master in master mode
8080
# and rotate to the next slave in slave mode

redis/sentinel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _connect_retry(self):
5959
self.connect_to((self.host, self.port))
6060
return
6161
self._connect_to_sentinel()
62-
62+
6363
def _connect_to_sentinel(self):
6464
# If same_server is False, connnect to master in master mode
6565
# and rotate to the next slave in slave mode

tests/test_asyncio/test_sentinel_managed_connection.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import socket
2-
from typing import Tuple
2+
from typing import Iterator, Tuple
33

44
import pytest
55
from redis.asyncio.retry import Retry
@@ -59,35 +59,46 @@ async def _connect_to_sentinel(self) -> None:
5959
self.host = f"host-{random.randint(0, 10)}"
6060
self.port = time.time()
6161

62+
async def connect_to(self, address: Tuple[str, int]) -> None:
63+
"""
64+
Do nothing, just mock.
65+
"""
66+
6267

6368
@pytest.fixture()
64-
def connection_pool_replica_mock() -> SentinelConnectionPool:
69+
def connection_pool_replica_mock() -> Iterator[SentinelConnectionPool]:
6570
sentinel_manager = Sentinel([["master", 400]])
6671
# Give a random slave
6772
sentinel_manager.discover_slaves = mock.AsyncMock(return_value=["replica", 5000])
68-
# Create connection pool with our mock connection object
69-
connection_pool = SentinelConnectionPool(
70-
"usasm",
71-
sentinel_manager,
72-
is_master=False,
73-
connection_class=SentinelManagedConnectionMock,
74-
)
75-
return connection_pool
73+
with mock.patch(
74+
"redis._parsers._AsyncRESP2Parser.can_read_destructive", return_value=False
75+
):
76+
# Create connection pool with our mock connection object
77+
connection_pool = SentinelConnectionPool(
78+
"usasm",
79+
sentinel_manager,
80+
is_master=False,
81+
connection_class=SentinelManagedConnectionMock,
82+
)
83+
yield connection_pool
7684

7785

7886
@pytest.fixture()
79-
def connection_pool_master_mock() -> SentinelConnectionPool:
87+
def connection_pool_master_mock() -> Iterator[SentinelConnectionPool]:
8088
sentinel_manager = Sentinel([["master", 400]])
8189
# Give a random slave
8290
sentinel_manager.discover_master = mock.AsyncMock(return_value=["replica", 5000])
83-
# Create connection pool with our mock connection object
84-
connection_pool = SentinelConnectionPool(
85-
"usasm",
86-
sentinel_manager,
87-
is_master=True,
88-
connection_class=SentinelManagedConnectionMock,
89-
)
90-
return connection_pool
91+
with mock.patch(
92+
"redis._parsers._AsyncRESP2Parser.can_read_destructive", return_value=False
93+
):
94+
# Create connection pool with our mock connection object
95+
connection_pool = SentinelConnectionPool(
96+
"usasm",
97+
sentinel_manager,
98+
is_master=True,
99+
connection_class=SentinelManagedConnectionMock,
100+
)
101+
yield connection_pool
91102

92103

93104
def same_address(

tests/test_sentinel_managed_connection.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,11 @@ def _connect_to_sentinel(self) -> None:
2323
self.host = f"host-{random.randint(0, 10)}"
2424
self.port = time.time()
2525

26-
27-
class SentinelManagedConnectionMock(SentinelManagedConnectionMock):
2826
def connect_to(self, address: Tuple[str, int]) -> None:
2927
"""
30-
This simulates the behavior of connect_to when
31-
:py:class:`~redis.SentinelConnectionPool`
32-
is in master mode.
33-
It'll try to connect to master. In this mock class,
34-
it'll just set the host and port without actually connecting.
28+
Do nothing, this is just to mock.
3529
"""
30+
pass
3631

3732

3833
@pytest.fixture()

0 commit comments

Comments
 (0)