From 91993128b0eade4284de442b3c4276e2838566d2 Mon Sep 17 00:00:00 2001 From: Yin Jifeng Date: Thu, 4 Aug 2016 12:52:00 +0800 Subject: [PATCH] fix iterator for hscan/sscan/zscan --- iterator.go | 6 +++++- iterator_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/iterator.go b/iterator.go index e9dfbad9..0a3df063 100644 --- a/iterator.go +++ b/iterator.go @@ -52,7 +52,11 @@ func (it *ScanIterator) Next() bool { } // Fetch next page. - it.ScanCmd._args[1] = it.ScanCmd.cursor + if it.ScanCmd._args[0] == "scan" { + it.ScanCmd._args[1] = it.ScanCmd.cursor + } else { + it.ScanCmd._args[2] = it.ScanCmd.cursor + } it.ScanCmd.reset() it.client.process(it.ScanCmd) if it.ScanCmd.Err() != nil { diff --git a/iterator_test.go b/iterator_test.go index 327feeb5..f752f68c 100644 --- a/iterator_test.go +++ b/iterator_test.go @@ -21,6 +21,16 @@ var _ = Describe("ScanIterator", func() { return err } + var hashKey = "K_HASHTEST" + var hashSeed = func(n int) error { + pipe := client.Pipeline() + for i := 1; i <= n; i++ { + pipe.HSet(hashKey, fmt.Sprintf("K%02d", i), "x").Err() + } + _, err := pipe.Exec() + return err + } + BeforeEach(func() { client = redis.NewClient(redisOptions()) Expect(client.FlushDb().Err()).NotTo(HaveOccurred()) @@ -62,6 +72,20 @@ var _ = Describe("ScanIterator", func() { Expect(vals).To(ContainElement("K71")) }) + It("should hscan across multiple pages", func() { + Expect(hashSeed(71)).NotTo(HaveOccurred()) + + var vals []string + iter := client.HScan(hashKey, 0, "", 10).Iterator() + for iter.Next() { + vals = append(vals, iter.Val()) + } + Expect(iter.Err()).NotTo(HaveOccurred()) + Expect(vals).To(HaveLen(71 * 2)) + Expect(vals).To(ContainElement("K01")) + Expect(vals).To(ContainElement("K71")) + }) + It("should scan to page borders", func() { Expect(seed(20)).NotTo(HaveOccurred())