| 1 |
package redis_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"net" |
| 6 |
"sync" |
| 7 |
"time" |
| 8 |
|
| 9 |
"gopkg.in/redis.v5" |
| 10 |
|
| 11 |
. "github.com/onsi/ginkgo" |
| 12 |
. "github.com/onsi/gomega" |
| 13 |
) |
| 14 |
|
| 15 |
var _ = Describe("PubSub", func() { |
| 16 |
var client *redis.Client |
| 17 |
|
| 18 |
BeforeEach(func() { |
| 19 |
client = redis.NewClient(redisOptions()) |
| 20 |
Expect(client.FlushDb().Err()).NotTo(HaveOccurred()) |
| 21 |
}) |
| 22 |
|
| 23 |
AfterEach(func() { |
| 24 |
Expect(client.Close()).NotTo(HaveOccurred()) |
| 25 |
}) |
| 26 |
|
| 27 |
It("should support pattern matching", func() { |
| 28 |
pubsub, err := client.PSubscribe("mychannel*") |
| 29 |
Expect(err).NotTo(HaveOccurred()) |
| 30 |
defer pubsub.Close() |
| 31 |
|
| 32 |
{ |
| 33 |
msgi, err := pubsub.ReceiveTimeout(time.Second) |
| 34 |
Expect(err).NotTo(HaveOccurred()) |
| 35 |
subscr := msgi.(*redis.Subscription) |
| 36 |
Expect(subscr.Kind).To(Equal("psubscribe")) |
| 37 |
Expect(subscr.Channel).To(Equal("mychannel*")) |
| 38 |
Expect(subscr.Count).To(Equal(1)) |
| 39 |
} |
| 40 |
|
| 41 |
{ |
| 42 |
msgi, err := pubsub.ReceiveTimeout(time.Second) |
| 43 |
Expect(err.(net.Error).Timeout()).To(Equal(true)) |
| 44 |
Expect(msgi).To(BeNil()) |
| 45 |
} |
| 46 |
|
| 47 |
n, err := client.Publish("mychannel1", "hello").Result() |
| 48 |
Expect(err).NotTo(HaveOccurred()) |
| 49 |
Expect(n).To(Equal(int64(1))) |
| 50 |
|
| 51 |
Expect(pubsub.PUnsubscribe("mychannel*")).NotTo(HaveOccurred()) |
| 52 |
|
| 53 |
{ |
| 54 |
msgi, err := pubsub.ReceiveTimeout(time.Second) |
| 55 |
Expect(err).NotTo(HaveOccurred()) |
| 56 |
subscr := msgi.(*redis.Message) |
| 57 |
Expect(subscr.Channel).To(Equal("mychannel1")) |
| 58 |
Expect(subscr.Pattern).To(Equal("mychannel*")) |
| 59 |
Expect(subscr.Payload).To(Equal("hello")) |
| 60 |
} |
| 61 |
|
| 62 |
{ |
| 63 |
msgi, err := pubsub.ReceiveTimeout(time.Second) |
| 64 |
Expect(err).NotTo(HaveOccurred()) |
| 65 |
subscr := msgi.(*redis.Subscription) |
| 66 |
Expect(subscr.Kind).To(Equal("punsubscribe")) |
| 67 |
Expect(subscr.Channel).To(Equal("mychannel*")) |
| 68 |
Expect(subscr.Count).To(Equal(0)) |
| 69 |
} |
| 70 |
|
| 71 |
stats := client.PoolStats() |
| 72 |
Expect(stats.Requests - stats.Hits).To(Equal(uint32(2))) |
| 73 |
}) |
| 74 |
|
| 75 |
It("should pub/sub channels", func() { |
| 76 |
channels, err := client.PubSubChannels("mychannel*").Result() |
| 77 |
Expect(err).NotTo(HaveOccurred()) |
| 78 |
Expect(channels).To(BeEmpty()) |
| 79 |
|
| 80 |
pubsub, err := client.Subscribe("mychannel", "mychannel2") |
| 81 |
Expect(err).NotTo(HaveOccurred()) |
| 82 |
defer pubsub.Close() |
| 83 |
|
| 84 |
channels, err = client.PubSubChannels("mychannel*").Result() |
| 85 |
Expect(err).NotTo(HaveOccurred()) |
| 86 |
Expect(channels).To(ConsistOf([]string{"mychannel", "mychannel2"})) |
| 87 |
|
| 88 |
channels, err = client.PubSubChannels("").Result() |
| 89 |
Expect(err).NotTo(HaveOccurred()) |
| 90 |
Expect(channels).To(BeEmpty()) |
| 91 |
|
| 92 |
channels, err = client.PubSubChannels("*").Result() |
| 93 |
Expect(err).NotTo(HaveOccurred()) |
| 94 |
Expect(len(channels)).To(BeNumerically(">=", 2)) |
| 95 |
}) |
| 96 |
|
| 97 |
It("should return the numbers of subscribers", func() { |
| 98 |
pubsub, err := client.Subscribe("mychannel", "mychannel2") |
| 99 |
Expect(err).NotTo(HaveOccurred()) |
| 100 |
defer pubsub.Close() |
| 101 |
|
| 102 |
channels, err := client.PubSubNumSub("mychannel", "mychannel2", "mychannel3").Result() |
| 103 |
Expect(err).NotTo(HaveOccurred()) |
| 104 |
Expect(channels).To(Equal(map[string]int64{ |
| 105 |
"mychannel": 1, |
| 106 |
"mychannel2": 1, |
| 107 |
"mychannel3": 0, |
| 108 |
})) |
| 109 |
}) |
| 110 |
|
| 111 |
It("should return the numbers of subscribers by pattern", func() { |
| 112 |
num, err := client.PubSubNumPat().Result() |
| 113 |
Expect(err).NotTo(HaveOccurred()) |
| 114 |
Expect(num).To(Equal(int64(0))) |
| 115 |
|
| 116 |
pubsub, err := client.PSubscribe("*") |
| 117 |
Expect(err).NotTo(HaveOccurred()) |
| 118 |
defer pubsub.Close() |
| 119 |
|
| 120 |
num, err = client.PubSubNumPat().Result() |
| 121 |
Expect(err).NotTo(HaveOccurred()) |
| 122 |
Expect(num).To(Equal(int64(1))) |
| 123 |
}) |
| 124 |
|
| 125 |
It("should pub/sub", func() { |
| 126 |
pubsub, err := client.Subscribe("mychannel", "mychannel2") |
| 127 |
Expect(err).NotTo(HaveOccurred()) |
| 128 |
defer pubsub.Close() |
| 129 |
|
| 130 |
{ |
| 131 |
msgi, err := pubsub.ReceiveTimeout(time.Second) |
| 132 |
Expect(err).NotTo(HaveOccurred()) |
| 133 |
subscr := msgi.(*redis.Subscription) |
| 134 |
Expect(subscr.Kind).To(Equal("subscribe")) |
| 135 |
Expect(subscr.Channel).To(Equal("mychannel")) |
| 136 |
Expect(subscr.Count).To(Equal(1)) |
| 137 |
} |
| 138 |
|
| 139 |
{ |
| 140 |
msgi, err := pubsub.ReceiveTimeout(time.Second) |
| 141 |
Expect(err).NotTo(HaveOccurred()) |
| 142 |
subscr := msgi.(*redis.Subscription) |
| 143 |
Expect(subscr.Kind).To(Equal("subscribe")) |
| 144 |
Expect(subscr.Channel).To(Equal("mychannel2")) |
| 145 |
Expect(subscr.Count).To(Equal(2)) |
| 146 |
} |
| 147 |
|
| 148 |
{ |
| 149 |
msgi, err := pubsub.ReceiveTimeout(time.Second) |
| 150 |
Expect(err.(net.Error).Timeout()).To(Equal(true)) |
| 151 |
Expect(msgi).NotTo(HaveOccurred()) |
| 152 |
} |
| 153 |
|
| 154 |
n, err := client.Publish("mychannel", "hello").Result() |
| 155 |
Expect(err).NotTo(HaveOccurred()) |
| 156 |
Expect(n).To(Equal(int64(1))) |
| 157 |
|
| 158 |
n, err = client.Publish("mychannel2", "hello2").Result() |
| 159 |
Expect(err).NotTo(HaveOccurred()) |
| 160 |
Expect(n).To(Equal(int64(1))) |
| 161 |
|
| 162 |
Expect(pubsub.Unsubscribe("mychannel", "mychannel2")).NotTo(HaveOccurred()) |
| 163 |
|
| 164 |
{ |
| 165 |
msgi, err := pubsub.ReceiveTimeout(time.Second) |
| 166 |
Expect(err).NotTo(HaveOccurred()) |
| 167 |
subscr := msgi.(*redis.Message) |
| 168 |
Expect(subscr.Channel).To(Equal("mychannel")) |
| 169 |
Expect(subscr.Payload).To(Equal("hello")) |
| 170 |
} |
| 171 |
|
| 172 |
{ |
| 173 |
msgi, err := pubsub.ReceiveTimeout(time.Second) |
| 174 |
Expect(err).NotTo(HaveOccurred()) |
| 175 |
msg := msgi.(*redis.Message) |
| 176 |
Expect(msg.Channel).To(Equal("mychannel2")) |
| 177 |
Expect(msg.Payload).To(Equal("hello2")) |
| 178 |
} |
| 179 |
|
| 180 |
{ |
| 181 |
msgi, err := pubsub.ReceiveTimeout(time.Second) |
| 182 |
Expect(err).NotTo(HaveOccurred()) |
| 183 |
subscr := msgi.(*redis.Subscription) |
| 184 |
Expect(subscr.Kind).To(Equal("unsubscribe")) |
| 185 |
Expect(subscr.Channel).To(Equal("mychannel")) |
| 186 |
Expect(subscr.Count).To(Equal(1)) |
| 187 |
} |
| 188 |
|
| 189 |
{ |
| 190 |
msgi, err := pubsub.ReceiveTimeout(time.Second) |
| 191 |
Expect(err).NotTo(HaveOccurred()) |
| 192 |
subscr := msgi.(*redis.Subscription) |
| 193 |
Expect(subscr.Kind).To(Equal("unsubscribe")) |
| 194 |
Expect(subscr.Channel).To(Equal("mychannel2")) |
| 195 |
Expect(subscr.Count).To(Equal(0)) |
| 196 |
} |
| 197 |
|
| 198 |
stats := client.PoolStats() |
| 199 |
Expect(stats.Requests - stats.Hits).To(Equal(uint32(2))) |
| 200 |
}) |
| 201 |
|
| 202 |
It("should ping/pong", func() { |
| 203 |
pubsub, err := client.Subscribe("mychannel") |
| 204 |
Expect(err).NotTo(HaveOccurred()) |
| 205 |
defer pubsub.Close() |
| 206 |
|
| 207 |
_, err = pubsub.ReceiveTimeout(time.Second) |
| 208 |
Expect(err).NotTo(HaveOccurred()) |
| 209 |
|
| 210 |
err = pubsub.Ping("") |
| 211 |
Expect(err).NotTo(HaveOccurred()) |
| 212 |
|
| 213 |
msgi, err := pubsub.ReceiveTimeout(time.Second) |
| 214 |
Expect(err).NotTo(HaveOccurred()) |
| 215 |
pong := msgi.(*redis.Pong) |
| 216 |
Expect(pong.Payload).To(Equal("")) |
| 217 |
}) |
| 218 |
|
| 219 |
It("should ping/pong with payload", func() { |
| 220 |
pubsub, err := client.Subscribe("mychannel") |
| 221 |
Expect(err).NotTo(HaveOccurred()) |
| 222 |
defer pubsub.Close() |
| 223 |
|
| 224 |
_, err = pubsub.ReceiveTimeout(time.Second) |
| 225 |
Expect(err).NotTo(HaveOccurred()) |
| 226 |
|
| 227 |
err = pubsub.Ping("hello") |
| 228 |
Expect(err).NotTo(HaveOccurred()) |
| 229 |
|
| 230 |
msgi, err := pubsub.ReceiveTimeout(time.Second) |
| 231 |
Expect(err).NotTo(HaveOccurred()) |
| 232 |
pong := msgi.(*redis.Pong) |
| 233 |
Expect(pong.Payload).To(Equal("hello")) |
| 234 |
}) |
| 235 |
|
| 236 |
It("should multi-ReceiveMessage", func() { |
| 237 |
pubsub, err := client.Subscribe("mychannel") |
| 238 |
Expect(err).NotTo(HaveOccurred()) |
| 239 |
defer pubsub.Close() |
| 240 |
|
| 241 |
err = client.Publish("mychannel", "hello").Err() |
| 242 |
Expect(err).NotTo(HaveOccurred()) |
| 243 |
|
| 244 |
err = client.Publish("mychannel", "world").Err() |
| 245 |
Expect(err).NotTo(HaveOccurred()) |
| 246 |
|
| 247 |
msg, err := pubsub.ReceiveMessage() |
| 248 |
Expect(err).NotTo(HaveOccurred()) |
| 249 |
Expect(msg.Channel).To(Equal("mychannel")) |
| 250 |
Expect(msg.Payload).To(Equal("hello")) |
| 251 |
|
| 252 |
msg, err = pubsub.ReceiveMessage() |
| 253 |
Expect(err).NotTo(HaveOccurred()) |
| 254 |
Expect(msg.Channel).To(Equal("mychannel")) |
| 255 |
Expect(msg.Payload).To(Equal("world")) |
| 256 |
}) |
| 257 |
|
| 258 |
It("should ReceiveMessage after timeout", func() { |
| 259 |
timeout := 100 * time.Millisecond |
| 260 |
|
| 261 |
pubsub, err := client.Subscribe("mychannel") |
| 262 |
Expect(err).NotTo(HaveOccurred()) |
| 263 |
defer pubsub.Close() |
| 264 |
|
| 265 |
done := make(chan bool, 1) |
| 266 |
go func() { |
| 267 |
defer GinkgoRecover() |
| 268 |
defer func() { |
| 269 |
done <- true |
| 270 |
}() |
| 271 |
|
| 272 |
time.Sleep(timeout + 100*time.Millisecond) |
| 273 |
n, err := client.Publish("mychannel", "hello").Result() |
| 274 |
Expect(err).NotTo(HaveOccurred()) |
| 275 |
Expect(n).To(Equal(int64(1))) |
| 276 |
}() |
| 277 |
|
| 278 |
msg, err := pubsub.ReceiveMessageTimeout(timeout) |
| 279 |
Expect(err).NotTo(HaveOccurred()) |
| 280 |
Expect(msg.Channel).To(Equal("mychannel")) |
| 281 |
Expect(msg.Payload).To(Equal("hello")) |
| 282 |
|
| 283 |
Eventually(done).Should(Receive()) |
| 284 |
|
| 285 |
stats := client.PoolStats() |
| 286 |
Expect(stats.Requests).To(Equal(uint32(3))) |
| 287 |
Expect(stats.Hits).To(Equal(uint32(1))) |
| 288 |
}) |
| 289 |
|
| 290 |
expectReceiveMessageOnError := func(pubsub *redis.PubSub) { |
| 291 |
cn, _, err := pubsub.Pool().Get() |
| 292 |
Expect(err).NotTo(HaveOccurred()) |
| 293 |
cn.SetNetConn(&badConn{ |
| 294 |
readErr: io.EOF, |
| 295 |
writeErr: io.EOF, |
| 296 |
}) |
| 297 |
pubsub.Pool().Put(cn) |
| 298 |
|
| 299 |
done := make(chan bool, 1) |
| 300 |
go func() { |
| 301 |
defer GinkgoRecover() |
| 302 |
defer func() { |
| 303 |
done <- true |
| 304 |
}() |
| 305 |
|
| 306 |
time.Sleep(100 * time.Millisecond) |
| 307 |
err := client.Publish("mychannel", "hello").Err() |
| 308 |
Expect(err).NotTo(HaveOccurred()) |
| 309 |
}() |
| 310 |
|
| 311 |
msg, err := pubsub.ReceiveMessage() |
| 312 |
Expect(err).NotTo(HaveOccurred()) |
| 313 |
Expect(msg.Channel).To(Equal("mychannel")) |
| 314 |
Expect(msg.Payload).To(Equal("hello")) |
| 315 |
|
| 316 |
Eventually(done).Should(Receive()) |
| 317 |
|
| 318 |
stats := client.PoolStats() |
| 319 |
Expect(stats.Requests).To(Equal(uint32(4))) |
| 320 |
Expect(stats.Hits).To(Equal(uint32(1))) |
| 321 |
} |
| 322 |
|
| 323 |
It("Subscribe should reconnect on ReceiveMessage error", func() { |
| 324 |
pubsub, err := client.Subscribe("mychannel") |
| 325 |
Expect(err).NotTo(HaveOccurred()) |
| 326 |
defer pubsub.Close() |
| 327 |
|
| 328 |
expectReceiveMessageOnError(pubsub) |
| 329 |
}) |
| 330 |
|
| 331 |
It("PSubscribe should reconnect on ReceiveMessage error", func() { |
| 332 |
pubsub, err := client.PSubscribe("mychannel") |
| 333 |
Expect(err).NotTo(HaveOccurred()) |
| 334 |
defer pubsub.Close() |
| 335 |
|
| 336 |
expectReceiveMessageOnError(pubsub) |
| 337 |
}) |
| 338 |
|
| 339 |
It("should return on Close", func() { |
| 340 |
pubsub, err := client.Subscribe("mychannel") |
| 341 |
Expect(err).NotTo(HaveOccurred()) |
| 342 |
defer pubsub.Close() |
| 343 |
|
| 344 |
var wg sync.WaitGroup |
| 345 |
wg.Add(1) |
| 346 |
go func() { |
| 347 |
defer GinkgoRecover() |
| 348 |
|
| 349 |
wg.Done() |
| 350 |
defer wg.Done() |
| 351 |
|
| 352 |
_, err := pubsub.ReceiveMessage() |
| 353 |
Expect(err).To(HaveOccurred()) |
| 354 |
Expect(err).To(SatisfyAny( |
| 355 |
MatchError("redis: client is closed"), |
| 356 |
MatchError("use of closed network connection"), // Go 1.4 |
| 357 |
)) |
| 358 |
}() |
| 359 |
|
| 360 |
wg.Wait() |
| 361 |
wg.Add(1) |
| 362 |
|
| 363 |
err = pubsub.Close() |
| 364 |
Expect(err).NotTo(HaveOccurred()) |
| 365 |
|
| 366 |
wg.Wait() |
| 367 |
}) |
| 368 |
|
| 369 |
It("should ReceiveMessage without a subscription", func() { |
| 370 |
timeout := 100 * time.Millisecond |
| 371 |
|
| 372 |
pubsub, err := client.Subscribe() |
| 373 |
Expect(err).NotTo(HaveOccurred()) |
| 374 |
defer pubsub.Close() |
| 375 |
|
| 376 |
go func() { |
| 377 |
defer GinkgoRecover() |
| 378 |
|
| 379 |
time.Sleep(2 * timeout) |
| 380 |
err = pubsub.Subscribe("mychannel") |
| 381 |
Expect(err).NotTo(HaveOccurred()) |
| 382 |
|
| 383 |
err := client.Publish("mychannel", "hello").Err() |
| 384 |
Expect(err).NotTo(HaveOccurred()) |
| 385 |
}() |
| 386 |
|
| 387 |
msg, err := pubsub.ReceiveMessageTimeout(timeout) |
| 388 |
Expect(err).NotTo(HaveOccurred()) |
| 389 |
Expect(msg.Channel).To(Equal("mychannel")) |
| 390 |
Expect(msg.Payload).To(Equal("hello")) |
| 391 |
}) |
| 392 |
}) |