11package redis_test
22
33import (
4- "bytes"
5- "strconv"
6- "sync"
7- "testing"
8- "time"
9-
104 . "github.com/onsi/ginkgo"
115 . "github.com/onsi/gomega"
126
137 "gopkg.in/redis.v3"
14- "gopkg.in/redis.v3/internal/pool"
158)
169
1710var _ = Describe ("Command" , func () {
1811 var client * redis.Client
1912
20- connect := func () * redis.Client {
21- return redis .NewClient (& redis.Options {
22- Addr : redisAddr ,
23- PoolTimeout : time .Minute ,
24- })
25- }
26-
2713 BeforeEach (func () {
28- client = connect ()
14+ client = redis .NewClient (redisOptions ())
15+ Expect (client .FlushDb ().Err ()).NotTo (HaveOccurred ())
2916 })
3017
3118 AfterEach (func () {
32- Expect (client .FlushDb ().Err ()).NotTo (HaveOccurred ())
3319 Expect (client .Close ()).NotTo (HaveOccurred ())
3420 })
3521
@@ -54,64 +40,6 @@ var _ = Describe("Command", func() {
5440 Expect (set .Val ()).To (Equal ("OK" ))
5541 })
5642
57- It ("should escape special chars" , func () {
58- set := client .Set ("key" , "hello1\r \n hello2\r \n " , 0 )
59- Expect (set .Err ()).NotTo (HaveOccurred ())
60- Expect (set .Val ()).To (Equal ("OK" ))
61-
62- get := client .Get ("key" )
63- Expect (get .Err ()).NotTo (HaveOccurred ())
64- Expect (get .Val ()).To (Equal ("hello1\r \n hello2\r \n " ))
65- })
66-
67- It ("should handle big vals" , func () {
68- bigVal := string (bytes .Repeat ([]byte {'*' }, 1 << 16 ))
69-
70- err := client .Set ("key" , bigVal , 0 ).Err ()
71- Expect (err ).NotTo (HaveOccurred ())
72-
73- // Reconnect to get new connection.
74- Expect (client .Close ()).To (BeNil ())
75- client = connect ()
76-
77- got , err := client .Get ("key" ).Result ()
78- Expect (err ).NotTo (HaveOccurred ())
79- Expect (len (got )).To (Equal (len (bigVal )))
80- Expect (got ).To (Equal (bigVal ))
81- })
82-
83- It ("should handle many keys #1" , func () {
84- const n = 100000
85- for i := 0 ; i < n ; i ++ {
86- client .Set ("keys.key" + strconv .Itoa (i ), "hello" + strconv .Itoa (i ), 0 )
87- }
88- keys := client .Keys ("keys.*" )
89- Expect (keys .Err ()).NotTo (HaveOccurred ())
90- Expect (len (keys .Val ())).To (Equal (n ))
91- })
92-
93- It ("should handle many keys #2" , func () {
94- const n = 100000
95-
96- keys := []string {"non-existent-key" }
97- for i := 0 ; i < n ; i ++ {
98- key := "keys.key" + strconv .Itoa (i )
99- client .Set (key , "hello" + strconv .Itoa (i ), 0 )
100- keys = append (keys , key )
101- }
102- keys = append (keys , "non-existent-key" )
103-
104- mget := client .MGet (keys ... )
105- Expect (mget .Err ()).NotTo (HaveOccurred ())
106- Expect (len (mget .Val ())).To (Equal (n + 2 ))
107- vals := mget .Val ()
108- for i := 0 ; i < n ; i ++ {
109- Expect (vals [i + 1 ]).To (Equal ("hello" + strconv .Itoa (i )))
110- }
111- Expect (vals [0 ]).To (BeNil ())
112- Expect (vals [n + 1 ]).To (BeNil ())
113- })
114-
11543 It ("should convert strings via helpers" , func () {
11644 set := client .Set ("key" , "10" , 0 )
11745 Expect (set .Err ()).NotTo (HaveOccurred ())
@@ -129,126 +57,4 @@ var _ = Describe("Command", func() {
12957 Expect (f ).To (Equal (float64 (10 )))
13058 })
13159
132- It ("Cmd should return string" , func () {
133- cmd := redis .NewCmd ("PING" )
134- client .Process (cmd )
135- Expect (cmd .Err ()).NotTo (HaveOccurred ())
136- Expect (cmd .Val ()).To (Equal ("PONG" ))
137- })
138-
139- Describe ("races" , func () {
140- var C , N = 10 , 1000
141- if testing .Short () {
142- C = 3
143- N = 100
144- }
145-
146- It ("should echo" , func () {
147- perform (C , func () {
148- for i := 0 ; i < N ; i ++ {
149- msg := "echo" + strconv .Itoa (i )
150- echo , err := client .Echo (msg ).Result ()
151- Expect (err ).NotTo (HaveOccurred ())
152- Expect (echo ).To (Equal (msg ))
153- }
154- })
155- })
156-
157- It ("should incr" , func () {
158- key := "TestIncrFromGoroutines"
159-
160- perform (C , func () {
161- for i := 0 ; i < N ; i ++ {
162- err := client .Incr (key ).Err ()
163- Expect (err ).NotTo (HaveOccurred ())
164- }
165- })
166-
167- val , err := client .Get (key ).Int64 ()
168- Expect (err ).NotTo (HaveOccurred ())
169- Expect (val ).To (Equal (int64 (C * N )))
170- })
171-
172- It ("should handle big vals" , func () {
173- client2 := connect ()
174- defer client2 .Close ()
175-
176- bigVal := string (bytes .Repeat ([]byte {'*' }, 1 << 16 ))
177-
178- var wg sync.WaitGroup
179- wg .Add (2 )
180-
181- go func () {
182- defer wg .Done ()
183- perform (C , func () {
184- for i := 0 ; i < N ; i ++ {
185- got , err := client .Get ("key" ).Result ()
186- if err == redis .Nil {
187- continue
188- }
189- Expect (got ).To (Equal (bigVal ))
190- }
191- })
192- }()
193-
194- go func () {
195- defer wg .Done ()
196- perform (C , func () {
197- for i := 0 ; i < N ; i ++ {
198- err := client2 .Set ("key" , bigVal , 0 ).Err ()
199- Expect (err ).NotTo (HaveOccurred ())
200- }
201- })
202- }()
203-
204- wg .Wait ()
205- })
206-
207- It ("should PubSub" , func () {
208- connPool := client .Pool ()
209- connPool .(* pool.ConnPool ).DialLimiter = nil
210-
211- var wg sync.WaitGroup
212- wg .Add (2 )
213-
214- go func () {
215- defer wg .Done ()
216- perform (C , func () {
217- for i := 0 ; i < N ; i ++ {
218- pubsub , err := client .Subscribe ("mychannel" )
219- Expect (err ).NotTo (HaveOccurred ())
220-
221- go func () {
222- defer GinkgoRecover ()
223-
224- time .Sleep (time .Millisecond )
225- err := pubsub .Close ()
226- Expect (err ).NotTo (HaveOccurred ())
227- }()
228-
229- _ , err = pubsub .ReceiveMessage ()
230- Expect (err .Error ()).To (ContainSubstring ("closed" ))
231- }
232- })
233- }()
234-
235- go func () {
236- defer wg .Done ()
237- perform (C , func () {
238- for i := 0 ; i < N ; i ++ {
239- val := "echo" + strconv .Itoa (i )
240- echo , err := client .Echo (val ).Result ()
241- Expect (err ).NotTo (HaveOccurred ())
242- Expect (echo ).To (Equal (val ))
243- }
244- })
245- }()
246-
247- wg .Wait ()
248-
249- Expect (connPool .Len ()).To (Equal (connPool .FreeLen ()))
250- Expect (connPool .Len ()).To (BeNumerically ("<=" , 10 ))
251- })
252- })
253-
25460})
0 commit comments