1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-29 17:41:15 +03:00

pubsub: add PING support.

This commit is contained in:
Vladimir Mihailenco
2015-07-11 13:12:47 +03:00
parent 39c9dc2665
commit f901321d84
3 changed files with 91 additions and 40 deletions

View File

@ -12,24 +12,22 @@ import (
var _ = Describe("PubSub", func() {
var client *redis.Client
var pubsub *redis.PubSub
BeforeEach(func() {
client = redis.NewClient(&redis.Options{
Addr: redisAddr,
})
Expect(client.FlushDb().Err()).NotTo(HaveOccurred())
pubsub = client.PubSub()
})
AfterEach(func() {
Expect(client.FlushDb().Err()).NotTo(HaveOccurred())
Expect(pubsub.Close()).NotTo(HaveOccurred())
Expect(client.Close()).NotTo(HaveOccurred())
})
It("should support pattern matching", func() {
pubsub := client.PubSub()
defer func() {
Expect(pubsub.Close()).NotTo(HaveOccurred())
}()
Expect(pubsub.PSubscribe("mychannel*")).NotTo(HaveOccurred())
pub := client.Publish("mychannel1", "hello")
@ -77,8 +75,6 @@ var _ = Describe("PubSub", func() {
Expect(err).NotTo(HaveOccurred())
Expect(channels).To(BeEmpty())
pubsub := client.PubSub()
defer pubsub.Close()
Expect(pubsub.Subscribe("mychannel", "mychannel2")).NotTo(HaveOccurred())
channels, err = client.PubSubChannels("mychannel*").Result()
@ -95,8 +91,6 @@ var _ = Describe("PubSub", func() {
})
It("should return the numbers of subscribers", func() {
pubsub := client.PubSub()
defer pubsub.Close()
Expect(pubsub.Subscribe("mychannel", "mychannel2")).NotTo(HaveOccurred())
channels, err := client.PubSubNumSub("mychannel", "mychannel2", "mychannel3").Result()
@ -113,8 +107,6 @@ var _ = Describe("PubSub", func() {
Expect(err).NotTo(HaveOccurred())
Expect(num).To(Equal(int64(0)))
pubsub := client.PubSub()
defer pubsub.Close()
Expect(pubsub.PSubscribe("*")).NotTo(HaveOccurred())
num, err = client.PubSubNumPat().Result()
@ -123,11 +115,6 @@ var _ = Describe("PubSub", func() {
})
It("should pub/sub", func() {
pubsub := client.PubSub()
defer func() {
Expect(pubsub.Close()).NotTo(HaveOccurred())
}()
Expect(pubsub.Subscribe("mychannel", "mychannel2")).NotTo(HaveOccurred())
pub := client.Publish("mychannel", "hello")
@ -199,4 +186,36 @@ var _ = Describe("PubSub", func() {
}
})
It("should ping/pong", func() {
err := pubsub.Subscribe("mychannel")
Expect(err).NotTo(HaveOccurred())
_, err = pubsub.ReceiveTimeout(time.Second)
Expect(err).NotTo(HaveOccurred())
err = pubsub.Ping("")
Expect(err).NotTo(HaveOccurred())
msgi, err := pubsub.ReceiveTimeout(time.Second)
Expect(err).NotTo(HaveOccurred())
pong := msgi.(*redis.Pong)
Expect(pong.Payload).To(Equal(""))
})
It("should ping/pong with payload", func() {
err := pubsub.Subscribe("mychannel")
Expect(err).NotTo(HaveOccurred())
_, err = pubsub.ReceiveTimeout(time.Second)
Expect(err).NotTo(HaveOccurred())
err = pubsub.Ping("hello")
Expect(err).NotTo(HaveOccurred())
msgi, err := pubsub.ReceiveTimeout(time.Second)
Expect(err).NotTo(HaveOccurred())
pong := msgi.(*redis.Pong)
Expect(pong.Payload).To(Equal("hello"))
})
})