From 7597d0def79b3df8c0655f747ec8bddf2fccd296 Mon Sep 17 00:00:00 2001 From: Dmitry Shmulevich Date: Thu, 3 Sep 2020 12:11:56 -0700 Subject: [PATCH] use 3 sentinel instances in unit testing Signed-off-by: Dmitry Shmulevich --- main_test.go | 27 ++++++++++++++++++++------- sentinel_test.go | 12 +++++++++--- universal_test.go | 2 +- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/main_test.go b/main_test.go index 8a1dc465..a9026d9d 100644 --- a/main_test.go +++ b/main_test.go @@ -35,13 +35,18 @@ const ( sentinelMasterPort = "9123" sentinelSlave1Port = "9124" sentinelSlave2Port = "9125" - sentinelPort = "9126" + sentinelPort1 = "9126" + sentinelPort2 = "9127" + sentinelPort3 = "9128" ) var ( - redisMain *redisProcess - ringShard1, ringShard2, ringShard3 *redisProcess - sentinelMaster, sentinelSlave1, sentinelSlave2, sentinel *redisProcess + redisMain *redisProcess + ringShard1, ringShard2, ringShard3 *redisProcess + sentinelMaster, sentinelSlave1, sentinelSlave2 *redisProcess + sentinel1, sentinel2, sentinel3 *redisProcess + + sentinelAddrs = []string{":" + sentinelPort1, ":" + sentinelPort2, ":" + sentinelPort3} ) var cluster = &clusterScenario{ @@ -69,7 +74,13 @@ var _ = BeforeSuite(func() { sentinelMaster, err = startRedis(sentinelMasterPort) Expect(err).NotTo(HaveOccurred()) - sentinel, err = startSentinel(sentinelPort, sentinelName, sentinelMasterPort) + sentinel1, err = startSentinel(sentinelPort1, sentinelName, sentinelMasterPort) + Expect(err).NotTo(HaveOccurred()) + + sentinel2, err = startSentinel(sentinelPort2, sentinelName, sentinelMasterPort) + Expect(err).NotTo(HaveOccurred()) + + sentinel3, err = startSentinel(sentinelPort3, sentinelName, sentinelMasterPort) Expect(err).NotTo(HaveOccurred()) sentinelSlave1, err = startRedis( @@ -90,7 +101,9 @@ var _ = AfterSuite(func() { Expect(ringShard2.Close()).NotTo(HaveOccurred()) Expect(ringShard3.Close()).NotTo(HaveOccurred()) - Expect(sentinel.Close()).NotTo(HaveOccurred()) + Expect(sentinel1.Close()).NotTo(HaveOccurred()) + Expect(sentinel2.Close()).NotTo(HaveOccurred()) + Expect(sentinel3.Close()).NotTo(HaveOccurred()) Expect(sentinelSlave1.Close()).NotTo(HaveOccurred()) Expect(sentinelSlave2.Close()).NotTo(HaveOccurred()) Expect(sentinelMaster.Close()).NotTo(HaveOccurred()) @@ -313,7 +326,7 @@ func startSentinel(port, masterName, masterPort string) (*redisProcess, error) { return nil, err } for _, cmd := range []*redis.StatusCmd{ - redis.NewStatusCmd(ctx, "SENTINEL", "MONITOR", masterName, "127.0.0.1", masterPort, "1"), + redis.NewStatusCmd(ctx, "SENTINEL", "MONITOR", masterName, "127.0.0.1", masterPort, "2"), redis.NewStatusCmd(ctx, "SENTINEL", "SET", masterName, "down-after-milliseconds", "500"), redis.NewStatusCmd(ctx, "SENTINEL", "SET", masterName, "failover-timeout", "1000"), redis.NewStatusCmd(ctx, "SENTINEL", "SET", masterName, "parallel-syncs", "1"), diff --git a/sentinel_test.go b/sentinel_test.go index 3ac887c2..80395c9e 100644 --- a/sentinel_test.go +++ b/sentinel_test.go @@ -13,7 +13,7 @@ var _ = Describe("Sentinel", func() { BeforeEach(func() { client = redis.NewFailoverClient(&redis.FailoverOptions{ MasterName: sentinelName, - SentinelAddrs: []string{":" + sentinelPort}, + SentinelAddrs: sentinelAddrs, }) Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred()) }) @@ -45,7 +45,13 @@ var _ = Describe("Sentinel", func() { // Wait until slaves are picked up by sentinel. Eventually(func() string { - return sentinel.Info(ctx).Val() + return sentinel1.Info(ctx).Val() + }, "10s", "100ms").Should(ContainSubstring("slaves=2")) + Eventually(func() string { + return sentinel2.Info(ctx).Val() + }, "10s", "100ms").Should(ContainSubstring("slaves=2")) + Eventually(func() string { + return sentinel3.Info(ctx).Val() }, "10s", "100ms").Should(ContainSubstring("slaves=2")) // Kill master. @@ -79,7 +85,7 @@ var _ = Describe("Sentinel", func() { client = redis.NewFailoverClient(&redis.FailoverOptions{ MasterName: sentinelName, - SentinelAddrs: []string{":" + sentinelPort}, + SentinelAddrs: sentinelAddrs, DB: 1, }) err := client.Ping(ctx).Err() diff --git a/universal_test.go b/universal_test.go index d92ca04f..7491a1d1 100644 --- a/universal_test.go +++ b/universal_test.go @@ -19,7 +19,7 @@ var _ = Describe("UniversalClient", func() { It("should connect to failover servers", func() { client = redis.NewUniversalClient(&redis.UniversalOptions{ MasterName: sentinelName, - Addrs: []string{":" + sentinelPort}, + Addrs: sentinelAddrs, }) Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred()) })