forked from mirror/redis
Make sentinel tests more reliable.
This commit is contained in:
parent
15a90c831b
commit
7f87de6109
9
redis.go
9
redis.go
|
@ -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{
|
||||
|
|
|
@ -140,18 +140,19 @@ func execCmd(name string, args ...string) (*os.Process, error) {
|
|||
}
|
||||
|
||||
func connectTo(port string) (client *redis.Client, err error) {
|
||||
client = redis.NewTCPClient(&redis.Options{
|
||||
client = redis.NewClient(&redis.Options{
|
||||
Addr: ":" + port,
|
||||
})
|
||||
|
||||
deadline := time.Now().Add(time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
if err = client.Ping().Err(); err == nil {
|
||||
break
|
||||
return client, nil
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
return
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
type redisProcess struct {
|
||||
|
@ -199,7 +200,7 @@ func startSentinel(port, masterName, masterPort string) (*redisProcess, error) {
|
|||
return nil, err
|
||||
}
|
||||
}
|
||||
return &redisProcess{process, client}, err
|
||||
return &redisProcess{process, client}, nil
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
|
|
@ -51,6 +51,11 @@ var _ = Describe("Sentinel", func() {
|
|||
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 {
|
||||
|
@ -60,12 +65,12 @@ var _ = Describe("Sentinel", func() {
|
|||
// Wait for Redis sentinel to elect new master.
|
||||
Eventually(func() string {
|
||||
return slave1.Info().Val() + slave2.Info().Val()
|
||||
}, "30s", "500ms").Should(ContainSubstring("role:master"))
|
||||
}, "30s", "1s").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"))
|
||||
Eventually(func() error {
|
||||
return client.Get("foo").Err()
|
||||
}, "5s", "100ms").ShouldNot(HaveOccurred())
|
||||
})
|
||||
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue