Skip to content

Commit

Permalink
fix iterator for hscan/sscan/zscan
Browse files Browse the repository at this point in the history
  • Loading branch information
flisky committed Aug 4, 2016
1 parent bc66ed0 commit 9199312
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())

Expand Down

0 comments on commit 9199312

Please sign in to comment.