forked from redis/go-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline_test.go
152 lines (119 loc) · 3.43 KB
/
pipeline_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package redis_test
import (
"strconv"
"sync"
"gopkg.in/redis.v2"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Pipelining", func() {
var client *redis.Client
BeforeEach(func() {
client = redis.NewTCPClient(&redis.Options{
Addr: redisAddr,
})
})
AfterEach(func() {
Expect(client.FlushDb().Err()).NotTo(HaveOccurred())
Expect(client.Close()).NotTo(HaveOccurred())
})
It("should pipeline", func() {
set := client.Set("key2", "hello2")
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
pipeline := client.Pipeline()
set = pipeline.Set("key1", "hello1")
get := pipeline.Get("key2")
incr := pipeline.Incr("key3")
getNil := pipeline.Get("key4")
cmds, err := pipeline.Exec()
Expect(err).To(Equal(redis.Nil))
Expect(cmds).To(HaveLen(4))
Expect(pipeline.Close()).NotTo(HaveOccurred())
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("hello2"))
Expect(incr.Err()).NotTo(HaveOccurred())
Expect(incr.Val()).To(Equal(int64(1)))
Expect(getNil.Err()).To(Equal(redis.Nil))
Expect(getNil.Val()).To(Equal(""))
})
It("should discard", func() {
pipeline := client.Pipeline()
pipeline.Get("key")
pipeline.Discard()
cmds, err := pipeline.Exec()
Expect(err).NotTo(HaveOccurred())
Expect(cmds).To(HaveLen(0))
Expect(pipeline.Close()).NotTo(HaveOccurred())
})
It("should support block style", func() {
var get *redis.StringCmd
cmds, err := client.Pipelined(func(pipe *redis.Pipeline) error {
get = pipe.Get("foo")
return nil
})
Expect(err).To(Equal(redis.Nil))
Expect(cmds).To(HaveLen(1))
Expect(cmds[0]).To(Equal(get))
Expect(get.Err()).To(Equal(redis.Nil))
Expect(get.Val()).To(Equal(""))
})
It("should handle vals/err", func() {
pipeline := client.Pipeline()
get := pipeline.Get("key")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal(""))
Expect(pipeline.Close()).NotTo(HaveOccurred())
})
It("should pipeline with empty queue", func() {
pipeline := client.Pipeline()
cmds, err := pipeline.Exec()
Expect(err).NotTo(HaveOccurred())
Expect(cmds).To(HaveLen(0))
Expect(pipeline.Close()).NotTo(HaveOccurred())
})
It("should increment correctly", func() {
const N = 20000
key := "TestPipelineIncr"
pipeline := client.Pipeline()
for i := 0; i < N; i++ {
pipeline.Incr(key)
}
cmds, err := pipeline.Exec()
Expect(err).NotTo(HaveOccurred())
Expect(pipeline.Close()).NotTo(HaveOccurred())
Expect(len(cmds)).To(Equal(20000))
for _, cmd := range cmds {
Expect(cmd.Err()).NotTo(HaveOccurred())
}
get := client.Get(key)
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal(strconv.Itoa(N)))
})
It("should PipelineEcho", func() {
const N = 1000
wg := &sync.WaitGroup{}
wg.Add(N)
for i := 0; i < N; i++ {
go func(i int) {
pipeline := client.Pipeline()
msg1 := "echo" + strconv.Itoa(i)
msg2 := "echo" + strconv.Itoa(i+1)
echo1 := pipeline.Echo(msg1)
echo2 := pipeline.Echo(msg2)
cmds, err := pipeline.Exec()
Expect(err).NotTo(HaveOccurred())
Expect(cmds).To(HaveLen(2))
Expect(echo1.Err()).NotTo(HaveOccurred())
Expect(echo1.Val()).To(Equal(msg1))
Expect(echo2.Err()).NotTo(HaveOccurred())
Expect(echo2.Val()).To(Equal(msg2))
Expect(pipeline.Close()).NotTo(HaveOccurred())
wg.Done()
}(i)
}
wg.Wait()
})
})