diff --git a/tests/test_commands.py b/tests/test_commands.py index 085f74c4..d74a948e 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -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): diff --git a/valkey/_parsers/helpers.py b/valkey/_parsers/helpers.py index 464c9e88..c8f31f92 100644 --- a/valkey/_parsers/helpers.py +++ b/valkey/_parsers/helpers.py @@ -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): @@ -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) @@ -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, diff --git a/valkey/commands/core.py b/valkey/commands/core.py index a366a730..84eaa2b7 100644 --- a/valkey/commands/core.py +++ b/valkey/commands/core.py @@ -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: """