From 7f87de6109bc610169db1a46ef7f89fa15d2d877 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Fri, 30 Jan 2015 16:45:57 +0200 Subject: [PATCH] Make sentinel tests more reliable. --- redis.go | 9 ++++++++- redis_test.go | 9 +++++---- sentinel_test.go | 13 +++++++++---- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/redis.go b/redis.go index 0d15dc8..285fb99 100644 --- a/redis.go +++ b/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{ diff --git a/redis_test.go b/redis_test.go index 6053af6..fde06f7 100644 --- a/redis_test.go +++ b/redis_test.go @@ -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 } //------------------------------------------------------------------------------ diff --git a/sentinel_test.go b/sentinel_test.go index 5404eec..8e6bf86 100644 --- a/sentinel_test.go +++ b/sentinel_test.go @@ -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()) }) })