Skip to content

Commit

Permalink
use 3 sentinel instances in unit testing
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Shmulevich <dmitry.shmulevich@sysdig.com>
  • Loading branch information
Dmitry Shmulevich committed Sep 3, 2020
1 parent 6115a78 commit 7597d0d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
27 changes: 20 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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(
Expand All @@ -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())
Expand Down Expand Up @@ -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"),
Expand Down
12 changes: 9 additions & 3 deletions sentinel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion universal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down

0 comments on commit 7597d0d

Please sign in to comment.