2014-07-02 19:04:38 +04:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
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() {
|
2015-05-14 16:13:45 +03:00
|
|
|
var client *redis.Client
|
2014-11-13 15:26:14 +03:00
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
BeforeEach(func() {
|
|
|
|
client = redis.NewFailoverClient(&redis.FailoverOptions{
|
|
|
|
MasterName: sentinelName,
|
2015-01-15 18:51:22 +03:00
|
|
|
SentinelAddrs: []string{":" + sentinelPort},
|
|
|
|
})
|
2020-03-11 17:26:42 +03:00
|
|
|
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
|
2015-05-14 16:13:45 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
Expect(client.Close()).NotTo(HaveOccurred())
|
|
|
|
})
|
2015-01-15 18:51:22 +03:00
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
It("should facilitate failover", func() {
|
2017-06-29 16:53:49 +03:00
|
|
|
// 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())
|
|
|
|
|
2017-06-29 16:53:49 +03:00
|
|
|
// Verify.
|
2020-03-11 17:26:42 +03:00
|
|
|
val, err := sentinelMaster.Get(ctx, "foo").Result()
|
2015-01-15 18:51:22 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(val).To(Equal("master"))
|
|
|
|
|
2017-06-29 16:53:49 +03:00
|
|
|
// Create subscription.
|
2020-03-11 17:26:42 +03:00
|
|
|
ch := client.Subscribe(ctx, "foo").Channel()
|
2017-06-29 16:53:49 +03:00
|
|
|
|
|
|
|
// 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 {
|
2020-03-11 17:26:42 +03:00
|
|
|
return sentinel.Info(ctx).Val()
|
2015-01-30 17:45:57 +03:00
|
|
|
}, "10s", "100ms").Should(ContainSubstring("slaves=2"))
|
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
// Kill master.
|
2020-03-11 17:26:42 +03:00
|
|
|
sentinelMaster.Shutdown(ctx)
|
2015-01-15 18:51:22 +03:00
|
|
|
Eventually(func() error {
|
2020-03-11 17:26:42 +03:00
|
|
|
return sentinelMaster.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-08-15 16:23:14 +03:00
|
|
|
}, "30s", "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())
|
2017-06-29 16:53:49 +03:00
|
|
|
|
|
|
|
// Publish message to check if subscription is renewed.
|
2020-03-11 17:26:42 +03:00
|
|
|
err = client.Publish(ctx, "foo", "hello").Err()
|
2017-06-29 16:53:49 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
var msg *redis.Message
|
2020-08-15 16:23:14 +03:00
|
|
|
Eventually(ch, "15s").Should(Receive(&msg))
|
2017-06-29 16:53:49 +03:00
|
|
|
Expect(msg.Channel).To(Equal("foo"))
|
|
|
|
Expect(msg.Payload).To(Equal("hello"))
|
2014-07-02 19:04:38 +04:00
|
|
|
})
|
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
It("supports DB selection", func() {
|
|
|
|
Expect(client.Close()).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
client = redis.NewFailoverClient(&redis.FailoverOptions{
|
|
|
|
MasterName: sentinelName,
|
|
|
|
SentinelAddrs: []string{":" + sentinelPort},
|
|
|
|
DB: 1,
|
|
|
|
})
|
2020-03-11 17:26:42 +03:00
|
|
|
err := client.Ping(ctx).Err()
|
2015-05-14 16:13:45 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
2015-01-15 18:51:22 +03:00
|
|
|
})
|