Make sentinel tests more reliable.

This commit is contained in:
Vladimir Mihailenco 2015-01-30 16:45:57 +02:00
parent 15a90c831b
commit 7f87de6109
3 changed files with 22 additions and 9 deletions

View File

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

View File

@ -140,18 +140,19 @@ func execCmd(name string, args ...string) (*os.Process, error) {
} }
func connectTo(port string) (client *redis.Client, err error) { func connectTo(port string) (client *redis.Client, err error) {
client = redis.NewTCPClient(&redis.Options{ client = redis.NewClient(&redis.Options{
Addr: ":" + port, Addr: ":" + port,
}) })
deadline := time.Now().Add(time.Second) deadline := time.Now().Add(time.Second)
for time.Now().Before(deadline) { for time.Now().Before(deadline) {
if err = client.Ping().Err(); err == nil { if err = client.Ping().Err(); err == nil {
break return client, nil
} }
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
} }
return
return nil, err
} }
type redisProcess struct { type redisProcess struct {
@ -199,7 +200,7 @@ func startSentinel(port, masterName, masterPort string) (*redisProcess, error) {
return nil, err return nil, err
} }
} }
return &redisProcess{process, client}, err return &redisProcess{process, client}, nil
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@ -51,6 +51,11 @@ var _ = Describe("Sentinel", func() {
return slave2.Get("foo").Val() return slave2.Get("foo").Val()
}, "1s", "100ms").Should(Equal("master")) }, "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. // Kill master.
master.Shutdown() master.Shutdown()
Eventually(func() error { Eventually(func() error {
@ -60,12 +65,12 @@ var _ = Describe("Sentinel", func() {
// Wait for Redis sentinel to elect new master. // Wait for Redis sentinel to elect new master.
Eventually(func() string { Eventually(func() string {
return slave1.Info().Val() + slave2.Info().Val() 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. // Check that client picked up new master.
val, err = client.Get("foo").Result() Eventually(func() error {
Expect(err).NotTo(HaveOccurred()) return client.Get("foo").Err()
Expect(val).To(Equal("master")) }, "5s", "100ms").ShouldNot(HaveOccurred())
}) })
}) })