redis/redis_test.go

321 lines
7.0 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"
2012-08-17 22:36:48 +04:00
"sort"
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").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 18:51:22 +03:00
func sortStrings(slice []string) []string {
sort.Strings(slice)
return slice
2014-05-11 11:42:40 +04:00
}
2015-01-15 20:23:22 +03:00
func execCmd(name string, args ...string) (*os.Process, error) {
cmd := exec.Command(name, args...)
if false {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
return cmd.Process, cmd.Start()
}
func connectTo(port string) (client *redis.Client, err error) {
client = redis.NewTCPClient(&redis.Options{
Addr: ":" + port,
})
deadline := time.Now().Add(time.Second)
for time.Now().Before(deadline) {
if err = client.Ping().Err(); err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
return
}
type redisProcess struct {
*os.Process
*redis.Client
}
func (p *redisProcess) Close() error {
p.Client.Close()
return p.Kill()
}
func startRedis(port string, args ...string) (*redisProcess, error) {
process, err := execCmd("redis-server", append([]string{"--port", port}, args...)...)
if err != nil {
return nil, err
}
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) {
process, err := execCmd("redis-server", os.DevNull, "--sentinel", "--port", port)
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
}
}
return &redisProcess{process, client}, err
}
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").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").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").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 {
2014-07-04 16:19:45 +04:00
pipe.Set("key", "hello")
pipe.Expire("key", time.Second)
return nil
})
if err != nil {
b.Fatal(err)
2014-07-04 16:19:45 +04:00
}
}
}