2015-05-14 16:13:45 +03:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
2015-12-22 12:44:49 +03:00
|
|
|
"errors"
|
2016-12-03 18:30:13 +03:00
|
|
|
"fmt"
|
2017-08-31 15:22:47 +03:00
|
|
|
"log"
|
2015-05-14 16:13:45 +03:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2016-03-14 14:17:33 +03:00
|
|
|
"sync"
|
2015-05-14 16:13:45 +03:00
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-02-18 17:42:34 +03:00
|
|
|
"github.com/go-redis/redis"
|
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2015-05-18 14:43:08 +03:00
|
|
|
const (
|
|
|
|
redisPort = "6380"
|
|
|
|
redisAddr = ":" + redisPort
|
|
|
|
redisSecondaryPort = "6381"
|
|
|
|
)
|
2015-05-14 16:13:45 +03:00
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
const (
|
|
|
|
ringShard1Port = "6390"
|
|
|
|
ringShard2Port = "6391"
|
|
|
|
)
|
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
const (
|
|
|
|
sentinelName = "mymaster"
|
|
|
|
sentinelMasterPort = "8123"
|
|
|
|
sentinelSlave1Port = "8124"
|
|
|
|
sentinelSlave2Port = "8125"
|
|
|
|
sentinelPort = "8126"
|
|
|
|
)
|
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
var (
|
|
|
|
redisMain *redisProcess
|
|
|
|
ringShard1, ringShard2 *redisProcess
|
|
|
|
sentinelMaster, sentinelSlave1, sentinelSlave2, sentinel *redisProcess
|
|
|
|
)
|
2015-05-14 16:13:45 +03:00
|
|
|
|
|
|
|
var cluster = &clusterScenario{
|
|
|
|
ports: []string{"8220", "8221", "8222", "8223", "8224", "8225"},
|
|
|
|
nodeIds: make([]string, 6),
|
|
|
|
processes: make(map[string]*redisProcess, 6),
|
|
|
|
clients: make(map[string]*redis.Client, 6),
|
|
|
|
}
|
|
|
|
|
2017-06-29 16:53:49 +03:00
|
|
|
func init() {
|
2017-08-31 15:22:47 +03:00
|
|
|
redis.SetLogger(log.New(os.Stderr, "redis: ", log.LstdFlags|log.Lshortfile))
|
2017-06-29 16:53:49 +03:00
|
|
|
}
|
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
var _ = BeforeSuite(func() {
|
|
|
|
var err error
|
|
|
|
|
2015-05-18 14:43:08 +03:00
|
|
|
redisMain, err = startRedis(redisPort)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
ringShard1, err = startRedis(ringShard1Port)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
ringShard2, err = startRedis(ringShard2Port)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
sentinelMaster, err = startRedis(sentinelMasterPort)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
sentinel, err = startSentinel(sentinelPort, sentinelName, sentinelMasterPort)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
sentinelSlave1, err = startRedis(
|
|
|
|
sentinelSlave1Port, "--slaveof", "127.0.0.1", sentinelMasterPort)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
sentinelSlave2, err = startRedis(
|
|
|
|
sentinelSlave2Port, "--slaveof", "127.0.0.1", sentinelMasterPort)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
Expect(startCluster(cluster)).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
var _ = AfterSuite(func() {
|
2015-05-18 14:43:08 +03:00
|
|
|
Expect(redisMain.Close()).NotTo(HaveOccurred())
|
2015-06-03 16:45:46 +03:00
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
Expect(ringShard1.Close()).NotTo(HaveOccurred())
|
|
|
|
Expect(ringShard2.Close()).NotTo(HaveOccurred())
|
2015-05-18 14:43:08 +03:00
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
Expect(sentinel.Close()).NotTo(HaveOccurred())
|
|
|
|
Expect(sentinelSlave1.Close()).NotTo(HaveOccurred())
|
|
|
|
Expect(sentinelSlave2.Close()).NotTo(HaveOccurred())
|
|
|
|
Expect(sentinelMaster.Close()).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
Expect(stopCluster(cluster)).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
func TestGinkgoSuite(t *testing.T) {
|
|
|
|
RegisterFailHandler(Fail)
|
2017-02-18 17:42:34 +03:00
|
|
|
RunSpecs(t, "go-redis")
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
func redisOptions() *redis.Options {
|
|
|
|
return &redis.Options{
|
2016-03-17 19:00:47 +03:00
|
|
|
Addr: redisAddr,
|
|
|
|
DB: 15,
|
|
|
|
DialTimeout: 10 * time.Second,
|
|
|
|
ReadTimeout: 30 * time.Second,
|
|
|
|
WriteTimeout: 30 * time.Second,
|
|
|
|
PoolSize: 10,
|
|
|
|
PoolTimeout: 30 * time.Second,
|
2016-10-02 15:44:01 +03:00
|
|
|
IdleTimeout: 500 * time.Millisecond,
|
|
|
|
IdleCheckFrequency: 500 * time.Millisecond,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func redisClusterOptions() *redis.ClusterOptions {
|
|
|
|
return &redis.ClusterOptions{
|
|
|
|
DialTimeout: 10 * time.Second,
|
|
|
|
ReadTimeout: 30 * time.Second,
|
|
|
|
WriteTimeout: 30 * time.Second,
|
|
|
|
PoolSize: 10,
|
|
|
|
PoolTimeout: 30 * time.Second,
|
|
|
|
IdleTimeout: 500 * time.Millisecond,
|
|
|
|
IdleCheckFrequency: 500 * time.Millisecond,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func redisRingOptions() *redis.RingOptions {
|
|
|
|
return &redis.RingOptions{
|
|
|
|
Addrs: map[string]string{
|
|
|
|
"ringShardOne": ":" + ringShard1Port,
|
|
|
|
"ringShardTwo": ":" + ringShard2Port,
|
|
|
|
},
|
|
|
|
DialTimeout: 10 * time.Second,
|
|
|
|
ReadTimeout: 30 * time.Second,
|
|
|
|
WriteTimeout: 30 * time.Second,
|
|
|
|
PoolSize: 10,
|
|
|
|
PoolTimeout: 30 * time.Second,
|
|
|
|
IdleTimeout: 500 * time.Millisecond,
|
|
|
|
IdleCheckFrequency: 500 * time.Millisecond,
|
2016-03-16 17:57:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
func perform(n int, cbs ...func(int)) {
|
2016-03-14 14:17:33 +03:00
|
|
|
var wg sync.WaitGroup
|
2016-03-17 19:00:47 +03:00
|
|
|
for _, cb := range cbs {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(cb func(int), i int) {
|
|
|
|
defer GinkgoRecover()
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
cb(i)
|
|
|
|
}(cb, i)
|
|
|
|
}
|
2016-03-14 14:17:33 +03:00
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2016-03-09 16:14:01 +03:00
|
|
|
func eventually(fn func() error, timeout time.Duration) error {
|
2015-05-14 16:13:45 +03:00
|
|
|
var exit int32
|
2016-12-03 18:30:13 +03:00
|
|
|
errCh := make(chan error)
|
2016-03-16 17:57:24 +03:00
|
|
|
done := make(chan struct{})
|
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
go func() {
|
2016-07-02 15:52:10 +03:00
|
|
|
defer GinkgoRecover()
|
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
for atomic.LoadInt32(&exit) == 0 {
|
2016-03-16 17:57:24 +03:00
|
|
|
err := fn()
|
2015-11-14 16:54:16 +03:00
|
|
|
if err == nil {
|
|
|
|
close(done)
|
2015-05-14 16:13:45 +03:00
|
|
|
return
|
|
|
|
}
|
2016-12-03 18:30:13 +03:00
|
|
|
select {
|
|
|
|
case errCh <- err:
|
|
|
|
default:
|
|
|
|
}
|
2015-05-14 16:13:45 +03:00
|
|
|
time.Sleep(timeout / 100)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2015-11-14 16:54:16 +03:00
|
|
|
case <-done:
|
2015-05-14 16:13:45 +03:00
|
|
|
return nil
|
|
|
|
case <-time.After(timeout):
|
|
|
|
atomic.StoreInt32(&exit, 1)
|
2016-12-03 18:30:13 +03:00
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("timeout after %s", timeout)
|
|
|
|
}
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func execCmd(name string, args ...string) (*os.Process, error) {
|
|
|
|
cmd := exec.Command(name, args...)
|
|
|
|
if testing.Verbose() {
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
}
|
|
|
|
return cmd.Process, cmd.Start()
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:44:49 +03:00
|
|
|
func connectTo(port string) (*redis.Client, error) {
|
|
|
|
client := redis.NewClient(&redis.Options{
|
2015-05-14 16:13:45 +03:00
|
|
|
Addr: ":" + port,
|
|
|
|
})
|
|
|
|
|
2015-12-22 12:44:49 +03:00
|
|
|
err := eventually(func() error {
|
|
|
|
return client.Ping().Err()
|
2016-03-14 14:17:33 +03:00
|
|
|
}, 30*time.Second)
|
2015-12-22 12:44:49 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:44:49 +03:00
|
|
|
return client, nil
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type redisProcess struct {
|
|
|
|
*os.Process
|
|
|
|
*redis.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *redisProcess) Close() error {
|
2015-12-22 12:44:49 +03:00
|
|
|
if err := p.Kill(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err := eventually(func() error {
|
|
|
|
if err := p.Client.Ping().Err(); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("client is not shutdown")
|
|
|
|
}, 10*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
p.Client.Close()
|
2015-12-22 12:44:49 +03:00
|
|
|
return nil
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2016-03-12 13:25:59 +03:00
|
|
|
redisServerBin, _ = filepath.Abs(filepath.Join("testdata", "redis", "src", "redis-server"))
|
|
|
|
redisServerConf, _ = filepath.Abs(filepath.Join("testdata", "redis.conf"))
|
2015-05-14 16:13:45 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func redisDir(port string) (string, error) {
|
2016-03-12 13:25:59 +03:00
|
|
|
dir, err := filepath.Abs(filepath.Join("testdata", "instances", port))
|
2015-05-14 16:13:45 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2015-12-22 12:44:49 +03:00
|
|
|
}
|
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
2015-05-14 16:13:45 +03:00
|
|
|
return "", err
|
2015-12-22 12:44:49 +03:00
|
|
|
}
|
|
|
|
if err := os.MkdirAll(dir, 0775); err != nil {
|
2015-05-14 16:13:45 +03:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func startRedis(port string, args ...string) (*redisProcess, error) {
|
|
|
|
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...)...)
|
|
|
|
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) {
|
|
|
|
dir, err := redisDir(port)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
process, err := execCmd(redisServerBin, os.DevNull, "--sentinel", "--port", port, "--dir", dir)
|
|
|
|
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}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2015-10-13 12:02:29 +03:00
|
|
|
type badConnError string
|
|
|
|
|
|
|
|
func (e badConnError) Error() string { return string(e) }
|
|
|
|
func (e badConnError) Timeout() bool { return false }
|
|
|
|
func (e badConnError) Temporary() bool { return false }
|
2015-05-14 16:13:45 +03:00
|
|
|
|
2015-09-06 13:50:16 +03:00
|
|
|
type badConn struct {
|
|
|
|
net.TCPConn
|
2015-05-14 16:13:45 +03:00
|
|
|
|
2015-09-06 13:50:16 +03:00
|
|
|
readDelay, writeDelay time.Duration
|
|
|
|
readErr, writeErr error
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
|
2015-09-06 13:50:16 +03:00
|
|
|
var _ net.Conn = &badConn{}
|
|
|
|
|
|
|
|
func (cn *badConn) Read([]byte) (int, error) {
|
|
|
|
if cn.readDelay != 0 {
|
|
|
|
time.Sleep(cn.readDelay)
|
|
|
|
}
|
|
|
|
if cn.readErr != nil {
|
|
|
|
return 0, cn.readErr
|
|
|
|
}
|
2015-10-13 12:02:29 +03:00
|
|
|
return 0, badConnError("bad connection")
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
|
2015-09-06 13:50:16 +03:00
|
|
|
func (cn *badConn) Write([]byte) (int, error) {
|
|
|
|
if cn.writeDelay != 0 {
|
|
|
|
time.Sleep(cn.writeDelay)
|
|
|
|
}
|
|
|
|
if cn.writeErr != nil {
|
|
|
|
return 0, cn.writeErr
|
|
|
|
}
|
2015-10-13 12:02:29 +03:00
|
|
|
return 0, badConnError("bad connection")
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|