redis/sentinel_test.go

72 lines
1.9 KiB
Go
Raw Normal View History

2014-07-02 19:04:38 +04:00
package redis_test
import (
2015-01-15 18:51:22 +03:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
2014-07-02 19:04:38 +04:00
"gopkg.in/redis.v2"
)
2015-01-15 18:51:22 +03:00
var _ = Describe("Sentinel", func() {
2014-11-13 15:26:14 +03:00
2015-01-15 18:51:22 +03:00
const masterName = "mymaster"
const masterPort = "8123"
const sentinelPort = "8124"
2014-07-02 19:04:38 +04:00
2015-01-15 20:23:22 +03:00
It("should facilitate failover", func() {
master, err := startRedis(masterPort)
2015-01-15 18:51:22 +03:00
Expect(err).NotTo(HaveOccurred())
2015-01-15 20:23:22 +03:00
defer master.Close()
2015-01-15 18:51:22 +03:00
2015-01-15 20:23:22 +03:00
sentinel, err := startSentinel(sentinelPort, masterName, masterPort)
2015-01-15 18:51:22 +03:00
Expect(err).NotTo(HaveOccurred())
2015-01-15 20:23:22 +03:00
defer sentinel.Close()
2015-01-15 18:51:22 +03:00
2015-01-15 20:23:22 +03:00
slave1, err := startRedis("8125", "--slaveof", "127.0.0.1", masterPort)
2015-01-15 18:51:22 +03:00
Expect(err).NotTo(HaveOccurred())
2015-01-15 20:23:22 +03:00
defer slave1.Close()
2015-01-15 18:51:22 +03:00
2015-01-15 20:23:22 +03:00
slave2, err := startRedis("8126", "--slaveof", "127.0.0.1", masterPort)
Expect(err).NotTo(HaveOccurred())
defer slave2.Close()
2015-01-15 18:51:22 +03:00
client := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: masterName,
SentinelAddrs: []string{":" + sentinelPort},
})
defer client.Close()
// Set value on master, verify
2015-01-15 20:23:22 +03:00
err = client.Set("foo", "master").Err()
2015-01-15 18:51:22 +03:00
Expect(err).NotTo(HaveOccurred())
val, err := master.Get("foo").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("master"))
// Wait until replicated
Eventually(func() string {
return slave1.Get("foo").Val()
}, "1s", "100ms").Should(Equal("master"))
Eventually(func() string {
return slave2.Get("foo").Val()
}, "1s", "100ms").Should(Equal("master"))
// Kill master.
master.Shutdown()
Eventually(func() error {
return master.Ping().Err()
}, "5s", "100ms").Should(HaveOccurred())
// Wait for Redis sentinel to elect new master.
Eventually(func() string {
return slave1.Info().Val() + slave2.Info().Val()
}, "30s", "500ms").Should(ContainSubstring("role:master"))
// Check that client picked up new master.
val, err = client.Get("foo").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("master"))
2014-07-02 19:04:38 +04:00
})
2015-01-15 18:51:22 +03:00
})