Skip to content

Commit 0f5e079

Browse files
committed
make mock class have same behavior as actual class
1 parent c75db8d commit 0f5e079

File tree

3 files changed

+26
-52
lines changed

3 files changed

+26
-52
lines changed

redis/sentinel.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ def _connect_retry(self):
5858
if self._is_address_fixed:
5959
self.connect_to((self.host, self.port))
6060
return
61+
self._connect_to_sentinel()
62+
63+
def _connect_to_sentinel(self):
6164
# If same_server is False, connnect to master in master mode
6265
# and rotate to the next slave in slave mode
6366
if self.connection_pool.is_master:

tests/test_asyncio/test_sentinel_managed_connection.py

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,21 @@ async def mock_connect():
4343

4444

4545
class SentinelManagedConnectionMock(SentinelManagedConnection):
46-
async def connect_to_same_address(self) -> None:
47-
pass
48-
49-
async def can_read_destructive(self) -> bool:
50-
# Mock this function to always return False.
51-
# Trrue means there's still data to be read and hence we can't reconnect
52-
# to this connection yet
53-
return False
54-
55-
56-
class SentinelManagedConnectionMockForReplicaMode(SentinelManagedConnectionMock):
57-
async def connect(self) -> None:
46+
async def _connect_to_sentinel(self) -> None:
5847
"""
59-
This simulates the behavior of connect when
48+
This simulates the behavior of _connect_to_sentinel when
6049
:py:class:`~redis.SentinelConnectionPool`
6150
is in replica mode.
6251
It'll call rotate_slaves and connect to the next replica.
6352
"""
64-
import random
65-
import time
66-
67-
self.host = f"host-{random.randint(0, 10)}"
68-
self.port = time.time()
69-
53+
if self.connection_pool.is_master:
54+
self.host, self.port = ("master", 1)
55+
else:
56+
import random
57+
import time
7058

71-
class SentinelManagedConnectionMockForMasterMode(SentinelManagedConnectionMock):
72-
async def connect_to(self, address: Tuple[str, int]) -> None:
73-
"""
74-
This simulates the behavior of connect_to when
75-
:py:class:`~redis.SentinelConnectionPool`
76-
is in master mode.
77-
It'll try to connect to master. In this mock class,
78-
it'll just set the host and port without actually connecting.
79-
"""
80-
self.host, self.port = address
59+
self.host = f"host-{random.randint(0, 10)}"
60+
self.port = time.time()
8161

8262

8363
@pytest.fixture()
@@ -90,7 +70,7 @@ def connection_pool_replica_mock() -> SentinelConnectionPool:
9070
"usasm",
9171
sentinel_manager,
9272
is_master=False,
93-
connection_class=SentinelManagedConnectionMockForReplicaMode,
73+
connection_class=SentinelManagedConnectionMock,
9474
)
9575
return connection_pool
9676

@@ -105,7 +85,7 @@ def connection_pool_master_mock() -> SentinelConnectionPool:
10585
"usasm",
10686
sentinel_manager,
10787
is_master=True,
108-
connection_class=SentinelManagedConnectionMockForMasterMode,
88+
connection_class=SentinelManagedConnectionMock,
10989
)
11090
return connection_pool
11191

tests/test_sentinel_managed_connection.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,24 @@
77

88

99
class SentinelManagedConnectionMock(SentinelManagedConnection):
10-
def connect_to_same_address(self) -> None:
11-
pass
12-
13-
def can_read_destructive(self) -> bool:
14-
# Mock this function to always return False.
15-
# Trrue means there's still data to be read and hence we can't reconnect
16-
# to this connection yet
17-
return False
18-
19-
20-
class SentinelManagedConnectionMockForReplicaMode(SentinelManagedConnectionMock):
21-
def connect(self) -> None:
10+
def _connect_to_sentinel(self) -> None:
2211
"""
23-
This simulates the behavior of connect when
12+
This simulates the behavior of _connect_to_sentinel when
2413
:py:class:`~redis.SentinelConnectionPool`
2514
is in replica mode.
2615
It'll call rotate_slaves and connect to the next replica.
2716
"""
28-
import random
29-
import time
17+
if self.connection_pool.is_master:
18+
self.host, self.port = ("master", 1)
19+
else:
20+
import random
21+
import time
3022

31-
self.host = f"host-{random.randint(0, 10)}"
32-
self.port = time.time()
23+
self.host = f"host-{random.randint(0, 10)}"
24+
self.port = time.time()
3325

3426

35-
class SentinelManagedConnectionMockForMasterMode(SentinelManagedConnectionMock):
27+
class SentinelManagedConnectionMock(SentinelManagedConnectionMock):
3628
def connect_to(self, address: Tuple[str, int]) -> None:
3729
"""
3830
This simulates the behavior of connect_to when
@@ -41,7 +33,6 @@ def connect_to(self, address: Tuple[str, int]) -> None:
4133
It'll try to connect to master. In this mock class,
4234
it'll just set the host and port without actually connecting.
4335
"""
44-
self.host, self.port = address
4536

4637

4738
@pytest.fixture()
@@ -54,7 +45,7 @@ def connection_pool_replica_mock() -> SentinelConnectionPool:
5445
"usasm",
5546
sentinel_manager,
5647
is_master=False,
57-
connection_class=SentinelManagedConnectionMockForReplicaMode,
48+
connection_class=SentinelManagedConnectionMock,
5849
)
5950
return connection_pool
6051

@@ -69,7 +60,7 @@ def connection_pool_master_mock() -> SentinelConnectionPool:
6960
"usasm",
7061
sentinel_manager,
7162
is_master=True,
72-
connection_class=SentinelManagedConnectionMockForMasterMode,
63+
connection_class=SentinelManagedConnectionMock,
7364
)
7465
return connection_pool
7566

0 commit comments

Comments
 (0)