Skip to content

Commit 4f02122

Browse files
committed
add message param for ping
Signed-off-by: root <qiwu391@gmail.com>
1 parent 1f6b63c commit 4f02122

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

tests/test_commands.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,10 @@ def test_object(self, r):
864864

865865
def test_ping(self, r):
866866
assert r.ping()
867+
assert r.ping(0)
868+
assert r.ping("Valkey")
869+
assert r.ping(" Valkey ")
870+
assert r.ping("Valkey", test="a")
867871

868872
@pytest.mark.onlynoncluster
869873
def test_quit(self, r):

valkey/_parsers/helpers.py

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

3-
from valkey.utils import str_if_bytes
3+
from valkey.utils import str_if_bytes, safe_str
44

55

66
def timestamp_to_datetime(response):
@@ -684,6 +684,12 @@ def parse_set_result(response, **options):
684684
return response and str_if_bytes(response) == "OK"
685685

686686

687+
def parse_ping(response, **options):
688+
response = str_if_bytes(response)
689+
message = "PONG" if options.get("message") is None else options.get("message")
690+
return response == safe_str(message)
691+
692+
687693
def string_keys_to_dict(key_string, callback):
688694
return dict.fromkeys(key_string.split(), callback)
689695

@@ -747,7 +753,7 @@ def string_keys_to_dict(key_string, callback):
747753
"MEMORY PURGE": bool_ok,
748754
"MODULE LOAD": bool,
749755
"MODULE UNLOAD": bool,
750-
"PING": lambda r: str_if_bytes(r) == "PONG",
756+
"PING": parse_ping,
751757
"PUBSUB NUMSUB": parse_pubsub_numsub,
752758
"PUBSUB SHARDNUMSUB": parse_pubsub_numsub,
753759
"QUIT": bool_ok,

valkey/commands/core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,13 +1203,14 @@ def latency_reset(self, *events: str) -> ResponseT:
12031203
"""
12041204
return self.execute_command("LATENCY RESET", *events)
12051205

1206-
def ping(self, **kwargs) -> ResponseT:
1206+
def ping(self, message=None, **kwargs) -> ResponseT:
12071207
"""
12081208
Ping the Valkey server
12091209
12101210
For more information see https://valkey.io/commands/ping
12111211
"""
1212-
return self.execute_command("PING", **kwargs)
1212+
args = ["PING", message] if message is not None else ["PING"]
1213+
return self.execute_command(*args, message=message, **kwargs)
12131214

12141215
def quit(self, **kwargs) -> ResponseT:
12151216
"""

0 commit comments

Comments
 (0)