From c9d2620e3f568443038565d80a27da0b7cd6334b Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Sun, 22 Aug 2021 11:07:26 +0300 Subject: [PATCH 1/6] general --- redis/commands.py | 18 +++++++++++++++--- tests/test_commands.py | 4 ++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/redis/commands.py b/redis/commands.py index 003b0f1bcc..fc60de9085 100644 --- a/redis/commands.py +++ b/redis/commands.py @@ -369,14 +369,26 @@ def client_unblock(self, client_id, error=False): args.append(b'ERROR') return self.execute_command(*args) - def client_pause(self, timeout): + def client_pause(self, timeout, all=True): """ Suspend all the Redis clients for the specified amount of time :param timeout: milliseconds to pause clients - """ + :param all: If true (default) all client commands are blocked. + otherwise, clients are only blocked if they attempt to execute + a write command. + For the WRITE mode, some commands have special behavior: + EVAL/EVALSHA: Will block client for all scripts. + PUBLISH: Will block client. + PFCOUNT: Will block client. + WAIT: Acknowledgments will be delayed, so this command will + appear blocked. + """ + args = ['CLIENT PAUSE', str(timeout)] if not isinstance(timeout, int): raise DataError("CLIENT PAUSE timeout must be an integer") - return self.execute_command('CLIENT PAUSE', str(timeout)) + if not all: + args.append('WRITE') + return self.execute_command(*args) def client_unpause(self): """ diff --git a/tests/test_commands.py b/tests/test_commands.py index 3c87dd9dd6..b1b2457b14 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -424,6 +424,10 @@ def test_client_pause(self, r): with pytest.raises(exceptions.RedisError): r.client_pause(timeout='not an integer') + @skip_if_server_version_lt('6.2.0') + def test_client_pause_write(self, r): + assert r.client_pause(timeout=10000, all=False) + @skip_if_server_version_lt('6.2.0') def test_client_unpause(self, r): assert r.client_unpause() == b'OK' From 498044d451cfa112cda5631a54a578ef10e125f4 Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Mon, 23 Aug 2021 17:27:04 +0300 Subject: [PATCH 2/6] test - still not ready --- tests/test_commands.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index b1b2457b14..9ed007ef6e 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -425,8 +425,22 @@ def test_client_pause(self, r): r.client_pause(timeout='not an integer') @skip_if_server_version_lt('6.2.0') - def test_client_pause_write(self, r): - assert r.client_pause(timeout=10000, all=False) + def test_client_pause_write(self, r, r2): + r.client_setname('redis-py-c1') + r2.client_setname('redis-py-c2') + """clients = [client for client in r.client_list() + if client.get('name') in ['redis-py-c1', 'redis-py-c2']] + assert len(clients) == 2 + + clients_by_name = dict([(client.get('name'), client) + for client in clients])""" + + assert r.client_pause(timeout=100000000, all=False) + r.set('FOO', 'BAR') + assert r.get('FOO') != 'BAR' + assert r.ping() + r2.client_unpause() + @skip_if_server_version_lt('6.2.0') def test_client_unpause(self, r): From 30a63fe35d98e737f854a10aa1e8949c70bf9e5b Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Mon, 23 Aug 2021 18:05:51 +0300 Subject: [PATCH 3/6] test --- tests/test_commands.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index 9ed007ef6e..d73f25c21c 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1,5 +1,7 @@ import binascii import datetime +import threading + import pytest import re import redis @@ -434,13 +436,18 @@ def test_client_pause_write(self, r, r2): clients_by_name = dict([(client.get('name'), client) for client in clients])""" + assert r.client_pause(timeout=1000, all=False) - assert r.client_pause(timeout=100000000, all=False) + x = threading.Thread(target=r2, args=(1,)) + x.start() r.set('FOO', 'BAR') assert r.get('FOO') != 'BAR' assert r.ping() r2.client_unpause() + def thread_function(self, r): + assert r.ping() + r.client_unpause() @skip_if_server_version_lt('6.2.0') def test_client_unpause(self, r): From 742f6dc7a0a22ffc3bfbc1ac5ceb7feaba9077c6 Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Tue, 23 Nov 2021 11:01:19 +0100 Subject: [PATCH 4/6] TRY TO TEST --- tests/test_commands.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_commands.py b/tests/test_commands.py index dbd04429b4..7a2a4c3d24 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -489,6 +489,14 @@ def test_client_pause(self, r): with pytest.raises(exceptions.RedisError): r.client_pause(timeout='not an integer') + @skip_if_server_version_lt('6.2.0') + def test_client_pause_all(self, r, r2): + assert r.client_pause(1, all=False) + assert r2.set('foo', 'bar') + assert r2.get('foo') == b'bar' + assert r.get('foo') == b'bar' + + @skip_if_server_version_lt('6.2.0') @skip_if_redis_enterprise def test_client_unpause(self, r): From 18df21e62b88498135a73fda55134f72c85eb6f6 Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Tue, 23 Nov 2021 11:01:19 +0100 Subject: [PATCH 5/6] TRY TO TEST --- redis/commands/core.py | 2 +- tests/test_commands.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/redis/commands/core.py b/redis/commands/core.py index 96135ed6d4..be41093ed6 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -475,7 +475,7 @@ def client_unblock(self, client_id, error=False): args.append(b'ERROR') return self.execute_command(*args) - def client_pause(self, timeout): + def client_pause(self, timeout, all=True): """ Suspend all the Redis clients for the specified amount of time :param timeout: milliseconds to pause clients diff --git a/tests/test_commands.py b/tests/test_commands.py index dbd04429b4..7a2a4c3d24 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -489,6 +489,14 @@ def test_client_pause(self, r): with pytest.raises(exceptions.RedisError): r.client_pause(timeout='not an integer') + @skip_if_server_version_lt('6.2.0') + def test_client_pause_all(self, r, r2): + assert r.client_pause(1, all=False) + assert r2.set('foo', 'bar') + assert r2.get('foo') == b'bar' + assert r.get('foo') == b'bar' + + @skip_if_server_version_lt('6.2.0') @skip_if_redis_enterprise def test_client_unpause(self, r): From f6aa3b4ae2b4e9d283ac632e5489c8b1d2fbb53d Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Tue, 23 Nov 2021 11:15:18 +0100 Subject: [PATCH 6/6] flake8 --- tests/test_commands.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index 7a2a4c3d24..760b9eb48c 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -496,7 +496,6 @@ def test_client_pause_all(self, r, r2): assert r2.get('foo') == b'bar' assert r.get('foo') == b'bar' - @skip_if_server_version_lt('6.2.0') @skip_if_redis_enterprise def test_client_unpause(self, r):