pipeline_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package redis_test
  2. import (
  3. "gopkg.in/redis.v5"
  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.Pipeline) 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 an error when there are no commands", func() {
  31. _, err := pipe.Exec()
  32. Expect(err).To(MatchError("redis: pipeline is empty"))
  33. })
  34. It("discards queued commands", func() {
  35. pipe.Get("key")
  36. pipe.Discard()
  37. _, err := pipe.Exec()
  38. Expect(err).To(MatchError("redis: pipeline is empty"))
  39. })
  40. It("handles val/err", func() {
  41. err := client.Set("key", "value", 0).Err()
  42. Expect(err).NotTo(HaveOccurred())
  43. get := pipe.Get("key")
  44. cmds, err := pipe.Exec()
  45. Expect(err).NotTo(HaveOccurred())
  46. Expect(cmds).To(HaveLen(1))
  47. val, err := get.Result()
  48. Expect(err).NotTo(HaveOccurred())
  49. Expect(val).To(Equal("value"))
  50. })
  51. }
  52. Describe("Pipeline", func() {
  53. BeforeEach(func() {
  54. pipe = client.Pipeline()
  55. })
  56. assertPipeline()
  57. })
  58. Describe("TxPipeline", func() {
  59. BeforeEach(func() {
  60. pipe = client.TxPipeline()
  61. })
  62. assertPipeline()
  63. })
  64. })