Debian Codegraph @

pipeline_test.go

raw
1 package redis_test
2
3 import (
4 "gopkg.in/redis.v5"
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.Pipeline) 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 an error when there are no commands", func() {
38 _, err := pipe.Exec()
39 Expect(err).To(MatchError("redis: pipeline is empty"))
40 })
41
42 It("discards queued commands", func() {
43 pipe.Get("key")
44 pipe.Discard()
45 _, err := pipe.Exec()
46 Expect(err).To(MatchError("redis: pipeline is empty"))
47 })
48
49 It("handles val/err", func() {
50 err := client.Set("key", "value", 0).Err()
51 Expect(err).NotTo(HaveOccurred())
52
53 get := pipe.Get("key")
54 cmds, err := pipe.Exec()
55 Expect(err).NotTo(HaveOccurred())
56 Expect(cmds).To(HaveLen(1))
57
58 val, err := get.Result()
59 Expect(err).NotTo(HaveOccurred())
60 Expect(val).To(Equal("value"))
61 })
62 }
63
64 Describe("Pipeline", func() {
65 BeforeEach(func() {
66 pipe = client.Pipeline()
67 })
68
69 assertPipeline()
70 })
71
72 Describe("TxPipeline", func() {
73 BeforeEach(func() {
74 pipe = client.TxPipeline()
75 })
76
77 assertPipeline()
78 })
79 })