Skip to content

Commit

Permalink
feat: add client get/set name commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-karan authored and alicebob committed Sep 27, 2023
1 parent dc54bd8 commit ae80f40
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
59 changes: 59 additions & 0 deletions cmd_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package miniredis

import (
"fmt"
"strings"

"github.com/alicebob/miniredis/v2/server"
)

// commandsClient handles client operations.
func commandsClient(m *Miniredis) {
m.srv.Register("CLIENT", m.cmdClient)
}

// CLIENT
func (m *Miniredis) cmdClient(c *server.Peer, cmd string, args []string) {
if len(args) == 0 {
setDirty(c)
c.WriteError("ERR wrong number of arguments for 'client' command")
return
}

switch strings.ToUpper(args[0]) {
case "SETNAME":
m.cmdClientSetName(c, args[1:])
case "GETNAME":
m.cmdClientGetName(c, args[1:])
default:
setDirty(c)
c.WriteError(fmt.Sprintf("ERR 'CLIENT %s' not supported", strings.Join(args, " ")))
}
}

// CLIENT SETNAME
func (m *Miniredis) cmdClientSetName(c *server.Peer, args []string) {
if len(args) != 1 {
setDirty(c)
c.WriteError("ERR wrong number of arguments for 'client setname' command")
return
}

c.ClientName = args[0]
c.WriteOK()
}

// CLIENT GETNAME
func (m *Miniredis) cmdClientGetName(c *server.Peer, args []string) {
if len(args) > 0 {
setDirty(c)
c.WriteError("ERR wrong number of arguments for 'client getname' command")
return
}

if c.ClientName == "" {
c.WriteNull()
} else {
c.WriteBulk(c.ClientName)
}
}
42 changes: 42 additions & 0 deletions cmd_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package miniredis

import (
"testing"

"github.com/alicebob/miniredis/v2/proto"
)

// Test CLIENT *.
func TestClient(t *testing.T) {
t.Run("setname and getname", func(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()

// Set the client name
mustDo(t, c,
"CLIENT", "SETNAME", "miniredis-tests",
proto.Inline("OK"),
)

// Get the client name
mustDo(t, c,
"CLIENT", "GETNAME",
proto.String("miniredis-tests"),
)
})

t.Run("getname without setname", func(t *testing.T) {
s := RunT(t)
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()

// Get the client name without setting it first
mustDo(t, c,
"CLIENT", "GETNAME",
proto.Nil,
)
})
}
17 changes: 17 additions & 0 deletions integration/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,20 @@ func TestCopy(t *testing.T) {
})
})
}

func TestClient(t *testing.T) {
skip(t)
testRaw(t, func(c *client) {
// Set the client name
c.Do("CLIENT", "SETNAME", "miniredis-tests")

// Get the client name
c.Do("CLIENT", "GETNAME")

// Try to get the client name without setting it first
c.Do("CLIENT", "GETNAME")

// Try to execute the CLIENT command with no arguments
c.Error("wrong number", "CLIENT")
})
}
1 change: 1 addition & 0 deletions miniredis.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func (m *Miniredis) start(s *server.Server) error {
commandsGeo(m)
commandsCluster(m)
commandsHll(m)
commandsClient(m)

return nil
}
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ type Peer struct {
Ctx interface{} // anything goes, server won't touch this
onDisconnect []func() // list of callbacks
mu sync.Mutex // for Block()
ClientName string // client name set by CLIENT SETNAME
}

func NewPeer(w *bufio.Writer) *Peer {
Expand Down

0 comments on commit ae80f40

Please sign in to comment.