Skip to content

Commit

Permalink
Return empty array for SRANDMEMBER on nonexistent key
Browse files Browse the repository at this point in the history
If the count argument is given, Redis returns an empty array if the key
does not exist, unlike single call which returns nil. Mimic this
behavior more precisely.
  • Loading branch information
WKBae committed May 27, 2024
1 parent 49d1b75 commit 1a30325
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,10 @@ func (m *Miniredis) cmdSrandmember(c *server.Peer, cmd string, args []string) {
db := m.db(ctx.selectedDB)

if !db.exists(key) {
if withCount {
c.WriteLen(0)
return
}
c.WriteNull()
return
}
Expand Down
6 changes: 6 additions & 0 deletions cmd_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ func TestSrandmember(t *testing.T) {
"SRANDMEMBER", "nosuch",
)

// a nonexisting key with count
mustDo(t, c,
"SRANDMEMBER", "nosuch", "1",
proto.Strings(),
)

t.Run("errors", func(t *testing.T) {
s.SetAdd("chk", "aap", "noot")
s.Set("str", "value")
Expand Down

0 comments on commit 1a30325

Please sign in to comment.