redis/sentinel_test.go

209 lines
5.8 KiB
Go
Raw Normal View History

2014-07-02 19:04:38 +04:00
package redis_test
import (
2020-09-09 17:39:13 +03:00
"net"
2020-03-11 17:29:16 +03:00
"github.com/go-redis/redis/v8"
2017-02-18 17:42:34 +03:00
2015-01-15 18:51:22 +03:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
2014-07-02 19:04:38 +04:00
)
2015-01-15 18:51:22 +03:00
var _ = Describe("Sentinel", func() {
var client *redis.Client
2020-09-09 17:39:13 +03:00
var master *redis.Client
var masterPort string
2014-11-13 15:26:14 +03:00
BeforeEach(func() {
client = redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: sentinelName,
SentinelAddrs: sentinelAddrs,
2015-01-15 18:51:22 +03:00
})
2020-03-11 17:26:42 +03:00
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
2020-09-09 17:39:13 +03:00
sentinel := redis.NewSentinelClient(&redis.Options{
Addr: ":" + sentinelPort1,
})
addr, err := sentinel.GetMasterAddrByName(ctx, sentinelName).Result()
Expect(err).NotTo(HaveOccurred())
master = redis.NewClient(&redis.Options{
Addr: net.JoinHostPort(addr[0], addr[1]),
})
masterPort = addr[1]
})
AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
2020-09-09 17:39:13 +03:00
Expect(master.Close()).NotTo(HaveOccurred())
})
2015-01-15 18:51:22 +03:00
It("should facilitate failover", func() {
// Set value on master.
2020-03-11 17:26:42 +03:00
err := client.Set(ctx, "foo", "master", 0).Err()
2015-01-15 18:51:22 +03:00
Expect(err).NotTo(HaveOccurred())
// Verify.
val, err := client.Get(ctx, "foo").Result()
2015-01-15 18:51:22 +03:00
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("master"))
// Create subscription.
2020-03-11 17:26:42 +03:00
ch := client.Subscribe(ctx, "foo").Channel()
// Wait until replicated.
2015-01-15 18:51:22 +03:00
Eventually(func() string {
2020-03-11 17:26:42 +03:00
return sentinelSlave1.Get(ctx, "foo").Val()
2020-08-15 16:23:14 +03:00
}, "15s", "100ms").Should(Equal("master"))
2015-01-15 18:51:22 +03:00
Eventually(func() string {
2020-03-11 17:26:42 +03:00
return sentinelSlave2.Get(ctx, "foo").Val()
2020-08-15 16:23:14 +03:00
}, "15s", "100ms").Should(Equal("master"))
2015-01-15 18:51:22 +03:00
2015-01-30 17:45:57 +03:00
// Wait until slaves are picked up by sentinel.
Eventually(func() string {
return sentinel1.Info(ctx).Val()
2020-09-09 15:27:17 +03:00
}, "15s", "100ms").Should(ContainSubstring("slaves=2"))
Eventually(func() string {
return sentinel2.Info(ctx).Val()
2020-09-09 15:27:17 +03:00
}, "15s", "100ms").Should(ContainSubstring("slaves=2"))
Eventually(func() string {
return sentinel3.Info(ctx).Val()
2020-09-09 15:27:17 +03:00
}, "15s", "100ms").Should(ContainSubstring("slaves=2"))
2015-01-30 17:45:57 +03:00
2015-01-15 18:51:22 +03:00
// Kill master.
2020-09-09 17:39:13 +03:00
err = master.Shutdown(ctx).Err()
Expect(err).NotTo(HaveOccurred())
2015-01-15 18:51:22 +03:00
Eventually(func() error {
2020-09-09 17:39:13 +03:00
return master.Ping(ctx).Err()
2020-08-15 16:23:14 +03:00
}, "15s", "100ms").Should(HaveOccurred())
2015-01-15 18:51:22 +03:00
// Wait for Redis sentinel to elect new master.
Eventually(func() string {
2020-03-11 17:26:42 +03:00
return sentinelSlave1.Info(ctx).Val() + sentinelSlave2.Info(ctx).Val()
2020-09-09 15:27:17 +03:00
}, "15s", "100ms").Should(ContainSubstring("role:master"))
2015-01-15 18:51:22 +03:00
// Check that client picked up new master.
2015-01-30 17:45:57 +03:00
Eventually(func() error {
2020-03-11 17:26:42 +03:00
return client.Get(ctx, "foo").Err()
2020-08-15 16:23:14 +03:00
}, "15s", "100ms").ShouldNot(HaveOccurred())
2020-09-05 10:56:09 +03:00
// Check if subscription is renewed.
var msg *redis.Message
2020-09-05 10:56:09 +03:00
Eventually(func() <-chan *redis.Message {
_ = client.Publish(ctx, "foo", "hello").Err()
return ch
2020-09-09 15:27:17 +03:00
}, "15s", "100ms").Should(Receive(&msg))
Expect(msg.Channel).To(Equal("foo"))
Expect(msg.Payload).To(Equal("hello"))
2020-09-09 17:39:13 +03:00
_, err = startRedis(masterPort)
Expect(err).NotTo(HaveOccurred())
2014-07-02 19:04:38 +04:00
})
It("supports DB selection", func() {
Expect(client.Close()).NotTo(HaveOccurred())
client = redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: sentinelName,
SentinelAddrs: sentinelAddrs,
DB: 1,
})
2020-03-11 17:26:42 +03:00
err := client.Ping(ctx).Err()
Expect(err).NotTo(HaveOccurred())
})
2015-01-15 18:51:22 +03:00
})
2020-09-09 15:27:17 +03:00
var _ = Describe("NewFailoverClusterClient", func() {
var client *redis.ClusterClient
2020-09-09 17:39:13 +03:00
var master *redis.Client
var masterPort string
2020-09-09 15:27:17 +03:00
BeforeEach(func() {
client = redis.NewFailoverClusterClient(&redis.FailoverOptions{
MasterName: sentinelName,
SentinelAddrs: sentinelAddrs,
})
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
2020-09-09 17:39:13 +03:00
sentinel := redis.NewSentinelClient(&redis.Options{
Addr: ":" + sentinelPort1,
})
addr, err := sentinel.GetMasterAddrByName(ctx, sentinelName).Result()
Expect(err).NotTo(HaveOccurred())
master = redis.NewClient(&redis.Options{
Addr: net.JoinHostPort(addr[0], addr[1]),
})
masterPort = addr[1]
2020-09-09 15:27:17 +03:00
})
AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
2020-09-09 17:39:13 +03:00
Expect(master.Close()).NotTo(HaveOccurred())
2020-09-09 15:27:17 +03:00
})
It("should facilitate failover", func() {
2020-09-09 17:39:13 +03:00
// Set value.
2020-09-09 15:27:17 +03:00
err := client.Set(ctx, "foo", "master", 0).Err()
Expect(err).NotTo(HaveOccurred())
// Verify.
val, err := client.Get(ctx, "foo").Result()
2020-09-09 15:27:17 +03:00
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("master"))
// Create subscription.
ch := client.Subscribe(ctx, "foo").Channel()
// Wait until replicated.
Eventually(func() string {
return sentinelSlave1.Get(ctx, "foo").Val()
}, "15s", "100ms").Should(Equal("master"))
Eventually(func() string {
return sentinelSlave2.Get(ctx, "foo").Val()
}, "15s", "100ms").Should(Equal("master"))
// Wait until slaves are picked up by sentinel.
Eventually(func() string {
return sentinel1.Info(ctx).Val()
}, "15s", "100ms").Should(ContainSubstring("slaves=2"))
Eventually(func() string {
return sentinel2.Info(ctx).Val()
}, "15s", "100ms").Should(ContainSubstring("slaves=2"))
Eventually(func() string {
return sentinel3.Info(ctx).Val()
}, "15s", "100ms").Should(ContainSubstring("slaves=2"))
// Kill master.
2020-09-09 17:39:13 +03:00
err = master.Shutdown(ctx).Err()
Expect(err).NotTo(HaveOccurred())
2020-09-09 15:27:17 +03:00
Eventually(func() error {
return sentinelMaster.Ping(ctx).Err()
}, "15s", "100ms").Should(HaveOccurred())
// Wait for Redis sentinel to elect new master.
Eventually(func() string {
return sentinelSlave1.Info(ctx).Val() + sentinelSlave2.Info(ctx).Val()
}, "15s", "100ms").Should(ContainSubstring("role:master"))
// Check that client picked up new master.
Eventually(func() error {
return client.Get(ctx, "foo").Err()
}, "15s", "100ms").ShouldNot(HaveOccurred())
// Check if subscription is renewed.
var msg *redis.Message
Eventually(func() <-chan *redis.Message {
_ = client.Publish(ctx, "foo", "hello").Err()
return ch
}, "15s", "100ms").Should(Receive(&msg))
Expect(msg.Channel).To(Equal("foo"))
Expect(msg.Payload).To(Equal("hello"))
2020-09-09 17:39:13 +03:00
_, err = startRedis(masterPort)
Expect(err).NotTo(HaveOccurred())
2020-09-09 15:27:17 +03:00
})
})