Skip to content

Commit

Permalink
add message param for ping
Browse files Browse the repository at this point in the history
Signed-off-by: root <[email protected]>
  • Loading branch information
spyinx committed Dec 11, 2024
1 parent 1f6b63c commit 4f02122
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 4 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,10 @@ def test_object(self, r):

def test_ping(self, r):
assert r.ping()
assert r.ping(0)
assert r.ping("Valkey")
assert r.ping(" Valkey ")
assert r.ping("Valkey", test="a")

@pytest.mark.onlynoncluster
def test_quit(self, r):
Expand Down
10 changes: 8 additions & 2 deletions valkey/_parsers/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime

from valkey.utils import str_if_bytes
from valkey.utils import str_if_bytes, safe_str


def timestamp_to_datetime(response):
Expand Down Expand Up @@ -684,6 +684,12 @@ def parse_set_result(response, **options):
return response and str_if_bytes(response) == "OK"


def parse_ping(response, **options):
response = str_if_bytes(response)
message = "PONG" if options.get("message") is None else options.get("message")
return response == safe_str(message)


def string_keys_to_dict(key_string, callback):
return dict.fromkeys(key_string.split(), callback)

Expand Down Expand Up @@ -747,7 +753,7 @@ def string_keys_to_dict(key_string, callback):
"MEMORY PURGE": bool_ok,
"MODULE LOAD": bool,
"MODULE UNLOAD": bool,
"PING": lambda r: str_if_bytes(r) == "PONG",
"PING": parse_ping,
"PUBSUB NUMSUB": parse_pubsub_numsub,
"PUBSUB SHARDNUMSUB": parse_pubsub_numsub,
"QUIT": bool_ok,
Expand Down
5 changes: 3 additions & 2 deletions valkey/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1203,13 +1203,14 @@ def latency_reset(self, *events: str) -> ResponseT:
"""
return self.execute_command("LATENCY RESET", *events)

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

def quit(self, **kwargs) -> ResponseT:
"""
Expand Down

0 comments on commit 4f02122

Please sign in to comment.