Skip to content

Commit c478458

Browse files
committed
CLIENT REPLY support, available since redis 3.2.0
Address redis#1546
1 parent cf39732 commit c478458

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

redis/commands.py

+18
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,24 @@ def client_getname(self):
376376
"Returns the current connection name"
377377
return self.execute_command('CLIENT GETNAME')
378378

379+
def client_reply(self, reply):
380+
"""Enable and disable redis server replies.
381+
``reply`` Must be ON OFF or SKIP,
382+
ON - The default most with server replies to commands
383+
OFF - Disable server responses to commands
384+
SKIP - Skip the response of the immediately following command.
385+
386+
Note: When setting OFF or SKIP replies, you will need a client object with a
387+
timeout specified in seconds, and will need to catch the TimeoutError.
388+
The test_client_reply unit test illustrates this, and conftest.py has a
389+
client with a timeout.
390+
See https://redis.io/commands/client-reply
391+
"""
392+
replies = ['ON', 'OFF', 'SKIP']
393+
if reply not in replies:
394+
raise DataError('CLIENT REPLY must be one of %r' % replies)
395+
return self.execute_command("CLIENT REPLY", reply)
396+
379397
def client_id(self):
380398
"Returns the current connection id"
381399
return self.execute_command('CLIENT ID')

tests/conftest.py

+6
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ def r(request):
100100
yield client
101101

102102

103+
@pytest.fixture()
104+
def r_timeout(request):
105+
with _get_client(redis.Redis, request, socket_timeout=1) as client:
106+
yield client
107+
108+
103109
@pytest.fixture()
104110
def r2(request):
105111
"A second client for tests that need multiple"

tests/test_commands.py

+13
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,19 @@ def test_client_pause(self, r):
478478
def test_client_unpause(self, r):
479479
assert r.client_unpause() == b'OK'
480480

481+
@skip_if_server_version_lt('3.2.0')
482+
def test_client_reply(self, r, r_timeout):
483+
assert r_timeout.client_reply('ON') == b'OK'
484+
with pytest.raises(exceptions.TimeoutError):
485+
r_timeout.client_reply('OFF')
486+
487+
r_timeout.client_reply('SKIP')
488+
489+
assert r_timeout.set('foo', 'bar')
490+
491+
# validate it was set
492+
assert r.get('foo') == b'bar'
493+
481494
def test_config_get(self, r):
482495
data = r.config_get()
483496
assert 'maxmemory' in data

0 commit comments

Comments
 (0)