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