2014-07-02 19:04:38 +04:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
2017-02-18 17:42:34 +03:00
|
|
|
"github.com/go-redis/redis"
|
|
|
|
|
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},
|
|
|
|
})
|
2016-03-16 17:57:24 +03:00
|
|
|
Expect(client.FlushDb().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() {
|
2015-01-15 18:51:22 +03:00
|
|
|
// Set value on master, verify
|
2015-05-14 16:13:45 +03:00
|
|
|
err := client.Set("foo", "master", 0).Err()
|
2015-01-15 18:51:22 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
val, err := sentinelMaster.Get("foo").Result()
|
2015-01-15 18:51:22 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(val).To(Equal("master"))
|
|
|
|
|
|
|
|
// Wait until replicated
|
|
|
|
Eventually(func() string {
|
2015-05-14 16:13:45 +03:00
|
|
|
return sentinelSlave1.Get("foo").Val()
|
2015-01-15 18:51:22 +03:00
|
|
|
}, "1s", "100ms").Should(Equal("master"))
|
|
|
|
Eventually(func() string {
|
2015-05-14 16:13:45 +03:00
|
|
|
return sentinelSlave2.Get("foo").Val()
|
2015-01-15 18:51:22 +03:00
|
|
|
}, "1s", "100ms").Should(Equal("master"))
|
|
|
|
|
2015-01-30 17:45:57 +03:00
|
|
|
// Wait until slaves are picked up by sentinel.
|
|
|
|
Eventually(func() string {
|
|
|
|
return sentinel.Info().Val()
|
|
|
|
}, "10s", "100ms").Should(ContainSubstring("slaves=2"))
|
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
// Kill master.
|
2015-05-14 16:13:45 +03:00
|
|
|
sentinelMaster.Shutdown()
|
2015-01-15 18:51:22 +03:00
|
|
|
Eventually(func() error {
|
2015-05-14 16:13:45 +03:00
|
|
|
return sentinelMaster.Ping().Err()
|
2015-01-15 18:51:22 +03:00
|
|
|
}, "5s", "100ms").Should(HaveOccurred())
|
|
|
|
|
|
|
|
// Wait for Redis sentinel to elect new master.
|
|
|
|
Eventually(func() string {
|
2015-05-14 16:13:45 +03:00
|
|
|
return sentinelSlave1.Info().Val() + sentinelSlave2.Info().Val()
|
2015-01-30 17:45:57 +03:00
|
|
|
}, "30s", "1s").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 {
|
|
|
|
return client.Get("foo").Err()
|
|
|
|
}, "5s", "100ms").ShouldNot(HaveOccurred())
|
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,
|
|
|
|
})
|
|
|
|
err := client.Ping().Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
2015-01-15 18:51:22 +03:00
|
|
|
})
|