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

fix: ping return bulk string #933

Merged
merged 9 commits into from
Oct 4, 2022
10 changes: 8 additions & 2 deletions src/redis_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,13 @@ class CommandFlushAll : public Commander {
class CommandPing : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
*output = Redis::SimpleString("PONG");
if (args_.size() == 1) {
*output = Redis::SimpleString("PONG");
} else if (args_.size() == 2) {
*output = Redis::BulkString(args_[1]);
} else {
return Status(Status::NotOK, errWrongNumOfArguments);
}
return Status::OK();
}
};
Expand Down Expand Up @@ -5833,7 +5839,7 @@ class CommandXTrim : public Commander {

CommandAttributes redisCommandTable[] = {
ADD_CMD("auth", 2, "read-only ok-loading", 0, 0, 0, CommandAuth),
ADD_CMD("ping", 1, "read-only", 0, 0, 0, CommandPing),
ADD_CMD("ping", -1, "read-only", 0, 0, 0, CommandPing),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask the reason to make this change? Is there some reference from the Redis document?

The overall change looks good to me. Merging...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This digit indicates the number of parameters allowed(including command name), "1 " means there is precisely one, and "-1 "means at least one. Do you want this source code and the comment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

ADD_CMD("select", 2, "read-only", 0, 0, 0, CommandSelect),
ADD_CMD("info", -1, "read-only ok-loading", 0, 0, 0, CommandInfo),
ADD_CMD("role", 1, "read-only ok-loading", 0, 0, 0, CommandRole),
Expand Down
52 changes: 52 additions & 0 deletions tests/gocase/unit/introspection/introspection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
PragmaTwice marked this conversation as resolved.
Show resolved Hide resolved
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package command

import (
"testing"

"github.com/apache/incubator-kvrocks/tests/gocase/util"
"github.com/stretchr/testify/require"
)

func TestIntrospection(t *testing.T) {
srv := util.StartServer(t, map[string]string{})
defer srv.Close()

t.Run("PING", func(t *testing.T) {
c := srv.NewTCPClient()
defer func() { require.NoError(t, c.Close()) }()
require.NoError(t, c.WriteArgs("PING"))
r, err := c.ReadLine()
require.NoError(t, err)
require.Equal(t, r, "+PONG")
require.NoError(t, c.WriteArgs("PING", "PONG"))
r, err = c.ReadLine()
require.NoError(t, err)
require.Equal(t, r, "$4")
r, err = c.ReadLine()
require.NoError(t, err)
require.Equal(t, r, "PONG")
require.NoError(t, c.WriteArgs("ping", "hello", "redis"))
r, err = c.ReadLine()
require.NoError(t, err)
require.Equal(t, r, "-ERR wrong number of arguments")
})
}