Fix race in tests.

This commit is contained in:
Vladimir Mihailenco 2016-04-09 11:34:40 +03:00
parent b351402995
commit 787609d1e6
3 changed files with 9 additions and 8 deletions

View File

@ -14,6 +14,6 @@ func (c *PubSub) Pool() pool.Pooler {
return c.base.connPool
}
func SetReceiveMessageTimeout(d time.Duration) {
receiveMessageTimeout = d
func (c *PubSub) ReceiveMessageTimeout(timeout time.Duration) (*Message, error) {
return c.receiveMessage(timeout)
}

View File

@ -8,8 +8,6 @@ import (
"gopkg.in/redis.v3/internal/pool"
)
var receiveMessageTimeout = 5 * time.Second
// Posts a message to the given channel.
func (c *Client) Publish(channel, message string) *IntCmd {
req := NewIntCmd("PUBLISH", channel, message)
@ -255,9 +253,13 @@ func (c *PubSub) Receive() (interface{}, error) {
// messages. It automatically reconnects to Redis Server and resubscribes
// to channels in case of network errors.
func (c *PubSub) ReceiveMessage() (*Message, error) {
return c.receiveMessage(5 * time.Second)
}
func (c *PubSub) receiveMessage(timeout time.Duration) (*Message, error) {
var errNum uint
for {
msgi, err := c.ReceiveTimeout(receiveMessageTimeout)
msgi, err := c.ReceiveTimeout(timeout)
if err != nil {
if !isNetworkError(err) {
return nil, err

View File

@ -256,8 +256,7 @@ var _ = Describe("PubSub", func() {
})
It("should ReceiveMessage after timeout", func() {
timeout := time.Second
redis.SetReceiveMessageTimeout(timeout)
timeout := 100 * time.Millisecond
pubsub, err := client.Subscribe("mychannel")
Expect(err).NotTo(HaveOccurred())
@ -276,7 +275,7 @@ var _ = Describe("PubSub", func() {
Expect(n).To(Equal(int64(1)))
}()
msg, err := pubsub.ReceiveMessage()
msg, err := pubsub.ReceiveMessageTimeout(timeout)
Expect(err).NotTo(HaveOccurred())
Expect(msg.Channel).To(Equal("mychannel"))
Expect(msg.Payload).To(Equal("hello"))