Skip to content

Commit 9b4a44b

Browse files
author
zhousheng06
committed
Fix incorrect attribute reuse
Fix incorrect attribute reuse Adjusting test cases for CacheProxyConnection, cache key should not reuse in different `read_response`. lint fix
1 parent 8f2276e commit 9b4a44b

File tree

2 files changed

+6
-23
lines changed

2 files changed

+6
-23
lines changed

redis/connection.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,9 +870,11 @@ def read_response(
870870
and self._cache.get(self._current_command_cache_key).status
871871
!= CacheEntryStatus.IN_PROGRESS
872872
):
873-
return copy.deepcopy(
873+
res = copy.deepcopy(
874874
self._cache.get(self._current_command_cache_key).cache_value
875875
)
876+
self._current_command_cache_key = None
877+
return res
876878

877879
response = self._conn.read_response(
878880
disable_decoding=disable_decoding,
@@ -898,6 +900,8 @@ def read_response(
898900
cache_entry.cache_value = response
899901
self._cache.set(cache_entry)
900902

903+
self._current_command_cache_key = None
904+
901905
return response
902906

903907
def pack_command(self, *args):

tests/test_connection.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -468,24 +468,6 @@ def test_read_response_returns_cached_reply(self, mock_cache, mock_connection):
468468
status=CacheEntryStatus.IN_PROGRESS,
469469
connection_ref=mock_connection,
470470
),
471-
CacheEntry(
472-
cache_key=CacheKey(command="GET", redis_keys=("foo",)),
473-
cache_value=b"bar",
474-
status=CacheEntryStatus.VALID,
475-
connection_ref=mock_connection,
476-
),
477-
CacheEntry(
478-
cache_key=CacheKey(command="GET", redis_keys=("foo",)),
479-
cache_value=b"bar",
480-
status=CacheEntryStatus.VALID,
481-
connection_ref=mock_connection,
482-
),
483-
CacheEntry(
484-
cache_key=CacheKey(command="GET", redis_keys=("foo",)),
485-
cache_value=b"bar",
486-
status=CacheEntryStatus.VALID,
487-
connection_ref=mock_connection,
488-
),
489471
]
490472
mock_connection.send_command.return_value = Any
491473
mock_connection.read_response.return_value = b"bar"
@@ -496,9 +478,9 @@ def test_read_response_returns_cached_reply(self, mock_cache, mock_connection):
496478
)
497479
proxy_connection.send_command(*["GET", "foo"], **{"keys": ["foo"]})
498480
assert proxy_connection.read_response() == b"bar"
481+
assert proxy_connection._current_command_cache_key is None
499482
assert proxy_connection.read_response() == b"bar"
500483

501-
mock_connection.read_response.assert_called_once()
502484
mock_cache.set.assert_has_calls(
503485
[
504486
call(
@@ -525,9 +507,6 @@ def test_read_response_returns_cached_reply(self, mock_cache, mock_connection):
525507
call(CacheKey(command="GET", redis_keys=("foo",))),
526508
call(CacheKey(command="GET", redis_keys=("foo",))),
527509
call(CacheKey(command="GET", redis_keys=("foo",))),
528-
call(CacheKey(command="GET", redis_keys=("foo",))),
529-
call(CacheKey(command="GET", redis_keys=("foo",))),
530-
call(CacheKey(command="GET", redis_keys=("foo",))),
531510
]
532511
)
533512

0 commit comments

Comments
 (0)