Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add message param for ping #149

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(" hello ")
assert r.ping("abc", test="a")

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

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


def timestamp_to_datetime(response):
Expand Down Expand Up @@ -684,6 +687,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 = options.get("message", "PONG")
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 +756,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
7 changes: 5 additions & 2 deletions valkey/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1203,13 +1203,16 @@ 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"]
if message is not None:
kwargs["message"] = message
return self.execute_command(*args, **kwargs)

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