Skip to content

Commit

Permalink
Fixed review comments and failing tests
Browse files Browse the repository at this point in the history
Signed-off-by: Niharika Bhavaraju <[email protected]>
  • Loading branch information
niharikabhavaraju committed Jan 29, 2025
1 parent ac3b8ff commit a3f6549
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 140 deletions.
134 changes: 3 additions & 131 deletions go/api/glide_cluster_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ func (client *GlideClusterClient) InfoWithOptions(options ClusterInfoOptions) (C
// For example:
//
// route := config.SimpleNodeRoute(config.RandomRoute)
// result, err := client.CustomCommand([]string{"ping"}, route)
// result.Value().(string): "PONG"
// result, err := client.CustomCommandWithRoute([]string{"ping"}, route)
// result.SingleValue().(string): "PONG"
//
// [Valkey GLIDE Wiki]: https://github.com/valkey-io/valkey-glide/wiki/General-Concepts#custom-command
func (client *GlideClusterClient) CustomCommandWithRoute(
Expand All @@ -205,139 +205,11 @@ func (client *GlideClusterClient) CustomCommandWithRoute(
return createClusterValue[interface{}](data), nil
}

// Gets information and statistics about the server.
//
// The command will be routed to all primary nodes.
//
// See [valkey.io] for details.
//
// Return value:
//
// A map where each address is the key and its corresponding node response is the information for the default sections.
//
// Example:
//
// response, err := clusterClient.Info(opts)
// if err != nil {
// // handle error
// }
// for node, data := range response {
// fmt.Printf("%s node returned %s\n", node, data)
// }
//
// [valkey.io]: https://valkey.io/commands/info/
func (client *GlideClusterClient) Info() (map[string]string, error) {
result, err := client.executeCommand(C.Info, []string{})
if err != nil {
return nil, err
}

return handleStringToStringMapResponse(result)
}

// Gets information and statistics about the server.
//
// The command will be routed to all primary nodes, unless `route` in [ClusterInfoOptions] is provided.
//
// See [valkey.io] for details.
//
// Parameters:
//
// options - Additional command parameters, see [ClusterInfoOptions] for more details.
//
// Return value:
//
// When specifying a route other than a single node or when route is not given,
// it returns a map where each address is the key and its corresponding node response is the value.
// When a single node route is given, command returns a string containing the information for the sections requested.
//
// Example:
//
// opts := api.ClusterInfoOptions{
// InfoOptions: &api.InfoOptions{Sections: []api.Section{api.Server}},
// Route: api.RandomRoute.ToPtr(),
// }
// response, err := clusterClient.InfoWithOptions(opts)
// if err != nil {
// // handle error
// }
// // Command sent to a single random node via RANDOM route, expecting SingleValue result as a `string`.
// fmt.Println(response.SingleValue())
//
// [valkey.io]: https://valkey.io/commands/info/
func (client *GlideClusterClient) InfoWithOptions(options ClusterInfoOptions) (ClusterValue[string], error) {
if options.Route == nil {
response, err := client.executeCommand(C.Info, options.toArgs())
if err != nil {
return createEmptyClusterValue[string](), err
}
data, err := handleStringToStringMapResponse(response)
if err != nil {
return createEmptyClusterValue[string](), err
}
return createClusterMultiValue[string](data), nil
}
response, err := client.executeCommandWithRoute(C.Info, options.toArgs(), *options.Route)
if err != nil {
return createEmptyClusterValue[string](), err
}
if (*options.Route).IsMultiNode() {
data, err := handleStringToStringMapResponse(response)
if err != nil {
return createEmptyClusterValue[string](), err
}
return createClusterMultiValue[string](data), nil
}
data, err := handleStringResponse(response)
if err != nil {
return createEmptyClusterValue[string](), err
}
return createClusterSingleValue[string](data), nil
}

// CustomCommandWithRoute executes a single command, specified by args, without checking inputs. Every part of the command,
// including the command name and subcommands, should be added as a separate value in args. The returning value depends on
// the executed command.
//
// See [Valkey GLIDE Wiki] for details on the restrictions and limitations of the custom command API.
//
// Parameters:
//
// args - Arguments for the custom command including the command name.
// route - Specifies the routing configuration for the command. The client will route the
// command to the nodes defined by route.
//
// Return value:
//
// The returning value depends on the executed command and route.
//
// For example:
//
// route := config.SimpleNodeRoute(config.RandomRoute)
// result, err := client.CustomCommand([]string{"ping"}, route)
// result.Value().(string): "PONG"
//
// [Valkey GLIDE Wiki]: https://github.com/valkey-io/valkey-glide/wiki/General-Concepts#custom-command
func (client *GlideClusterClient) CustomCommandWithRoute(
args []string,
route config.Route,
) (ClusterValue[interface{}], error) {
res, err := client.executeCommandWithRoute(C.CustomCommand, args, route)
if err != nil {
return CreateEmptyClusterValue(), err
}
data, err := handleInterfaceResponse(res)
if err != nil {
return CreateEmptyClusterValue(), err
}
return CreateClusterValue(data), nil
}

// Pings the server.
//
// Return value:
//
// Returns "PONG".
// Returns "PONG".
//
// For example:
//
Expand Down
24 changes: 15 additions & 9 deletions go/integTest/cluster_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,17 @@ func (suite *GlideTestSuite) TestInfoCluster() {
}
}
}

func (suite *GlideTestSuite) TestClusterCustomCommandWithRoute_Info() {
client := suite.defaultClusterClient()
route := config.SimpleNodeRoute(config.AllPrimaries)
result, err := client.CustomCommandWithRoute([]string{"INFO"}, route)
assert.Nil(suite.T(), err)
for _, value := range result.Value().(map[string]interface{}) {
assert.True(suite.T(), strings.Contains(value.(string), "# Stats"))
assert.True(suite.T(), result.IsMultiValue())

multiValue := result.MultiValue()
for nodeName, value := range multiValue {
assert.True(suite.T(), strings.Contains(value.(string), "# Stats"), "Node %s info should contain '# Stats'", nodeName)
}
}

Expand All @@ -122,24 +126,29 @@ func (suite *GlideTestSuite) TestClusterCustomCommandWithRoute_Echo() {
route := config.SimpleNodeRoute(config.RandomRoute)
result, err := client.CustomCommandWithRoute([]string{"ECHO", "GO GLIDE GO"}, route)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), "GO GLIDE GO", result.Value().(string))
assert.True(suite.T(), result.IsSingleValue())
assert.Equal(suite.T(), "GO GLIDE GO", result.SingleValue().(string))
}

func (suite *GlideTestSuite) TestClusterCustomCommandWithRoute_InvalidRoute() {
client := suite.defaultClusterClient()
invalidRoute := config.NewByAddressRoute("invalidHost", 9999)
result, err := client.CustomCommandWithRoute([]string{"PING"}, invalidRoute)
assert.NotNil(suite.T(), err)
assert.Equal(suite.T(), api.CreateEmptyClusterValue(), result)
assert.True(suite.T(), result.IsEmpty())
}

func (suite *GlideTestSuite) TestClusterCustomCommandWithRoute_AllNodes() {
client := suite.defaultClusterClient()
route := config.SimpleNodeRoute(config.AllNodes)
result, err := client.CustomCommandWithRoute([]string{"PING"}, route)
value := result.Value()
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), "PONG", value.(string))
assert.True(suite.T(), result.IsMultiValue())

multiValue := result.MultiValue()
for nodeName, value := range multiValue {
assert.Equal(suite.T(), "PONG", value.(string), "Node %s should return 'PONG'", nodeName)
}
}

func (suite *GlideTestSuite) TestPingWithOptions_NoRoute() {
Expand All @@ -150,7 +159,6 @@ func (suite *GlideTestSuite) TestPingWithOptions_NoRoute() {
},
Route: nil,
}

result, err := client.PingWithOptions(options)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), "hello", result)
Expand All @@ -165,7 +173,6 @@ func (suite *GlideTestSuite) TestPingWithOptions_WithRoute() {
},
Route: &route,
}

result, err := client.PingWithOptions(options)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), "hello", result)
Expand All @@ -180,7 +187,6 @@ func (suite *GlideTestSuite) TestPingWithOptions_InvalidRoute() {
},
Route: &invalidRoute,
}

result, err := client.PingWithOptions(options)
assert.NotNil(suite.T(), err)
assert.Empty(suite.T(), result)
Expand Down

0 comments on commit a3f6549

Please sign in to comment.