diff --git a/cmd_generic.go b/cmd_generic.go index 2f55d5f..d030888 100644 --- a/cmd_generic.go +++ b/cmd_generic.go @@ -649,7 +649,8 @@ func (m *Miniredis) cmdScan(c *server.Peer, cmd string, args []string) { withTx(m, c, func(c *server.Peer, ctx *connCtx) { db := m.db(ctx.selectedDB) - // We return _all_ (matched) keys every time. + // We return _all_ (matched) keys every time, so that cursors work. + // We ignore "COUNT", which is allowed according to the Redis docs. var keys []string if opts.withType { @@ -670,25 +671,14 @@ func (m *Miniredis) cmdScan(c *server.Peer, cmd string, args []string) { keys, _ = matchKeys(keys, opts.match) } - low := opts.cursor - high := low + opts.count - // validate high is correct - if high > len(keys) || high == 0 { - high = len(keys) - } - if opts.cursor > high { - // invalid cursor + // we only ever return all at once, so no non-zero cursor can every be valid + if opts.cursor != 0 { c.WriteLen(2) c.WriteBulk("0") // no next cursor c.WriteLen(0) // no elements return } - cursorValue := low + opts.count - if cursorValue >= len(keys) { - cursorValue = 0 // no next cursor - } - keys = keys[low:high] - + cursorValue := 0 // we don't use cursors c.WriteLen(2) c.WriteBulk(fmt.Sprintf("%d", cursorValue)) c.WriteLen(len(keys)) diff --git a/cmd_generic_test.go b/cmd_generic_test.go index 3994bac..f2faf62 100644 --- a/cmd_generic_test.go +++ b/cmd_generic_test.go @@ -758,14 +758,22 @@ func TestScan(t *testing.T) { s.Set("v8", "value") s.Set("v9", "value") + // count is ignored mustDo(t, c, "SCAN", "0", "COUNT", "3", proto.Array( - proto.String("3"), + proto.String("0"), proto.Array( proto.String("key"), proto.String("v1"), proto.String("v2"), + proto.String("v3"), + proto.String("v4"), + proto.String("v5"), + proto.String("v6"), + proto.String("v7"), + proto.String("v8"), + proto.String("v9"), ), ), ) @@ -773,12 +781,8 @@ func TestScan(t *testing.T) { mustDo(t, c, "SCAN", "3", "COUNT", "3", proto.Array( - proto.String("6"), - proto.Array( - proto.String("v3"), - proto.String("v4"), - proto.String("v5"), - ), + proto.String("0"), + proto.Array(), ), ) })