redis/redis_test.go

348 lines
7.8 KiB
Go
Raw Normal View History

2012-07-25 17:00:50 +04:00
package redis_test
import (
"net"
2015-01-15 20:23:22 +03:00
"os"
"os/exec"
2015-01-24 15:12:48 +03:00
"path/filepath"
2012-07-25 17:00:50 +04:00
"testing"
"time"
2014-07-02 19:04:04 +04:00
"gopkg.in/redis.v2"
2013-04-08 13:43:38 +04:00
2015-01-15 18:51:22 +03:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
2012-07-25 17:00:50 +04:00
)
const redisAddr = ":6379"
2015-01-15 18:51:22 +03:00
var _ = Describe("Client", func() {
var client *redis.Client
2012-07-25 17:00:50 +04:00
2015-01-15 18:51:22 +03:00
BeforeEach(func() {
client = redis.NewTCPClient(&redis.Options{
Addr: redisAddr,
})
2014-05-11 11:42:40 +04:00
})
2015-01-15 18:51:22 +03:00
AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
})
2015-01-15 18:51:22 +03:00
It("should ping", func() {
val, err := client.Ping().Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("PONG"))
2014-05-11 11:42:40 +04:00
})
2015-01-15 18:51:22 +03:00
It("should support custom dialers", func() {
custom := redis.NewClient(&redis.Options{
Dialer: func() (net.Conn, error) {
return net.Dial("tcp", redisAddr)
},
})
2014-09-30 12:46:56 +04:00
2015-01-15 18:51:22 +03:00
val, err := custom.Ping().Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("PONG"))
Expect(custom.Close()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
})
2012-08-25 16:35:39 +04:00
2015-01-15 18:51:22 +03:00
It("should close", func() {
Expect(client.Close()).NotTo(HaveOccurred())
err := client.Ping().Err()
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("redis: client is closed"))
2014-05-11 11:42:40 +04:00
})
2012-08-25 16:35:39 +04:00
2015-01-15 18:51:22 +03:00
It("should close pubsub without closing the connection", func() {
pubsub := client.PubSub()
Expect(pubsub.Close()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
2015-01-15 18:51:22 +03:00
_, err := pubsub.Receive()
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("redis: client is closed"))
Expect(client.Ping().Err()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
})
2015-01-15 18:51:22 +03:00
It("should close multi without closing the connection", func() {
multi := client.Multi()
Expect(multi.Close()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
2015-01-15 18:51:22 +03:00
_, err := multi.Exec(func() error {
multi.Ping()
return nil
})
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("redis: client is closed"))
Expect(client.Ping().Err()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
})
2015-01-15 18:51:22 +03:00
It("should close pipeline without closing the connection", func() {
pipeline := client.Pipeline()
Expect(pipeline.Close()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
pipeline.Ping()
2015-01-15 18:51:22 +03:00
_, err := pipeline.Exec()
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("redis: client is closed"))
Expect(client.Ping().Err()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
})
2014-07-08 12:24:19 +04:00
2015-01-15 18:51:22 +03:00
It("should support idle-timeouts", func() {
idle := redis.NewTCPClient(&redis.Options{
Addr: redisAddr,
IdleTimeout: time.Nanosecond,
})
defer idle.Close()
2014-05-11 11:42:40 +04:00
2015-01-15 18:51:22 +03:00
Expect(idle.Ping().Err()).NotTo(HaveOccurred())
time.Sleep(time.Millisecond)
Expect(idle.Ping().Err()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
})
2015-01-15 18:51:22 +03:00
It("should support DB selection", func() {
db1 := redis.NewTCPClient(&redis.Options{
Addr: redisAddr,
DB: 1,
})
defer db1.Close()
2015-01-15 18:51:22 +03:00
Expect(db1.Get("key").Err()).To(Equal(redis.Nil))
Expect(db1.Set("key", "value", 0).Err()).NotTo(HaveOccurred())
2012-08-26 13:18:42 +04:00
2015-01-15 18:51:22 +03:00
Expect(client.Get("key").Err()).To(Equal(redis.Nil))
Expect(db1.Get("key").Val()).To(Equal("value"))
Expect(db1.FlushDb().Err()).NotTo(HaveOccurred())
2014-05-11 11:42:40 +04:00
})
2013-12-30 15:45:04 +04:00
2015-01-15 18:51:22 +03:00
})
2014-05-11 11:42:40 +04:00
2012-08-17 22:36:48 +04:00
//------------------------------------------------------------------------------
2012-07-26 19:16:17 +04:00
2015-01-15 18:51:22 +03:00
func TestGinkgoSuite(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "gopkg.in/redis.v2")
2012-08-19 16:57:58 +04:00
}
2015-01-15 20:23:22 +03:00
func execCmd(name string, args ...string) (*os.Process, error) {
cmd := exec.Command(name, args...)
2015-01-24 15:12:48 +03:00
if false {
2015-01-15 20:23:22 +03:00
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
return cmd.Process, cmd.Start()
}
func connectTo(port string) (client *redis.Client, err error) {
2015-01-30 17:45:57 +03:00
client = redis.NewClient(&redis.Options{
2015-01-15 20:23:22 +03:00
Addr: ":" + port,
})
2015-01-24 15:12:48 +03:00
deadline := time.Now().Add(3 * time.Second)
2015-01-15 20:23:22 +03:00
for time.Now().Before(deadline) {
if err = client.Ping().Err(); err == nil {
2015-01-30 17:45:57 +03:00
return client, nil
2015-01-15 20:23:22 +03:00
}
2015-01-24 15:12:48 +03:00
time.Sleep(250 * time.Millisecond)
2015-01-15 20:23:22 +03:00
}
2015-01-30 17:45:57 +03:00
return nil, err
2015-01-15 20:23:22 +03:00
}
type redisProcess struct {
*os.Process
*redis.Client
}
func (p *redisProcess) Close() error {
p.Client.Close()
return p.Kill()
}
2015-01-24 15:12:48 +03:00
var (
redisServerBin, _ = filepath.Abs(filepath.Join(".test", "redis", "src", "redis-server"))
redisServerConf, _ = filepath.Abs(filepath.Join(".test", "redis.conf"))
)
func redisDir(port string) (string, error) {
dir, err := filepath.Abs(filepath.Join(".test", "instances", port))
if err != nil {
return "", err
} else if err = os.RemoveAll(dir); err != nil {
return "", err
} else if err = os.MkdirAll(dir, 0775); err != nil {
return "", err
}
return dir, nil
}
2015-01-15 20:23:22 +03:00
func startRedis(port string, args ...string) (*redisProcess, error) {
2015-01-24 15:12:48 +03:00
dir, err := redisDir(port)
if err != nil {
return nil, err
}
if err = exec.Command("cp", "-f", redisServerConf, dir).Run(); err != nil {
return nil, err
}
baseArgs := []string{filepath.Join(dir, "redis.conf"), "--port", port, "--dir", dir}
process, err := execCmd(redisServerBin, append(baseArgs, args...)...)
2015-01-15 20:23:22 +03:00
if err != nil {
return nil, err
}
2015-01-24 15:12:48 +03:00
2015-01-15 20:23:22 +03:00
client, err := connectTo(port)
if err != nil {
process.Kill()
return nil, err
}
return &redisProcess{process, client}, err
}
func startSentinel(port, masterName, masterPort string) (*redisProcess, error) {
2015-01-24 15:12:48 +03:00
dir, err := redisDir(port)
if err != nil {
return nil, err
}
process, err := execCmd(redisServerBin, os.DevNull, "--sentinel", "--port", port, "--dir", dir)
2015-01-15 20:23:22 +03:00
if err != nil {
return nil, err
}
client, err := connectTo(port)
if err != nil {
process.Kill()
return nil, err
}
for _, cmd := range []*redis.StatusCmd{
redis.NewStatusCmd("SENTINEL", "MONITOR", masterName, "127.0.0.1", masterPort, "1"),
redis.NewStatusCmd("SENTINEL", "SET", masterName, "down-after-milliseconds", "500"),
redis.NewStatusCmd("SENTINEL", "SET", masterName, "failover-timeout", "1000"),
redis.NewStatusCmd("SENTINEL", "SET", masterName, "parallel-syncs", "1"),
} {
client.Process(cmd)
if err := cmd.Err(); err != nil {
process.Kill()
return nil, err
}
}
2015-01-30 17:45:57 +03:00
return &redisProcess{process, client}, nil
2015-01-15 20:23:22 +03:00
}
2012-08-20 14:42:33 +04:00
//------------------------------------------------------------------------------
func BenchmarkRedisPing(b *testing.B) {
b.StopTimer()
client := redis.NewTCPClient(&redis.Options{
Addr: redisAddr,
})
b.StartTimer()
for i := 0; i < b.N; i++ {
if err := client.Ping().Err(); err != nil {
2013-04-08 13:43:38 +04:00
panic(err)
}
2012-07-25 17:00:50 +04:00
}
}
func BenchmarkRedisSet(b *testing.B) {
b.StopTimer()
client := redis.NewTCPClient(&redis.Options{
Addr: redisAddr,
})
b.StartTimer()
for i := 0; i < b.N; i++ {
if err := client.Set("key", "hello", 0).Err(); err != nil {
2013-04-08 13:43:38 +04:00
panic(err)
}
2012-07-25 17:00:50 +04:00
}
}
func BenchmarkRedisGetNil(b *testing.B) {
b.StopTimer()
client := redis.NewTCPClient(&redis.Options{
Addr: redisAddr,
})
if err := client.FlushDb().Err(); err != nil {
b.Fatal(err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
if err := client.Get("key").Err(); err != redis.Nil {
b.Fatal(err)
2013-04-08 13:43:38 +04:00
}
2012-07-25 17:00:50 +04:00
}
}
2012-07-27 15:43:30 +04:00
func BenchmarkRedisGet(b *testing.B) {
b.StopTimer()
client := redis.NewTCPClient(&redis.Options{
Addr: redisAddr,
})
if err := client.Set("key", "hello", 0).Err(); err != nil {
b.Fatal(err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
if err := client.Get("key").Err(); err != nil {
b.Fatal(err)
2013-04-08 13:43:38 +04:00
}
}
}
func BenchmarkRedisMGet(b *testing.B) {
b.StopTimer()
client := redis.NewTCPClient(&redis.Options{
Addr: redisAddr,
})
if err := client.MSet("key1", "hello1", "key2", "hello2").Err(); err != nil {
b.Fatal(err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
if err := client.MGet("key1", "key2").Err(); err != nil {
b.Fatal(err)
2013-04-08 13:43:38 +04:00
}
}
}
2014-07-04 16:19:45 +04:00
func BenchmarkSetExpire(b *testing.B) {
b.StopTimer()
client := redis.NewTCPClient(&redis.Options{
Addr: redisAddr,
})
b.StartTimer()
for i := 0; i < b.N; i++ {
if err := client.Set("key", "hello", 0).Err(); err != nil {
b.Fatal(err)
2014-07-04 16:19:45 +04:00
}
if err := client.Expire("key", time.Second).Err(); err != nil {
b.Fatal(err)
2014-07-04 16:19:45 +04:00
}
}
}
func BenchmarkPipeline(b *testing.B) {
b.StopTimer()
client := redis.NewTCPClient(&redis.Options{
Addr: redisAddr,
})
b.StartTimer()
for i := 0; i < b.N; i++ {
_, err := client.Pipelined(func(pipe *redis.Pipeline) error {
pipe.Set("key", "hello", 0)
2014-07-04 16:19:45 +04:00
pipe.Expire("key", time.Second)
return nil
})
if err != nil {
b.Fatal(err)
2014-07-04 16:19:45 +04:00
}
}
}