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.
WKBae committed May 27, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 49d1b75 commit 1a30325
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
@@ -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
}
6 changes: 6 additions & 0 deletions cmd_set_test.go
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 1a30325

Please sign in to comment.