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

support ping command #19

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
41 changes: 41 additions & 0 deletions internal/command/conn/ping.go
Original file line number Diff line number Diff line change
@@ -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 changes: 89 additions & 0 deletions internal/command/conn/ping_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}