diff --git a/go/api/base_client.go b/go/api/base_client.go index c94f30f608..5347f711f1 100644 --- a/go/api/base_client.go +++ b/go/api/base_client.go @@ -24,7 +24,7 @@ type BaseClient interface { StringCommands HashCommands ListCommands - + ConnectionManagementCommands // Close terminates the client by closing all associated resources. Close() } @@ -512,3 +512,31 @@ func (client *baseClient) RPush(key string, elements []string) (Result[int64], e return handleLongResponse(result) } + +func (client *baseClient) Ping() (string, error) { + result, err := client.executeCommand(C.Ping, []string{}) + if err != nil { + return "", err + } + + response, err := handleStringResponse(result) + if err != nil { + return "", err + } + return response.Value(), nil +} + +func (client *baseClient) PingWithMessage(message string) (string, error) { + args := []string{message} + + result, err := client.executeCommand(C.Ping, args) + if err != nil { + return "", err + } + + response, err := handleStringResponse(result) + if err != nil { + return "", err + } + return response.Value(), nil +} diff --git a/go/api/commands.go b/go/api/commands.go index ec9dbdbb6d..f48239c524 100644 --- a/go/api/commands.go +++ b/go/api/commands.go @@ -692,3 +692,37 @@ type HashCommands interface { // [valkey.io]: https://valkey.io/commands/hstrlen/ HStrLen(key string, field string) (Result[int64], error) } + +// ConnectionManagementCommands defines an interface for connection management-related commands. +// +// See [valkey.io] for details. +type ConnectionManagementCommands interface { + // Pings the server. + // + // If no argument is provided, returns "PONG". If a message is provided, returns the message. + // + // Return value: + // If no argument is provided, returns "PONG". + // If an argument is provided, returns the argument. + // + // For example: + // result, err := client.Ping("Hello") + // + // [valkey.io]: https://valkey.io/commands/ping/ + Ping() (string, error) + + // Pings the server with a custom message. + // + // If a message is provided, returns the message. + // If no argument is provided, returns "PONG". + // + // Return value: + // If no argument is provided, returns "PONG". + // If an argument is provided, returns the argument. + // + // For example: + // result, err := client.PingWithMessage("Hello") + // + // [valkey.io]: https://valkey.io/commands/ping/ + PingWithMessage(message string) (string, error) +} diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go index 4315cda5d7..b5b46c8fd0 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -588,6 +588,23 @@ func (suite *GlideTestSuite) TestGetDel_EmptyKey() { }) } +func (suite *GlideTestSuite) TestPing_NoArgument() { + suite.runWithDefaultClients(func(client api.BaseClient) { + result, err := client.Ping() + assert.Nil(suite.T(), err) + assert.Equal(suite.T(), "PONG", result) + }) +} + +func (suite *GlideTestSuite) TestPing_WithArgument() { + suite.runWithDefaultClients(func(client api.BaseClient) { + // Passing "Hello" as the message + result, err := client.PingWithMessage("Hello") + assert.Nil(suite.T(), err) + assert.Equal(suite.T(), "Hello", result) + }) +} + func (suite *GlideTestSuite) TestHSet_WithExistingKey() { suite.runWithDefaultClients(func(client api.BaseClient) { fields := map[string]string{"field1": "value1", "field2": "value2"}