Skip to content

Commit b755fd3

Browse files
committed
fix:improve TcpFakeServer
1 parent 836f8af commit b755fd3

21 files changed

+42
-21
lines changed

docs/about/changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ tags:
77
toc_depth: 2
88
---
99

10+
## v2.31.1 - 2025-09-01
11+
12+
### 🐛 Bug Fixes
13+
14+
- Sending lib_name and lib_version only when relevant #408
15+
- Improve documentation of using `TcpFakeServer` #409
16+
1017
## v2.31.0 - 2025-08-10
1118

1219
### 🚀 Features

docs/index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ r = redis.Redis(host=server_address[0], port=server_address[1])
5959
r.set("foo", "bar")
6060
assert r.get("foo") == b"bar"
6161

62+
63+
# When you are done with the server, you can stop it with:
64+
server.shutdown()
65+
server.server_close()
66+
t.join()
6267
```
6368

6469
### Use as a pytest fixture

fakeredis/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22

3-
from . import typing
3+
from . import _typing
44
from ._connection import (
55
FakeRedis,
66
FakeStrictRedis,
@@ -21,7 +21,7 @@ def __init__(self, *args, **kwargs):
2121
raise NotImplementedError("TcpFakeServer is only available in Python 3.11+")
2222

2323

24-
__version__ = typing.lib_version
24+
__version__ = _typing.lib_version
2525
__author__ = "Daniel Moran"
2626
__maintainer__ = "Daniel Moran"
2727
__email__ = "daniel@moransoftware.ca"

fakeredis/_commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from . import _msgs as msgs
1414
from ._helpers import null_terminate, SimpleError, Database
15-
from .typing import VersionType, ServerType
15+
from ._typing import VersionType, ServerType
1616

1717
MAX_STRING_SIZE = 512 * 1024 * 1024
1818
SUPPORTED_COMMANDS: Dict[str, "Signature"] = dict() # Dictionary of supported commands name => Signature

fakeredis/_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from fakeredis._helpers import FakeSelector, convert_args_to_redis_init_kwargs
1010
from . import _msgs as msgs
1111
from ._server import FakeBaseConnectionMixin, FakeServer, VersionType, ServerType
12-
from .typing import Self, lib_version, RaiseErrorTypes
12+
from ._typing import Self, lib_version, RaiseErrorTypes
1313

1414

1515
class FakeConnection(FakeBaseConnectionMixin, redis.Connection):

fakeredis/_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from fakeredis.model import AccessControlList
1111
from fakeredis._helpers import Database, FakeSelector
12-
from fakeredis.typing import VersionType, ServerType
12+
from fakeredis._typing import VersionType, ServerType
1313

1414
LOGGER = logging.getLogger("fakeredis")
1515

fakeredis/_tcp_server.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
from fakeredis import FakeRedis
88
from fakeredis import FakeServer
9-
from fakeredis.typing import VersionType, ServerType
9+
from fakeredis._typing import VersionType, ServerType
1010

1111
LOGGER = logging.getLogger("fakeredis")
1212
LOGGER.setLevel(logging.DEBUG)
13+
# logging.basicConfig(level=logging.DEBUG)
1314

1415

1516
def to_bytes(value) -> bytes:
@@ -90,17 +91,23 @@ def setup(self) -> None:
9091
self.server.clients[self.client_address] = self.current_client
9192

9293
def handle(self):
94+
LOGGER.debug(f"+++ {self.client_address[0]} connected")
9395
while True:
9496
try:
9597
self.data = self.reader.load()
9698
LOGGER.debug(f">>> {self.client_address[0]}: {self.data}")
99+
if len(self.data) == 1 and self.data[0].upper() == b"SHUTDOWN":
100+
LOGGER.debug(f"*** {self.client_address[0]} requested shutdown")
101+
break
97102
res = self.current_client.connection.execute_command(*self.data)
98103
LOGGER.debug(f"<<< {self.client_address[0]}: {res}")
99104
self.writer.dump(res)
100105
except Exception as e:
101106
LOGGER.debug(f"!!! {self.client_address[0]}: {e}")
102107
self.writer.dump(e)
103108
break
109+
self.server.socket.close()
110+
self.server.shutdown()
104111

105112
def finish(self) -> None:
106113
del self.server.clients[self.current_client.client_address]
@@ -116,6 +123,7 @@ def __init__(
116123
server_version: VersionType = (8, 0),
117124
):
118125
super().__init__(server_address, TCPFakeRequestHandler, bind_and_activate)
126+
self.allow_reuse_address = True
119127
self.fake_server = FakeServer(server_type=server_type, version=server_version)
120128
self.client_ids = count(0)
121129
self.clients: Dict[int, FakeRedis] = dict()

fakeredis/typing.py renamed to fakeredis/_typing.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import sys
2+
from typing import Tuple, Literal
23

34
import redis
45

56
if sys.version_info >= (3, 11):
6-
from typing import Self, Tuple, Literal
7+
from typing import Self
78
from asyncio import timeout as async_timeout
89
else:
910
from async_timeout import timeout as async_timeout
1011
from typing_extensions import Self
11-
from typing import Tuple, Literal
12+
1213
try:
1314
from importlib import metadata
1415
except ImportError: # for Python < 3.8

fakeredis/_valkey.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from ._connection import FakeRedisMixin
66
from .aioredis import FakeRedisMixin as FakeAsyncRedisMixin
7-
from .typing import Self
7+
from ._typing import Self
88

99

1010
def _validate_server_type(args_dict: Dict[str, Any]) -> None:

fakeredis/aioredis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from . import _msgs as msgs
1414
from ._helpers import SimpleError, convert_args_to_redis_init_kwargs
1515
from ._server import FakeBaseConnectionMixin, VersionType, FakeServer, ServerType
16-
from .typing import async_timeout, Self
16+
from ._typing import async_timeout, Self
1717

1818

1919
class AsyncFakeSocket(_fakesocket.FakeSocket):

0 commit comments

Comments
 (0)