forked from mirror/redis
Migrates tests to ginkgo/gomega
This commit is contained in:
parent
f7399b48cd
commit
f6bca78168
|
@ -0,0 +1 @@
|
|||
*.rdb
|
|
@ -11,7 +11,8 @@ go:
|
|||
|
||||
install:
|
||||
- go get gopkg.in/bufio.v1
|
||||
- go get gopkg.in/check.v1
|
||||
- go get onsi.github.io/ginkgo
|
||||
- go get onsi.github.io/gomega
|
||||
- mkdir -p $HOME/gopath/src/gopkg.in
|
||||
- ln -s `pwd` $HOME/gopath/src/gopkg.in/redis.v2
|
||||
|
||||
|
|
4
Makefile
4
Makefile
|
@ -1,3 +1,3 @@
|
|||
all:
|
||||
go test gopkg.in/redis.v2 -cpu=1,2,4
|
||||
go test gopkg.in/redis.v2 -short -race
|
||||
go test ./... -cpu=1,2,4
|
||||
go test ./... -short -race
|
||||
|
|
|
@ -0,0 +1,167 @@
|
|||
package redis_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"gopkg.in/redis.v2"
|
||||
)
|
||||
|
||||
var _ = Describe("Command", func() {
|
||||
var client *redis.Client
|
||||
|
||||
BeforeEach(func() {
|
||||
client = redis.NewTCPClient(&redis.Options{
|
||||
Addr: redisAddr,
|
||||
})
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
Expect(client.FlushDb().Err()).NotTo(HaveOccurred())
|
||||
Expect(client.Close()).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should have a plain string result", func() {
|
||||
set := client.Set("foo", "bar")
|
||||
Expect(set.String()).To(Equal("SET foo bar: OK"))
|
||||
|
||||
get := client.Get("foo")
|
||||
Expect(get.String()).To(Equal("GET foo: bar"))
|
||||
})
|
||||
|
||||
It("should have correct val/err states", func() {
|
||||
set := client.Set("key", "hello")
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
get := client.Get("key")
|
||||
Expect(get.Err()).NotTo(HaveOccurred())
|
||||
Expect(get.Val()).To(Equal("hello"))
|
||||
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
})
|
||||
|
||||
It("should escape special chars", func() {
|
||||
set := client.Set("key", "hello1\r\nhello2\r\n")
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
get := client.Get("key")
|
||||
Expect(get.Err()).NotTo(HaveOccurred())
|
||||
Expect(get.Val()).To(Equal("hello1\r\nhello2\r\n"))
|
||||
})
|
||||
|
||||
It("should handle big vals", func() {
|
||||
val := string(bytes.Repeat([]byte{'*'}, 1<<16))
|
||||
set := client.Set("key", val)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
get := client.Get("key")
|
||||
Expect(get.Err()).NotTo(HaveOccurred())
|
||||
Expect(get.Val()).To(Equal(val))
|
||||
})
|
||||
|
||||
It("should handle many keys #1", func() {
|
||||
const n = 100000
|
||||
for i := 0; i < n; i++ {
|
||||
client.Set("keys.key"+strconv.Itoa(i), "hello"+strconv.Itoa(i))
|
||||
}
|
||||
keys := client.Keys("keys.*")
|
||||
Expect(keys.Err()).NotTo(HaveOccurred())
|
||||
Expect(len(keys.Val())).To(Equal(n))
|
||||
})
|
||||
|
||||
It("should handle many keys #2", func() {
|
||||
const n = 100000
|
||||
|
||||
keys := []string{"non-existent-key"}
|
||||
for i := 0; i < n; i++ {
|
||||
key := "keys.key" + strconv.Itoa(i)
|
||||
client.Set(key, "hello"+strconv.Itoa(i))
|
||||
keys = append(keys, key)
|
||||
}
|
||||
keys = append(keys, "non-existent-key")
|
||||
|
||||
mget := client.MGet(keys...)
|
||||
Expect(mget.Err()).NotTo(HaveOccurred())
|
||||
Expect(len(mget.Val())).To(Equal(n + 2))
|
||||
vals := mget.Val()
|
||||
for i := 0; i < n; i++ {
|
||||
Expect(vals[i+1]).To(Equal("hello" + strconv.Itoa(i)))
|
||||
}
|
||||
Expect(vals[0]).To(BeNil())
|
||||
Expect(vals[n+1]).To(BeNil())
|
||||
})
|
||||
|
||||
It("should convert strings via helpers", func() {
|
||||
set := client.Set("key", "10")
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
|
||||
n, err := client.Get("key").Int64()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(n).To(Equal(int64(10)))
|
||||
|
||||
un, err := client.Get("key").Uint64()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(un).To(Equal(uint64(10)))
|
||||
|
||||
f, err := client.Get("key").Float64()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(f).To(Equal(float64(10)))
|
||||
})
|
||||
|
||||
Describe("races", func() {
|
||||
|
||||
It("should echo", func() {
|
||||
var n = 10000
|
||||
if testing.Short() {
|
||||
n = 1000
|
||||
}
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(n)
|
||||
for i := 0; i < n; i++ {
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
|
||||
msg := "echo" + strconv.Itoa(i)
|
||||
echo := client.Echo(msg)
|
||||
Expect(echo.Err()).NotTo(HaveOccurred())
|
||||
Expect(echo.Val()).To(Equal(msg))
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
})
|
||||
|
||||
It("should incr", func() {
|
||||
var n = 10000
|
||||
if testing.Short() {
|
||||
n = 1000
|
||||
}
|
||||
|
||||
key := "TestIncrFromGoroutines"
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(n)
|
||||
for i := 0; i < n; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
err := client.Incr(key).Err()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
val, err := client.Get(key).Int64()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(val).To(Equal(int64(n)))
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
})
|
File diff suppressed because it is too large
Load Diff
|
@ -30,7 +30,7 @@ func ExampleNewTCPClient() {
|
|||
|
||||
func ExampleNewFailoverClient() {
|
||||
client := redis.NewFailoverClient(&redis.FailoverOptions{
|
||||
MasterName: "master",
|
||||
MasterName: "mymaster",
|
||||
SentinelAddrs: []string{":26379"},
|
||||
})
|
||||
|
||||
|
|
|
@ -0,0 +1,122 @@
|
|||
package redis_test
|
||||
|
||||
import (
|
||||
"gopkg.in/redis.v2"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Multi", func() {
|
||||
var client *redis.Client
|
||||
|
||||
BeforeEach(func() {
|
||||
client = redis.NewTCPClient(&redis.Options{
|
||||
Addr: redisAddr,
|
||||
})
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
Expect(client.FlushDb().Err()).NotTo(HaveOccurred())
|
||||
Expect(client.Close()).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should exec", func() {
|
||||
multi := client.Multi()
|
||||
defer func() {
|
||||
Expect(multi.Close()).NotTo(HaveOccurred())
|
||||
}()
|
||||
|
||||
var (
|
||||
set *redis.StatusCmd
|
||||
get *redis.StringCmd
|
||||
)
|
||||
cmds, err := multi.Exec(func() error {
|
||||
set = multi.Set("key", "hello")
|
||||
get = multi.Get("key")
|
||||
return nil
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(cmds).To(HaveLen(2))
|
||||
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
Expect(get.Err()).NotTo(HaveOccurred())
|
||||
Expect(get.Val()).To(Equal("hello"))
|
||||
})
|
||||
|
||||
It("should discard", func() {
|
||||
multi := client.Multi()
|
||||
defer func() {
|
||||
Expect(multi.Close()).NotTo(HaveOccurred())
|
||||
}()
|
||||
|
||||
cmds, err := multi.Exec(func() error {
|
||||
multi.Set("key1", "hello1")
|
||||
multi.Discard()
|
||||
multi.Set("key2", "hello2")
|
||||
return nil
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(cmds).To(HaveLen(1))
|
||||
|
||||
get := client.Get("key1")
|
||||
Expect(get.Err()).To(Equal(redis.Nil))
|
||||
Expect(get.Val()).To(Equal(""))
|
||||
|
||||
get = client.Get("key2")
|
||||
Expect(get.Err()).NotTo(HaveOccurred())
|
||||
Expect(get.Val()).To(Equal("hello2"))
|
||||
})
|
||||
|
||||
It("should exec empty", func() {
|
||||
multi := client.Multi()
|
||||
defer func() {
|
||||
Expect(multi.Close()).NotTo(HaveOccurred())
|
||||
}()
|
||||
|
||||
cmds, err := multi.Exec(func() error { return nil })
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(cmds).To(HaveLen(0))
|
||||
|
||||
ping := multi.Ping()
|
||||
Expect(ping.Err()).NotTo(HaveOccurred())
|
||||
Expect(ping.Val()).To(Equal("PONG"))
|
||||
})
|
||||
|
||||
It("should exec empty queue", func() {
|
||||
multi := client.Multi()
|
||||
defer func() {
|
||||
Expect(multi.Close()).NotTo(HaveOccurred())
|
||||
}()
|
||||
|
||||
cmds, err := multi.Exec(func() error { return nil })
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(cmds).To(HaveLen(0))
|
||||
})
|
||||
|
||||
It("should exec bulks", func() {
|
||||
multi := client.Multi()
|
||||
defer func() {
|
||||
Expect(multi.Close()).NotTo(HaveOccurred())
|
||||
}()
|
||||
|
||||
cmds, err := multi.Exec(func() error {
|
||||
for i := int64(0); i < 20000; i++ {
|
||||
multi.Incr("key")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(cmds)).To(Equal(20000))
|
||||
for _, cmd := range cmds {
|
||||
Expect(cmd.Err()).NotTo(HaveOccurred())
|
||||
}
|
||||
|
||||
get := client.Get("key")
|
||||
Expect(get.Err()).NotTo(HaveOccurred())
|
||||
Expect(get.Val()).To(Equal("20000"))
|
||||
})
|
||||
|
||||
})
|
|
@ -0,0 +1,152 @@
|
|||
package redis_test
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"gopkg.in/redis.v2"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Pipelining", func() {
|
||||
var client *redis.Client
|
||||
|
||||
BeforeEach(func() {
|
||||
client = redis.NewTCPClient(&redis.Options{
|
||||
Addr: redisAddr,
|
||||
})
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
Expect(client.FlushDb().Err()).NotTo(HaveOccurred())
|
||||
Expect(client.Close()).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should pipeline", func() {
|
||||
set := client.Set("key2", "hello2")
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
pipeline := client.Pipeline()
|
||||
set = pipeline.Set("key1", "hello1")
|
||||
get := pipeline.Get("key2")
|
||||
incr := pipeline.Incr("key3")
|
||||
getNil := pipeline.Get("key4")
|
||||
|
||||
cmds, err := pipeline.Exec()
|
||||
Expect(err).To(Equal(redis.Nil))
|
||||
Expect(cmds).To(HaveLen(4))
|
||||
Expect(pipeline.Close()).NotTo(HaveOccurred())
|
||||
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
Expect(get.Err()).NotTo(HaveOccurred())
|
||||
Expect(get.Val()).To(Equal("hello2"))
|
||||
|
||||
Expect(incr.Err()).NotTo(HaveOccurred())
|
||||
Expect(incr.Val()).To(Equal(int64(1)))
|
||||
|
||||
Expect(getNil.Err()).To(Equal(redis.Nil))
|
||||
Expect(getNil.Val()).To(Equal(""))
|
||||
})
|
||||
|
||||
It("should discard", func() {
|
||||
pipeline := client.Pipeline()
|
||||
|
||||
pipeline.Get("key")
|
||||
pipeline.Discard()
|
||||
cmds, err := pipeline.Exec()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(cmds).To(HaveLen(0))
|
||||
Expect(pipeline.Close()).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should support block style", func() {
|
||||
var get *redis.StringCmd
|
||||
cmds, err := client.Pipelined(func(pipe *redis.Pipeline) error {
|
||||
get = pipe.Get("foo")
|
||||
return nil
|
||||
})
|
||||
Expect(err).To(Equal(redis.Nil))
|
||||
Expect(cmds).To(HaveLen(1))
|
||||
Expect(cmds[0]).To(Equal(get))
|
||||
Expect(get.Err()).To(Equal(redis.Nil))
|
||||
Expect(get.Val()).To(Equal(""))
|
||||
})
|
||||
|
||||
It("should handle vals/err", func() {
|
||||
pipeline := client.Pipeline()
|
||||
|
||||
get := pipeline.Get("key")
|
||||
Expect(get.Err()).NotTo(HaveOccurred())
|
||||
Expect(get.Val()).To(Equal(""))
|
||||
Expect(pipeline.Close()).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should pipeline with empty queue", func() {
|
||||
pipeline := client.Pipeline()
|
||||
cmds, err := pipeline.Exec()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(cmds).To(HaveLen(0))
|
||||
Expect(pipeline.Close()).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should increment correctly", func() {
|
||||
const N = 20000
|
||||
key := "TestPipelineIncr"
|
||||
pipeline := client.Pipeline()
|
||||
for i := 0; i < N; i++ {
|
||||
pipeline.Incr(key)
|
||||
}
|
||||
|
||||
cmds, err := pipeline.Exec()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(pipeline.Close()).NotTo(HaveOccurred())
|
||||
|
||||
Expect(len(cmds)).To(Equal(20000))
|
||||
for _, cmd := range cmds {
|
||||
Expect(cmd.Err()).NotTo(HaveOccurred())
|
||||
}
|
||||
|
||||
get := client.Get(key)
|
||||
Expect(get.Err()).NotTo(HaveOccurred())
|
||||
Expect(get.Val()).To(Equal(strconv.Itoa(N)))
|
||||
})
|
||||
|
||||
It("should PipelineEcho", func() {
|
||||
const N = 1000
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(N)
|
||||
for i := 0; i < N; i++ {
|
||||
go func(i int) {
|
||||
pipeline := client.Pipeline()
|
||||
|
||||
msg1 := "echo" + strconv.Itoa(i)
|
||||
msg2 := "echo" + strconv.Itoa(i+1)
|
||||
|
||||
echo1 := pipeline.Echo(msg1)
|
||||
echo2 := pipeline.Echo(msg2)
|
||||
|
||||
cmds, err := pipeline.Exec()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(cmds).To(HaveLen(2))
|
||||
|
||||
Expect(echo1.Err()).NotTo(HaveOccurred())
|
||||
Expect(echo1.Val()).To(Equal(msg1))
|
||||
|
||||
Expect(echo2.Err()).NotTo(HaveOccurred())
|
||||
Expect(echo2.Val()).To(Equal(msg2))
|
||||
|
||||
Expect(pipeline.Close()).NotTo(HaveOccurred())
|
||||
|
||||
wg.Done()
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
})
|
||||
|
||||
})
|
|
@ -0,0 +1,122 @@
|
|||
package redis_test
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"gopkg.in/redis.v2"
|
||||
)
|
||||
|
||||
var _ = Describe("Pool", func() {
|
||||
var client *redis.Client
|
||||
var perform = func(n int, cb func()) {
|
||||
wg := &sync.WaitGroup{}
|
||||
for i := 0; i < n; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
cb()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
BeforeEach(func() {
|
||||
client = redis.NewTCPClient(&redis.Options{
|
||||
Addr: redisAddr,
|
||||
})
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
client.FlushDb()
|
||||
Expect(client.Close()).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should respect max size", func() {
|
||||
perform(1000, func() {
|
||||
val, err := client.Ping().Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(val).To(Equal("PONG"))
|
||||
})
|
||||
Expect(client.Pool().Size()).To(Equal(10))
|
||||
Expect(client.Pool().Len()).To(Equal(10))
|
||||
})
|
||||
|
||||
It("should respect max on multi", func() {
|
||||
perform(1000, func() {
|
||||
var ping *redis.StatusCmd
|
||||
|
||||
multi := client.Multi()
|
||||
cmds, err := multi.Exec(func() error {
|
||||
ping = multi.Ping()
|
||||
return nil
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(cmds).To(HaveLen(1))
|
||||
Expect(ping.Err()).NotTo(HaveOccurred())
|
||||
Expect(ping.Val()).To(Equal("PONG"))
|
||||
Expect(multi.Close()).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
Expect(client.Pool().Size()).To(Equal(10))
|
||||
Expect(client.Pool().Len()).To(Equal(10))
|
||||
})
|
||||
|
||||
It("should respect max on pipelines", func() {
|
||||
perform(1000, func() {
|
||||
pipe := client.Pipeline()
|
||||
ping := pipe.Ping()
|
||||
cmds, err := pipe.Exec()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(cmds).To(HaveLen(1))
|
||||
Expect(ping.Err()).NotTo(HaveOccurred())
|
||||
Expect(ping.Val()).To(Equal("PONG"))
|
||||
Expect(pipe.Close()).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
Expect(client.Pool().Size()).To(Equal(10))
|
||||
Expect(client.Pool().Len()).To(Equal(10))
|
||||
})
|
||||
|
||||
It("should respect max on pubsub", func() {
|
||||
perform(10, func() {
|
||||
pubsub := client.PubSub()
|
||||
Expect(pubsub.Subscribe()).NotTo(HaveOccurred())
|
||||
Expect(pubsub.Close()).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
Expect(client.Pool().Size()).To(Equal(0))
|
||||
Expect(client.Pool().Len()).To(Equal(0))
|
||||
})
|
||||
|
||||
It("should remove broken connections", func() {
|
||||
cn, _, err := client.Pool().Get()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(cn.Close()).NotTo(HaveOccurred())
|
||||
Expect(client.Pool().Put(cn)).NotTo(HaveOccurred())
|
||||
|
||||
err = client.Ping().Err()
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.Error()).To(Equal("use of closed network connection"))
|
||||
|
||||
val, err := client.Ping().Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(val).To(Equal("PONG"))
|
||||
|
||||
Expect(client.Pool().Size()).To(Equal(1))
|
||||
Expect(client.Pool().Len()).To(Equal(1))
|
||||
})
|
||||
|
||||
It("should reuse connections", func() {
|
||||
for i := 0; i < 100; i++ {
|
||||
val, err := client.Ping().Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(val).To(Equal("PONG"))
|
||||
}
|
||||
|
||||
Expect(client.Pool().Size()).To(Equal(1))
|
||||
Expect(client.Pool().Len()).To(Equal(1))
|
||||
})
|
||||
|
||||
})
|
|
@ -0,0 +1,202 @@
|
|||
package redis_test
|
||||
|
||||
import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"gopkg.in/redis.v2"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("PubSub", func() {
|
||||
var client *redis.Client
|
||||
|
||||
BeforeEach(func() {
|
||||
client = redis.NewTCPClient(&redis.Options{
|
||||
Addr: redisAddr,
|
||||
})
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
Expect(client.FlushDb().Err()).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")
|
||||
Expect(pub.Err()).NotTo(HaveOccurred())
|
||||
Expect(pub.Val()).To(Equal(int64(1)))
|
||||
|
||||
Expect(pubsub.PUnsubscribe("mychannel*")).NotTo(HaveOccurred())
|
||||
|
||||
{
|
||||
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
subscr := msgi.(*redis.Subscription)
|
||||
Expect(subscr.Kind).To(Equal("psubscribe"))
|
||||
Expect(subscr.Channel).To(Equal("mychannel*"))
|
||||
Expect(subscr.Count).To(Equal(1))
|
||||
}
|
||||
|
||||
{
|
||||
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
subscr := msgi.(*redis.PMessage)
|
||||
Expect(subscr.Channel).To(Equal("mychannel1"))
|
||||
Expect(subscr.Pattern).To(Equal("mychannel*"))
|
||||
Expect(subscr.Payload).To(Equal("hello"))
|
||||
}
|
||||
|
||||
{
|
||||
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
subscr := msgi.(*redis.Subscription)
|
||||
Expect(subscr.Kind).To(Equal("punsubscribe"))
|
||||
Expect(subscr.Channel).To(Equal("mychannel*"))
|
||||
Expect(subscr.Count).To(Equal(0))
|
||||
}
|
||||
|
||||
{
|
||||
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||
Expect(err.(net.Error).Timeout()).To(Equal(true))
|
||||
Expect(msgi).NotTo(HaveOccurred())
|
||||
}
|
||||
})
|
||||
|
||||
It("should pub/sub channels", func() {
|
||||
channels, err := client.PubSubChannels("mychannel*").Result()
|
||||
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()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(channels).To(ConsistOf([]string{"mychannel", "mychannel2"}))
|
||||
|
||||
channels, err = client.PubSubChannels("").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(channels).To(BeEmpty())
|
||||
|
||||
channels, err = client.PubSubChannels("*").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(channels)).To(BeNumerically(">=", 2))
|
||||
})
|
||||
|
||||
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()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(channels).To(Equal([]interface{}{
|
||||
"mychannel", int64(1),
|
||||
"mychannel2", int64(1),
|
||||
"mychannel3", int64(0),
|
||||
}))
|
||||
})
|
||||
|
||||
It("should return the numbers of subscribers by pattern", func() {
|
||||
num, err := client.PubSubNumPat().Result()
|
||||
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()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(num).To(Equal(int64(1)))
|
||||
})
|
||||
|
||||
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")
|
||||
Expect(pub.Err()).NotTo(HaveOccurred())
|
||||
Expect(pub.Val()).To(Equal(int64(1)))
|
||||
|
||||
pub = client.Publish("mychannel2", "hello2")
|
||||
Expect(pub.Err()).NotTo(HaveOccurred())
|
||||
Expect(pub.Val()).To(Equal(int64(1)))
|
||||
|
||||
Expect(pubsub.Unsubscribe("mychannel", "mychannel2")).NotTo(HaveOccurred())
|
||||
|
||||
{
|
||||
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
subscr := msgi.(*redis.Subscription)
|
||||
Expect(subscr.Kind).To(Equal("subscribe"))
|
||||
Expect(subscr.Channel).To(Equal("mychannel"))
|
||||
Expect(subscr.Count).To(Equal(1))
|
||||
}
|
||||
|
||||
{
|
||||
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
subscr := msgi.(*redis.Subscription)
|
||||
Expect(subscr.Kind).To(Equal("subscribe"))
|
||||
Expect(subscr.Channel).To(Equal("mychannel2"))
|
||||
Expect(subscr.Count).To(Equal(2))
|
||||
}
|
||||
|
||||
{
|
||||
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
subscr := msgi.(*redis.Message)
|
||||
Expect(subscr.Channel).To(Equal("mychannel"))
|
||||
Expect(subscr.Payload).To(Equal("hello"))
|
||||
}
|
||||
|
||||
{
|
||||
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
msg := msgi.(*redis.Message)
|
||||
Expect(msg.Channel).To(Equal("mychannel2"))
|
||||
Expect(msg.Payload).To(Equal("hello2"))
|
||||
}
|
||||
|
||||
{
|
||||
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
subscr := msgi.(*redis.Subscription)
|
||||
Expect(subscr.Kind).To(Equal("unsubscribe"))
|
||||
Expect(subscr.Channel).To(Equal("mychannel"))
|
||||
Expect(subscr.Count).To(Equal(1))
|
||||
}
|
||||
|
||||
{
|
||||
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
subscr := msgi.(*redis.Subscription)
|
||||
Expect(subscr.Kind).To(Equal("unsubscribe"))
|
||||
Expect(subscr.Channel).To(Equal("mychannel2"))
|
||||
Expect(subscr.Count).To(Equal(0))
|
||||
}
|
||||
|
||||
{
|
||||
msgi, err := pubsub.ReceiveTimeout(time.Second)
|
||||
Expect(err.(net.Error).Timeout()).To(Equal(true))
|
||||
Expect(msgi).NotTo(HaveOccurred())
|
||||
}
|
||||
})
|
||||
|
||||
})
|
3305
redis_test.go
3305
redis_test.go
File diff suppressed because it is too large
Load Diff
279
sentinel_test.go
279
sentinel_test.go
|
@ -5,181 +5,118 @@ import (
|
|||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"gopkg.in/redis.v2"
|
||||
)
|
||||
|
||||
func startRedis(port string) (*exec.Cmd, error) {
|
||||
cmd := exec.Command("redis-server", "--port", port)
|
||||
if false {
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
}
|
||||
if err := cmd.Start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cmd, nil
|
||||
}
|
||||
var _ = Describe("Sentinel", func() {
|
||||
|
||||
func startRedisSlave(port, slave string) (*exec.Cmd, error) {
|
||||
cmd := exec.Command("redis-server", "--port", port, "--slaveof", "127.0.0.1", slave)
|
||||
if false {
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
}
|
||||
if err := cmd.Start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cmd, nil
|
||||
}
|
||||
const masterName = "mymaster"
|
||||
const masterPort = "8123"
|
||||
const sentinelPort = "8124"
|
||||
const sentinelConf = `
|
||||
port ` + sentinelPort + `
|
||||
|
||||
func startRedisSentinel(port, masterName, masterPort string) (*exec.Cmd, error) {
|
||||
dir, err := ioutil.TempDir("", "sentinel")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sentinelConfFilepath := filepath.Join(dir, "sentinel.conf")
|
||||
tpl, err := template.New("sentinel.conf").Parse(sentinelConf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data := struct {
|
||||
Port string
|
||||
MasterName string
|
||||
MasterPort string
|
||||
}{
|
||||
Port: port,
|
||||
MasterName: masterName,
|
||||
MasterPort: masterPort,
|
||||
}
|
||||
if err := writeTemplateToFile(sentinelConfFilepath, tpl, data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cmd := exec.Command("redis-server", sentinelConfFilepath, "--sentinel")
|
||||
if true {
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
}
|
||||
if err := cmd.Start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
func writeTemplateToFile(path string, t *template.Template, data interface{}) error {
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
return t.Execute(f, data)
|
||||
}
|
||||
|
||||
func TestSentinel(t *testing.T) {
|
||||
masterName := "mymaster"
|
||||
masterPort := "8123"
|
||||
slavePort := "8124"
|
||||
sentinelPort := "8125"
|
||||
|
||||
masterCmd, err := startRedis(masterPort)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer masterCmd.Process.Kill()
|
||||
|
||||
// Wait for master to start.
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
|
||||
master := redis.NewTCPClient(&redis.Options{
|
||||
Addr: ":" + masterPort,
|
||||
})
|
||||
if err := master.Ping().Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
slaveCmd, err := startRedisSlave(slavePort, masterPort)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer slaveCmd.Process.Kill()
|
||||
|
||||
// Wait for slave to start.
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
|
||||
slave := redis.NewTCPClient(&redis.Options{
|
||||
Addr: ":" + slavePort,
|
||||
})
|
||||
if err := slave.Ping().Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sentinelCmd, err := startRedisSentinel(sentinelPort, masterName, masterPort)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer sentinelCmd.Process.Kill()
|
||||
|
||||
// Wait for sentinel to start.
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
|
||||
sentinel := redis.NewTCPClient(&redis.Options{
|
||||
Addr: ":" + sentinelPort,
|
||||
})
|
||||
if err := sentinel.Ping().Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer sentinel.Shutdown()
|
||||
|
||||
client := redis.NewFailoverClient(&redis.FailoverOptions{
|
||||
MasterName: masterName,
|
||||
SentinelAddrs: []string{":" + sentinelPort},
|
||||
})
|
||||
|
||||
if err := client.Set("foo", "master").Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
val, err := master.Get("foo").Result()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if val != "master" {
|
||||
t.Fatalf(`got %q, expected "master"`, val)
|
||||
}
|
||||
|
||||
// Kill Redis master.
|
||||
if err := masterCmd.Process.Kill(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := master.Ping().Err(); err == nil {
|
||||
t.Fatalf("master was not killed")
|
||||
}
|
||||
|
||||
// Wait for Redis sentinel to elect new master.
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
// Check that client picked up new master.
|
||||
val, err = client.Get("foo").Result()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if val != "master" {
|
||||
t.Fatalf(`got %q, expected "master"`, val)
|
||||
}
|
||||
}
|
||||
|
||||
var sentinelConf = `
|
||||
port {{ .Port }}
|
||||
|
||||
sentinel monitor {{ .MasterName }} 127.0.0.1 {{ .MasterPort }} 1
|
||||
sentinel down-after-milliseconds {{ .MasterName }} 1000
|
||||
sentinel failover-timeout {{ .MasterName }} 2000
|
||||
sentinel parallel-syncs {{ .MasterName }} 1
|
||||
sentinel monitor ` + masterName + ` 127.0.0.1 ` + masterPort + ` 1
|
||||
sentinel down-after-milliseconds ` + masterName + ` 400
|
||||
sentinel failover-timeout ` + masterName + ` 800
|
||||
sentinel parallel-syncs ` + masterName + ` 1
|
||||
`
|
||||
|
||||
var runCmd = func(name string, args ...string) *os.Process {
|
||||
cmd := exec.Command(name, args...)
|
||||
if false {
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
}
|
||||
err := cmd.Start()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
return cmd.Process
|
||||
}
|
||||
|
||||
var connect = func(port string) *redis.Client {
|
||||
client := redis.NewTCPClient(&redis.Options{
|
||||
Addr: ":" + port,
|
||||
})
|
||||
Eventually(func() error {
|
||||
return client.Ping().Err()
|
||||
}, "1s", "100ms").ShouldNot(HaveOccurred())
|
||||
return client
|
||||
}
|
||||
|
||||
var startMaster = func() (*redis.Client, *os.Process) {
|
||||
proc := runCmd("redis-server", "--port", masterPort)
|
||||
return connect(masterPort), proc
|
||||
}
|
||||
|
||||
var startSlave = func(port string) (*redis.Client, *os.Process) {
|
||||
proc := runCmd("redis-server", "--port", port, "--slaveof", "127.0.0.1", masterPort)
|
||||
return connect(port), proc
|
||||
}
|
||||
|
||||
var startSentinel = func() *os.Process {
|
||||
dir, err := ioutil.TempDir("", "sentinel")
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
fname := filepath.Join(dir, "sentinel.conf")
|
||||
err = ioutil.WriteFile(fname, []byte(sentinelConf), 0664)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
proc := runCmd("redis-server", fname, "--sentinel")
|
||||
client := connect(sentinelPort)
|
||||
client.Close()
|
||||
return proc
|
||||
}
|
||||
|
||||
It("should facilitate failover", func() {
|
||||
master, mproc := startMaster()
|
||||
defer mproc.Kill()
|
||||
slave1, sproc1 := startSlave("8125")
|
||||
defer sproc1.Kill()
|
||||
slave2, sproc2 := startSlave("8126")
|
||||
defer sproc2.Kill()
|
||||
sntproc := startSentinel()
|
||||
defer sntproc.Kill()
|
||||
|
||||
client := redis.NewFailoverClient(&redis.FailoverOptions{
|
||||
MasterName: masterName,
|
||||
SentinelAddrs: []string{":" + sentinelPort},
|
||||
})
|
||||
defer client.Close()
|
||||
|
||||
// Set value on master, verify
|
||||
err := client.Set("foo", "master").Err()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
val, err := master.Get("foo").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(val).To(Equal("master"))
|
||||
|
||||
// Wait until replicated
|
||||
Eventually(func() string {
|
||||
return slave1.Get("foo").Val()
|
||||
}, "1s", "100ms").Should(Equal("master"))
|
||||
Eventually(func() string {
|
||||
return slave2.Get("foo").Val()
|
||||
}, "1s", "100ms").Should(Equal("master"))
|
||||
|
||||
// Kill master.
|
||||
master.Shutdown()
|
||||
Eventually(func() error {
|
||||
return master.Ping().Err()
|
||||
}, "5s", "100ms").Should(HaveOccurred())
|
||||
|
||||
// Wait for Redis sentinel to elect new master.
|
||||
Eventually(func() string {
|
||||
return slave1.Info().Val() + slave2.Info().Val()
|
||||
}, "30s", "500ms").Should(ContainSubstring("role:master"))
|
||||
|
||||
// Check that client picked up new master.
|
||||
val, err = client.Get("foo").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(val).To(Equal("master"))
|
||||
})
|
||||
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue