Skip to content

Commit 80eb7b3

Browse files
authoredMay 27, 2024··
Merge pull request #376 from WKBae/srandmember_count_nil
Return empty array for SRANDMEMBER on nonexistent key
2 parents 49d1b75 + c007f55 commit 80eb7b3

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed
 

‎cmd_set.go

+4
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,10 @@ func (m *Miniredis) cmdSrandmember(c *server.Peer, cmd string, args []string) {
582582
db := m.db(ctx.selectedDB)
583583

584584
if !db.exists(key) {
585+
if withCount {
586+
c.WriteLen(0)
587+
return
588+
}
585589
c.WriteNull()
586590
return
587591
}

‎cmd_set_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,12 @@ func TestSrandmember(t *testing.T) {
381381
"SRANDMEMBER", "nosuch",
382382
)
383383

384+
// a nonexisting key with count
385+
mustDo(t, c,
386+
"SRANDMEMBER", "nosuch", "1",
387+
proto.Strings(),
388+
)
389+
384390
t.Run("errors", func(t *testing.T) {
385391
s.SetAdd("chk", "aap", "noot")
386392
s.Set("str", "value")

‎integration/set_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ func TestSetSrandmember(t *testing.T) {
191191
c.Do("SRANDMEMBER", "s", "-5")
192192

193193
c.Do("SRANDMEMBER", "s", "0")
194-
c.Do("SPOP", "nosuch")
194+
c.Do("SRANDMEMBER", "nosuch")
195+
c.Do("SRANDMEMBER", "nosuch", "1")
195196

196197
// failure cases
197198
c.Error("wrong number", "SRANDMEMBER")

0 commit comments

Comments
 (0)
Please sign in to comment.