@@ -102,6 +102,35 @@ var _ = Describe("PubSub", func() {
102102 Expect (len (channels )).To (BeNumerically (">=" , 2 ))
103103 })
104104
105+ It ("should sharded pub/sub channels" , func () {
106+ channels , err := client .PubSubShardChannels (ctx , "mychannel*" ).Result ()
107+ Expect (err ).NotTo (HaveOccurred ())
108+ Expect (channels ).To (BeEmpty ())
109+
110+ pubsub := client .SSubscribe (ctx , "mychannel" , "mychannel2" )
111+ defer pubsub .Close ()
112+
113+ channels , err = client .PubSubShardChannels (ctx , "mychannel*" ).Result ()
114+ Expect (err ).NotTo (HaveOccurred ())
115+ Expect (channels ).To (ConsistOf ([]string {"mychannel" , "mychannel2" }))
116+
117+ channels , err = client .PubSubShardChannels (ctx , "" ).Result ()
118+ Expect (err ).NotTo (HaveOccurred ())
119+ Expect (channels ).To (BeEmpty ())
120+
121+ channels , err = client .PubSubShardChannels (ctx , "*" ).Result ()
122+ Expect (err ).NotTo (HaveOccurred ())
123+ Expect (len (channels )).To (BeNumerically (">=" , 2 ))
124+
125+ nums , err := client .PubSubShardNumSub (ctx , "mychannel" , "mychannel2" , "mychannel3" ).Result ()
126+ Expect (err ).NotTo (HaveOccurred ())
127+ Expect (nums ).To (Equal (map [string ]int64 {
128+ "mychannel" : 1 ,
129+ "mychannel2" : 1 ,
130+ "mychannel3" : 0 ,
131+ }))
132+ })
133+
105134 It ("should return the numbers of subscribers" , func () {
106135 pubsub := client .Subscribe (ctx , "mychannel" , "mychannel2" )
107136 defer pubsub .Close ()
@@ -204,6 +233,82 @@ var _ = Describe("PubSub", func() {
204233 Expect (stats .Misses ).To (Equal (uint32 (1 )))
205234 })
206235
236+ It ("should sharded pub/sub" , func () {
237+ pubsub := client .SSubscribe (ctx , "mychannel" , "mychannel2" )
238+ defer pubsub .Close ()
239+
240+ {
241+ msgi , err := pubsub .ReceiveTimeout (ctx , time .Second )
242+ Expect (err ).NotTo (HaveOccurred ())
243+ subscr := msgi .(* redis.Subscription )
244+ Expect (subscr .Kind ).To (Equal ("ssubscribe" ))
245+ Expect (subscr .Channel ).To (Equal ("mychannel" ))
246+ Expect (subscr .Count ).To (Equal (1 ))
247+ }
248+
249+ {
250+ msgi , err := pubsub .ReceiveTimeout (ctx , time .Second )
251+ Expect (err ).NotTo (HaveOccurred ())
252+ subscr := msgi .(* redis.Subscription )
253+ Expect (subscr .Kind ).To (Equal ("ssubscribe" ))
254+ Expect (subscr .Channel ).To (Equal ("mychannel2" ))
255+ Expect (subscr .Count ).To (Equal (2 ))
256+ }
257+
258+ {
259+ msgi , err := pubsub .ReceiveTimeout (ctx , time .Second )
260+ Expect (err .(net.Error ).Timeout ()).To (Equal (true ))
261+ Expect (msgi ).NotTo (HaveOccurred ())
262+ }
263+
264+ n , err := client .SPublish (ctx , "mychannel" , "hello" ).Result ()
265+ Expect (err ).NotTo (HaveOccurred ())
266+ Expect (n ).To (Equal (int64 (1 )))
267+
268+ n , err = client .SPublish (ctx , "mychannel2" , "hello2" ).Result ()
269+ Expect (err ).NotTo (HaveOccurred ())
270+ Expect (n ).To (Equal (int64 (1 )))
271+
272+ Expect (pubsub .SUnsubscribe (ctx , "mychannel" , "mychannel2" )).NotTo (HaveOccurred ())
273+
274+ {
275+ msgi , err := pubsub .ReceiveTimeout (ctx , time .Second )
276+ Expect (err ).NotTo (HaveOccurred ())
277+ msg := msgi .(* redis.Message )
278+ Expect (msg .Channel ).To (Equal ("mychannel" ))
279+ Expect (msg .Payload ).To (Equal ("hello" ))
280+ }
281+
282+ {
283+ msgi , err := pubsub .ReceiveTimeout (ctx , time .Second )
284+ Expect (err ).NotTo (HaveOccurred ())
285+ msg := msgi .(* redis.Message )
286+ Expect (msg .Channel ).To (Equal ("mychannel2" ))
287+ Expect (msg .Payload ).To (Equal ("hello2" ))
288+ }
289+
290+ {
291+ msgi , err := pubsub .ReceiveTimeout (ctx , time .Second )
292+ Expect (err ).NotTo (HaveOccurred ())
293+ subscr := msgi .(* redis.Subscription )
294+ Expect (subscr .Kind ).To (Equal ("sunsubscribe" ))
295+ Expect (subscr .Channel ).To (Equal ("mychannel" ))
296+ Expect (subscr .Count ).To (Equal (1 ))
297+ }
298+
299+ {
300+ msgi , err := pubsub .ReceiveTimeout (ctx , time .Second )
301+ Expect (err ).NotTo (HaveOccurred ())
302+ subscr := msgi .(* redis.Subscription )
303+ Expect (subscr .Kind ).To (Equal ("sunsubscribe" ))
304+ Expect (subscr .Channel ).To (Equal ("mychannel2" ))
305+ Expect (subscr .Count ).To (Equal (0 ))
306+ }
307+
308+ stats := client .PoolStats ()
309+ Expect (stats .Misses ).To (Equal (uint32 (1 )))
310+ })
311+
207312 It ("should ping/pong" , func () {
208313 pubsub := client .Subscribe (ctx , "mychannel" )
209314 defer pubsub .Close ()
0 commit comments