iterator_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package redis_test
  2. import (
  3. "fmt"
  4. . "github.com/onsi/ginkgo"
  5. . "github.com/onsi/gomega"
  6. "gopkg.in/redis.v5"
  7. )
  8. var _ = Describe("ScanIterator", func() {
  9. var client *redis.Client
  10. var seed = func(n int) error {
  11. pipe := client.Pipeline()
  12. for i := 1; i <= n; i++ {
  13. pipe.Set(fmt.Sprintf("K%02d", i), "x", 0).Err()
  14. }
  15. _, err := pipe.Exec()
  16. return err
  17. }
  18. var extraSeed = func(n int, m int) error {
  19. pipe := client.Pipeline()
  20. for i := 1; i <= m; i++ {
  21. pipe.Set(fmt.Sprintf("A%02d", i), "x", 0).Err()
  22. }
  23. for i := 1; i <= n; i++ {
  24. pipe.Set(fmt.Sprintf("K%02d", i), "x", 0).Err()
  25. }
  26. _, err := pipe.Exec()
  27. return err
  28. }
  29. var hashKey = "K_HASHTEST"
  30. var hashSeed = func(n int) error {
  31. pipe := client.Pipeline()
  32. for i := 1; i <= n; i++ {
  33. pipe.HSet(hashKey, fmt.Sprintf("K%02d", i), "x").Err()
  34. }
  35. _, err := pipe.Exec()
  36. return err
  37. }
  38. BeforeEach(func() {
  39. client = redis.NewClient(redisOptions())
  40. Expect(client.FlushDb().Err()).NotTo(HaveOccurred())
  41. })
  42. AfterEach(func() {
  43. Expect(client.Close()).NotTo(HaveOccurred())
  44. })
  45. It("should scan across empty DBs", func() {
  46. iter := client.Scan(0, "", 10).Iterator()
  47. Expect(iter.Next()).To(BeFalse())
  48. Expect(iter.Err()).NotTo(HaveOccurred())
  49. })
  50. It("should scan across one page", func() {
  51. Expect(seed(7)).NotTo(HaveOccurred())
  52. var vals []string
  53. iter := client.Scan(0, "", 0).Iterator()
  54. for iter.Next() {
  55. vals = append(vals, iter.Val())
  56. }
  57. Expect(iter.Err()).NotTo(HaveOccurred())
  58. Expect(vals).To(ConsistOf([]string{"K01", "K02", "K03", "K04", "K05", "K06", "K07"}))
  59. })
  60. It("should scan across multiple pages", func() {
  61. Expect(seed(71)).NotTo(HaveOccurred())
  62. var vals []string
  63. iter := client.Scan(0, "", 10).Iterator()
  64. for iter.Next() {
  65. vals = append(vals, iter.Val())
  66. }
  67. Expect(iter.Err()).NotTo(HaveOccurred())
  68. Expect(vals).To(HaveLen(71))
  69. Expect(vals).To(ContainElement("K01"))
  70. Expect(vals).To(ContainElement("K71"))
  71. })
  72. It("should hscan across multiple pages", func() {
  73. Expect(hashSeed(71)).NotTo(HaveOccurred())
  74. var vals []string
  75. iter := client.HScan(hashKey, 0, "", 10).Iterator()
  76. for iter.Next() {
  77. vals = append(vals, iter.Val())
  78. }
  79. Expect(iter.Err()).NotTo(HaveOccurred())
  80. Expect(vals).To(HaveLen(71 * 2))
  81. Expect(vals).To(ContainElement("K01"))
  82. Expect(vals).To(ContainElement("K71"))
  83. })
  84. It("should scan to page borders", func() {
  85. Expect(seed(20)).NotTo(HaveOccurred())
  86. var vals []string
  87. iter := client.Scan(0, "", 10).Iterator()
  88. for iter.Next() {
  89. vals = append(vals, iter.Val())
  90. }
  91. Expect(iter.Err()).NotTo(HaveOccurred())
  92. Expect(vals).To(HaveLen(20))
  93. })
  94. It("should scan with match", func() {
  95. Expect(seed(33)).NotTo(HaveOccurred())
  96. var vals []string
  97. iter := client.Scan(0, "K*2*", 10).Iterator()
  98. for iter.Next() {
  99. vals = append(vals, iter.Val())
  100. }
  101. Expect(iter.Err()).NotTo(HaveOccurred())
  102. Expect(vals).To(HaveLen(13))
  103. })
  104. It("should scan with match across empty pages", func() {
  105. Expect(extraSeed(2, 10)).NotTo(HaveOccurred())
  106. var vals []string
  107. iter := client.Scan(0, "K*", 1).Iterator()
  108. for iter.Next() {
  109. vals = append(vals, iter.Val())
  110. }
  111. Expect(iter.Err()).NotTo(HaveOccurred())
  112. Expect(vals).To(HaveLen(2))
  113. })
  114. })