pipeline_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package redis_test
  2. import (
  3. "github.com/go-redis/redis"
  4. . "github.com/onsi/ginkgo"
  5. . "github.com/onsi/gomega"
  6. )
  7. var _ = Describe("pipelining", func() {
  8. var client *redis.Client
  9. var pipe *redis.Pipeline
  10. BeforeEach(func() {
  11. client = redis.NewClient(redisOptions())
  12. Expect(client.FlushDB().Err()).NotTo(HaveOccurred())
  13. })
  14. AfterEach(func() {
  15. Expect(client.Close()).NotTo(HaveOccurred())
  16. })
  17. It("supports block style", func() {
  18. var get *redis.StringCmd
  19. cmds, err := client.Pipelined(func(pipe redis.Pipeliner) error {
  20. get = pipe.Get("foo")
  21. return nil
  22. })
  23. Expect(err).To(Equal(redis.Nil))
  24. Expect(cmds).To(HaveLen(1))
  25. Expect(cmds[0]).To(Equal(get))
  26. Expect(get.Err()).To(Equal(redis.Nil))
  27. Expect(get.Val()).To(Equal(""))
  28. })
  29. assertPipeline := func() {
  30. It("returns no errors when there are no commands", func() {
  31. _, err := pipe.Exec()
  32. Expect(err).NotTo(HaveOccurred())
  33. })
  34. It("discards queued commands", func() {
  35. pipe.Get("key")
  36. pipe.Discard()
  37. cmds, err := pipe.Exec()
  38. Expect(err).NotTo(HaveOccurred())
  39. Expect(cmds).To(BeNil())
  40. })
  41. It("handles val/err", func() {
  42. err := client.Set("key", "value", 0).Err()
  43. Expect(err).NotTo(HaveOccurred())
  44. get := pipe.Get("key")
  45. cmds, err := pipe.Exec()
  46. Expect(err).NotTo(HaveOccurred())
  47. Expect(cmds).To(HaveLen(1))
  48. val, err := get.Result()
  49. Expect(err).NotTo(HaveOccurred())
  50. Expect(val).To(Equal("value"))
  51. })
  52. It("supports custom command", func() {
  53. pipe.Do("ping")
  54. cmds, err := pipe.Exec()
  55. Expect(err).NotTo(HaveOccurred())
  56. Expect(cmds).To(HaveLen(1))
  57. })
  58. }
  59. Describe("Pipeline", func() {
  60. BeforeEach(func() {
  61. pipe = client.Pipeline().(*redis.Pipeline)
  62. })
  63. assertPipeline()
  64. })
  65. Describe("TxPipeline", func() {
  66. BeforeEach(func() {
  67. pipe = client.TxPipeline().(*redis.Pipeline)
  68. })
  69. assertPipeline()
  70. })
  71. })