redis/iterator_test.go

153 lines
3.8 KiB
Go
Raw Normal View History

2016-04-13 11:52:47 +03:00
package redis_test
import (
"fmt"
2023-01-27 18:00:49 +03:00
. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"
2021-09-08 16:00:52 +03:00
2023-01-23 09:48:54 +03:00
"github.com/redis/go-redis/v9"
2016-04-13 11:52:47 +03:00
)
var _ = Describe("ScanIterator", func() {
var client *redis.Client
2020-07-16 09:52:07 +03:00
seed := func(n int) error {
2016-04-13 11:52:47 +03:00
pipe := client.Pipeline()
for i := 1; i <= n; i++ {
2020-03-11 17:26:42 +03:00
pipe.Set(ctx, fmt.Sprintf("K%02d", i), "x", 0).Err()
2016-04-13 11:52:47 +03:00
}
2020-03-11 17:26:42 +03:00
_, err := pipe.Exec(ctx)
2016-04-13 11:52:47 +03:00
return err
}
2020-07-16 09:52:07 +03:00
extraSeed := func(n int, m int) error {
2016-09-08 16:07:49 +03:00
pipe := client.Pipeline()
for i := 1; i <= m; i++ {
2020-03-11 17:26:42 +03:00
pipe.Set(ctx, fmt.Sprintf("A%02d", i), "x", 0).Err()
2016-09-08 16:07:49 +03:00
}
for i := 1; i <= n; i++ {
2020-03-11 17:26:42 +03:00
pipe.Set(ctx, fmt.Sprintf("K%02d", i), "x", 0).Err()
2016-09-08 16:07:49 +03:00
}
2020-03-11 17:26:42 +03:00
_, err := pipe.Exec(ctx)
2016-09-08 16:07:49 +03:00
return err
}
2020-07-16 09:52:07 +03:00
hashKey := "K_HASHTEST"
hashSeed := func(n int) error {
2016-08-04 07:52:00 +03:00
pipe := client.Pipeline()
for i := 1; i <= n; i++ {
2020-03-11 17:26:42 +03:00
pipe.HSet(ctx, hashKey, fmt.Sprintf("K%02d", i), "x").Err()
2016-08-04 07:52:00 +03:00
}
2020-03-11 17:26:42 +03:00
_, err := pipe.Exec(ctx)
2016-08-04 07:52:00 +03:00
return err
}
2016-04-13 11:52:47 +03:00
BeforeEach(func() {
client = redis.NewClient(redisOptions())
2020-03-11 17:26:42 +03:00
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
2016-04-13 11:52:47 +03:00
})
AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
})
It("should scan across empty DBs", func() {
2020-03-11 17:26:42 +03:00
iter := client.Scan(ctx, 0, "", 10).Iterator()
Expect(iter.Next(ctx)).To(BeFalse())
2016-04-13 11:52:47 +03:00
Expect(iter.Err()).NotTo(HaveOccurred())
})
It("should scan across one page", func() {
Expect(seed(7)).NotTo(HaveOccurred())
var vals []string
2020-03-11 17:26:42 +03:00
iter := client.Scan(ctx, 0, "", 0).Iterator()
for iter.Next(ctx) {
2016-04-13 11:52:47 +03:00
vals = append(vals, iter.Val())
}
Expect(iter.Err()).NotTo(HaveOccurred())
Expect(vals).To(ConsistOf([]string{"K01", "K02", "K03", "K04", "K05", "K06", "K07"}))
})
It("should scan across multiple pages", func() {
Expect(seed(71)).NotTo(HaveOccurred())
var vals []string
2020-03-11 17:26:42 +03:00
iter := client.Scan(ctx, 0, "", 10).Iterator()
for iter.Next(ctx) {
2016-04-13 11:52:47 +03:00
vals = append(vals, iter.Val())
}
Expect(iter.Err()).NotTo(HaveOccurred())
Expect(vals).To(HaveLen(71))
Expect(vals).To(ContainElement("K01"))
Expect(vals).To(ContainElement("K71"))
})
2016-08-04 07:52:00 +03:00
It("should hscan across multiple pages", func() {
Expect(hashSeed(71)).NotTo(HaveOccurred())
var vals []string
2020-03-11 17:26:42 +03:00
iter := client.HScan(ctx, hashKey, 0, "", 10).Iterator()
for iter.Next(ctx) {
2016-08-04 07:52:00 +03:00
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"))
Expect(vals).To(ContainElement("x"))
})
2024-06-20 02:25:49 +03:00
It("should hscan without values across multiple pages", Label("NonRedisEnterprise"), func() {
Expect(hashSeed(71)).NotTo(HaveOccurred())
var vals []string
iter := client.HScanNoValues(ctx, hashKey, 0, "", 10).Iterator()
for iter.Next(ctx) {
vals = append(vals, iter.Val())
}
Expect(iter.Err()).NotTo(HaveOccurred())
Expect(vals).To(HaveLen(71))
Expect(vals).To(ContainElement("K01"))
Expect(vals).To(ContainElement("K71"))
Expect(vals).NotTo(ContainElement("x"))
2016-08-04 07:52:00 +03:00
})
2016-04-13 11:52:47 +03:00
It("should scan to page borders", func() {
Expect(seed(20)).NotTo(HaveOccurred())
var vals []string
2020-03-11 17:26:42 +03:00
iter := client.Scan(ctx, 0, "", 10).Iterator()
for iter.Next(ctx) {
2016-04-13 11:52:47 +03:00
vals = append(vals, iter.Val())
}
Expect(iter.Err()).NotTo(HaveOccurred())
Expect(vals).To(HaveLen(20))
})
It("should scan with match", func() {
Expect(seed(33)).NotTo(HaveOccurred())
var vals []string
2020-03-11 17:26:42 +03:00
iter := client.Scan(ctx, 0, "K*2*", 10).Iterator()
for iter.Next(ctx) {
2016-04-13 11:52:47 +03:00
vals = append(vals, iter.Val())
}
Expect(iter.Err()).NotTo(HaveOccurred())
Expect(vals).To(HaveLen(13))
})
2016-09-08 16:07:49 +03:00
It("should scan with match across empty pages", func() {
Expect(extraSeed(2, 10)).NotTo(HaveOccurred())
var vals []string
2020-03-11 17:26:42 +03:00
iter := client.Scan(ctx, 0, "K*", 1).Iterator()
for iter.Next(ctx) {
2016-09-08 16:07:49 +03:00
vals = append(vals, iter.Val())
}
Expect(iter.Err()).NotTo(HaveOccurred())
Expect(vals).To(HaveLen(2))
})
2016-04-13 11:52:47 +03:00
})