Merge pull request #64 from go-redis/feature/ginkgo-suite

Use ginkgo test suite
This commit is contained in:
Vladimir Mihailenco 2015-01-30 17:06:49 +02:00
commit 2a81661b34
15 changed files with 3231 additions and 3378 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.rdb

View File

@ -4,16 +4,15 @@ services:
- redis-server
go:
- 1.1
- 1.2
- 1.3
- 1.4
- tip
install:
- go get gopkg.in/bufio.v1
- go get gopkg.in/check.v1
- go get github.com/onsi/ginkgo
- go get github.com/onsi/gomega
- mkdir -p $HOME/gopath/src/gopkg.in
- ln -s `pwd` $HOME/gopath/src/gopkg.in/redis.v2
before_script:
- redis-server testdata/sentinel.conf --sentinel &
- mv $HOME/gopath/src/github.com/go-redis/redis $HOME/gopath/src/gopkg.in/redis.v2
- cd $HOME/gopath/src/gopkg.in/redis.v2

View File

@ -1,3 +1,4 @@
all:
go test gopkg.in/redis.v2 -cpu=1,2,4
go test gopkg.in/redis.v2 -short -race
go test ./...
go test ./... -cpu=2
go test ./... -short -race

167
command_test.go Normal file
View File

@ -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)))
})
})
})

2215
commands_test.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -29,14 +29,10 @@ func ExampleNewTCPClient() {
}
func ExampleNewFailoverClient() {
client := redis.NewFailoverClient(&redis.FailoverOptions{
redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: "master",
SentinelAddrs: []string{":26379"},
})
pong, err := client.Ping().Result()
fmt.Println(pong, err)
// Output: PONG <nil>
}
func ExampleClient() {

122
multi_test.go Normal file
View File

@ -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"))
})
})

152
pipeline_test.go Normal file
View File

@ -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()
})
})

124
pool_test.go Normal file
View File

@ -0,0 +1,124 @@
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"))
})
pool := client.Pool()
Expect(pool.Size()).To(BeNumerically("<=", 10))
Expect(pool.Len()).To(BeNumerically("<=", 10))
Expect(pool.Size()).To(Equal(pool.Len()))
})
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))
})
})

202
pubsub_test.go Normal file
View File

@ -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())
}
})
})

View File

@ -1,31 +1,25 @@
package redis
import (
"sync"
"testing"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestRateLimiter(t *testing.T) {
var _ = Describe("RateLimiter", func() {
var n = 100000
if testing.Short() {
n = 1000
}
It("should rate limit", func() {
rl := newRateLimiter(time.Minute, n)
for i := 0; i <= n; i++ {
Expect(rl.Check()).To(BeTrue())
}
Expect(rl.Check()).To(BeFalse())
})
wg := &sync.WaitGroup{}
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
if !rl.Check() {
panic("check failed")
}
wg.Done()
}()
}
wg.Wait()
if rl.Check() && rl.Check() {
t.Fatal("check passed")
}
}
})

View File

@ -170,6 +170,13 @@ type Options struct {
IdleTimeout time.Duration
}
func (opt *Options) getNetwork() string {
if opt.Network == "" {
return "tcp"
}
return opt.Network
}
func (opt *Options) getPoolSize() int {
if opt.PoolSize == 0 {
return 10
@ -207,7 +214,7 @@ func NewClient(clOpt *Options) *Client {
dialer := clOpt.Dialer
if dialer == nil {
dialer = func() (net.Conn, error) {
return net.DialTimeout(clOpt.Network, clOpt.Addr, opt.DialTimeout)
return net.DialTimeout(clOpt.getNetwork(), clOpt.Addr, opt.DialTimeout)
}
}
return &Client{

File diff suppressed because it is too large Load Diff

View File

@ -1,185 +1,76 @@
package redis_test
import (
"io/ioutil"
"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"
func startRedisSentinel(port, masterName, masterPort string) (*exec.Cmd, error) {
dir, err := ioutil.TempDir("", "sentinel")
if err != nil {
return nil, err
}
It("should facilitate failover", func() {
master, err := startRedis(masterPort)
Expect(err).NotTo(HaveOccurred())
defer master.Close()
sentinelConfFilepath := filepath.Join(dir, "sentinel.conf")
tpl, err := template.New("sentinel.conf").Parse(sentinelConf)
if err != nil {
return nil, err
}
sentinel, err := startSentinel(sentinelPort, masterName, masterPort)
Expect(err).NotTo(HaveOccurred())
defer sentinel.Close()
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
}
slave1, err := startRedis("8125", "--slaveof", "127.0.0.1", masterPort)
Expect(err).NotTo(HaveOccurred())
defer slave1.Close()
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()
slave2, err := startRedis("8126", "--slaveof", "127.0.0.1", masterPort)
Expect(err).NotTo(HaveOccurred())
defer slave2.Close()
client := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: masterName,
SentinelAddrs: []string{":" + sentinelPort},
})
defer client.Close()
if err := client.Set("foo", "master").Err(); err != nil {
t.Fatal(err)
}
// Set value on master, verify
err = client.Set("foo", "master").Err()
Expect(err).NotTo(HaveOccurred())
val, err := master.Get("foo").Result()
if err != nil {
t.Fatal(err)
}
if val != "master" {
t.Fatalf(`got %q, expected "master"`, val)
}
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("master"))
// 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 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"))
// Wait until slaves are picked up by sentinel.
Eventually(func() string {
return sentinel.Info().Val()
}, "10s", "100ms").Should(ContainSubstring("slaves=2"))
// Kill master.
master.Shutdown()
Eventually(func() error {
return master.Ping().Err()
}, "5s", "100ms").Should(HaveOccurred())
// Wait for Redis sentinel to elect new master.
time.Sleep(5 * time.Second)
Eventually(func() string {
return slave1.Info().Val() + slave2.Info().Val()
}, "30s", "1s").Should(ContainSubstring("role:master"))
// 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)
}
}
Eventually(func() error {
return client.Get("foo").Err()
}, "5s", "100ms").ShouldNot(HaveOccurred())
})
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
`
})

View File

@ -1,6 +0,0 @@
port 26379
sentinel monitor master 127.0.0.1 6379 1
sentinel down-after-milliseconds master 2000
sentinel failover-timeout master 5000
sentinel parallel-syncs master 4