Skip to content

Commit 2b470cb

Browse files
authored
Fix #2581 UnixDomainSocketConnection' object has no attribute '_command_packer' (#2583)
* Fix #2581 UnixDomainSocketConnection' object has no attribute '_command_packer' . Apparently there is no end-to-end tests for Unix sockets so automation didn't catch it. I assume that setting up domain sockets reliably in dockerized environment is not very trivial. Added test for pack_command specifically. * Figuring out why CI fails. Locally: " congratulations :)" * Fix the test. hiredis doesn't treat memoryviews differently.
1 parent 5cb5712 commit 2b470cb

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

redis/connection.py

+2
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,7 @@ def __init__(
11531153
retry=None,
11541154
redis_connect_func=None,
11551155
credential_provider: Optional[CredentialProvider] = None,
1156+
command_packer=None,
11561157
):
11571158
"""
11581159
Initialize a new UnixDomainSocketConnection.
@@ -1202,6 +1203,7 @@ def __init__(
12021203
self.set_parser(parser_class)
12031204
self._connect_callbacks = []
12041205
self._buffer_cutoff = 6000
1206+
self._command_packer = self._construct_command_packer(command_packer)
12051207

12061208
def repr_pieces(self):
12071209
pieces = [("path", self.path), ("db", self.db)]

tests/test_connection.py

+43-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77

88
import redis
99
from redis.backoff import NoBackoff
10-
from redis.connection import Connection, HiredisParser, PythonParser
10+
from redis.connection import (
11+
Connection,
12+
HiredisParser,
13+
PythonParser,
14+
SSLConnection,
15+
UnixDomainSocketConnection,
16+
)
1117
from redis.exceptions import ConnectionError, InvalidResponse, TimeoutError
1218
from redis.retry import Retry
1319
from redis.utils import HIREDIS_AVAILABLE
@@ -163,3 +169,39 @@ def test_connection_parse_response_resume(r: redis.Redis, parser_class):
163169
pytest.fail("didn't receive a response")
164170
assert response
165171
assert i > 0
172+
173+
174+
@pytest.mark.onlynoncluster
175+
@pytest.mark.parametrize(
176+
"Class",
177+
[
178+
Connection,
179+
SSLConnection,
180+
UnixDomainSocketConnection,
181+
],
182+
)
183+
def test_pack_command(Class):
184+
"""
185+
This test verifies that the pack_command works
186+
on all supported connections. #2581
187+
"""
188+
cmd = (
189+
"HSET",
190+
"foo",
191+
"key",
192+
"value1",
193+
b"key_b",
194+
b"bytes str",
195+
b"key_i",
196+
67,
197+
"key_f",
198+
3.14159265359,
199+
)
200+
expected = (
201+
b"*10\r\n$4\r\nHSET\r\n$3\r\nfoo\r\n$3\r\nkey\r\n$6\r\nvalue1\r\n"
202+
b"$5\r\nkey_b\r\n$9\r\nbytes str\r\n$5\r\nkey_i\r\n$2\r\n67\r\n$5"
203+
b"\r\nkey_f\r\n$13\r\n3.14159265359\r\n"
204+
)
205+
206+
actual = Class().pack_command(*cmd)[0]
207+
assert actual == expected, f"actual = {actual}, expected = {expected}"

0 commit comments

Comments
 (0)