mirror of
https://github.com/nalgeon/redka.git
synced 2025-10-25 17:10:26 +08:00
feat: command - ping (#19)
This commit is contained in:
@@ -49,6 +49,8 @@ func Parse(args [][]byte) (redis.Cmd, error) {
|
||||
// connection
|
||||
case "echo":
|
||||
return conn.ParseEcho(b)
|
||||
case "ping":
|
||||
return conn.ParsePing(b)
|
||||
|
||||
// key
|
||||
case "del":
|
||||
|
||||
41
internal/command/conn/ping.go
Normal file
41
internal/command/conn/ping.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package conn
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/nalgeon/redka/internal/parser"
|
||||
"github.com/nalgeon/redka/internal/redis"
|
||||
)
|
||||
|
||||
const (
|
||||
PONG = "PONG"
|
||||
)
|
||||
|
||||
// Returns PONG if no argument is provided, otherwise return a copy of the argument as a bulk
|
||||
// https://redis.io/commands/ping
|
||||
type Ping struct {
|
||||
redis.BaseCmd
|
||||
Parts []string
|
||||
}
|
||||
|
||||
|
||||
func ParsePing(b redis.BaseCmd) (*Ping, error) {
|
||||
cmd := &Ping{BaseCmd: b}
|
||||
err := parser.New(
|
||||
parser.Strings(&cmd.Parts),
|
||||
).Required(0).Run(cmd.Args())
|
||||
if err != nil {
|
||||
return cmd, err
|
||||
}
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
func (c *Ping) Run(w redis.Writer, _ redis.Redka) (any, error) {
|
||||
if len(c.Parts) == 0 {
|
||||
w.WriteAny(PONG)
|
||||
return PONG, nil
|
||||
}
|
||||
out := strings.Join(c.Parts, " ")
|
||||
w.WriteAny(out)
|
||||
return out, nil
|
||||
}
|
||||
89
internal/command/conn/ping_test.go
Normal file
89
internal/command/conn/ping_test.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package conn_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/nalgeon/redka/internal/command"
|
||||
"github.com/nalgeon/redka/internal/command/conn"
|
||||
"github.com/nalgeon/redka/internal/redis"
|
||||
"github.com/nalgeon/redka/internal/testx"
|
||||
)
|
||||
|
||||
func TestPingParse(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args [][]byte
|
||||
want []string
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "ping",
|
||||
args: command.BuildArgs("ping"),
|
||||
want: []string(nil),
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
name: "ping hello",
|
||||
args: command.BuildArgs("ping", "hello"),
|
||||
want: []string{"hello"},
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
name: "ping one two",
|
||||
args: command.BuildArgs("ping", "one", "two"),
|
||||
want: []string{"one", "two"},
|
||||
err: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
cmd, err := command.Parse(test.args)
|
||||
testx.AssertEqual(t, err, test.err)
|
||||
if err == nil {
|
||||
testx.AssertEqual(t, cmd.(*conn.Ping).Parts, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPingExec(t *testing.T) {
|
||||
db, red := getDB(t)
|
||||
defer db.Close()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cmd *conn.Ping
|
||||
res any
|
||||
out string
|
||||
}{
|
||||
{
|
||||
name: "ping",
|
||||
cmd: command.MustParse[*conn.Ping]("ping"),
|
||||
res: "PONG",
|
||||
out: "PONG",
|
||||
},
|
||||
{
|
||||
name: "ping hello",
|
||||
cmd: command.MustParse[*conn.Ping]("ping hello"),
|
||||
res: "hello",
|
||||
out: "hello",
|
||||
},
|
||||
{
|
||||
name: "ping one two",
|
||||
cmd: command.MustParse[*conn.Ping]("ping one two"),
|
||||
res: "one two",
|
||||
out: "one two",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
conn := redis.NewFakeConn()
|
||||
res, err := test.cmd.Run(conn, red)
|
||||
testx.AssertNoErr(t, err)
|
||||
testx.AssertEqual(t, res, test.res)
|
||||
testx.AssertEqual(t, conn.Out(), test.out)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user