Skip to content

Commit

Permalink
Make COMMAND command consistent with redis when the renamed command e…
Browse files Browse the repository at this point in the history
…xists (#2123)
  • Loading branch information
caipengbo authored Feb 28, 2024
1 parent 0d8f738 commit 350816c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/commands/commander.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ std::string CommandTable::GetCommandInfo(const CommandAttributes *command_attrib
}

void CommandTable::GetAllCommandsInfo(std::string *info) {
info->append(redis::MultiLen(original_commands.size()));
for (const auto &iter : original_commands) {
info->append(redis::MultiLen(commands.size()));
for (const auto &iter : commands) {
auto command_attribute = iter.second;
auto command_info = GetCommandInfo(command_attribute);
info->append(command_info);
Expand All @@ -66,8 +66,8 @@ void CommandTable::GetAllCommandsInfo(std::string *info) {
void CommandTable::GetCommandsInfo(std::string *info, const std::vector<std::string> &cmd_names) {
info->append(redis::MultiLen(cmd_names.size()));
for (const auto &cmd_name : cmd_names) {
auto cmd_iter = original_commands.find(util::ToLower(cmd_name));
if (cmd_iter == original_commands.end()) {
auto cmd_iter = commands.find(util::ToLower(cmd_name));
if (cmd_iter == commands.end()) {
info->append(NilString(RESP::v2));
} else {
auto command_attribute = cmd_iter->second;
Expand Down
40 changes: 39 additions & 1 deletion tests/gocase/unit/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import (
)

func TestCommand(t *testing.T) {
srv := util.StartServer(t, map[string]string{})
srv := util.StartServer(t, map[string]string{
"rename-command KEYS": "RENAMED_KEYS",
"rename-command CONFIG": "''", // remove CONFIG command
})
defer srv.Close()

ctx := context.Background()
Expand All @@ -50,6 +53,41 @@ func TestCommand(t *testing.T) {
require.EqualValues(t, 1, v[5])
})

t.Run("acquire renamed command info by COMMAND INFO", func(t *testing.T) {
r := rdb.Do(ctx, "COMMAND", "INFO", "KEYS")
vs, err := r.Slice()
require.NoError(t, err)
require.Len(t, vs, 1)
require.Nil(t, vs[0])

r = rdb.Do(ctx, "COMMAND", "INFO", "RENAMED_KEYS")
vs, err = r.Slice()
require.NoError(t, err)
require.Len(t, vs, 1)
v := vs[0].([]interface{})
require.Len(t, v, 6)
require.Equal(t, "keys", v[0])
require.EqualValues(t, 2, v[1])
require.Equal(t, []interface{}{"readonly"}, v[2])
require.EqualValues(t, 0, v[3])
require.EqualValues(t, 0, v[4])
require.EqualValues(t, 0, v[5])
})

t.Run("renamed command can be acquired by COMMAND", func(t *testing.T) {
commandInfos := rdb.Command(ctx).Val()
require.NotNil(t, commandInfos)
_, found := commandInfos["keys"]
require.True(t, found)
})

t.Run("removed command can not be acquired by COMMAND", func(t *testing.T) {
commandInfos := rdb.Command(ctx).Val()
require.NotNil(t, commandInfos)
_, found := commandInfos["config"]
require.True(t, found)
})

t.Run("command entry length check", func(t *testing.T) {
r := rdb.Do(ctx, "COMMAND")
vs, err := r.Slice()
Expand Down

0 comments on commit 350816c

Please sign in to comment.