sentinel_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package redis_test
  2. import (
  3. . "github.com/onsi/ginkgo"
  4. . "github.com/onsi/gomega"
  5. "gopkg.in/redis.v5"
  6. )
  7. var _ = Describe("Sentinel", func() {
  8. var client *redis.Client
  9. BeforeEach(func() {
  10. client = redis.NewFailoverClient(&redis.FailoverOptions{
  11. MasterName: sentinelName,
  12. SentinelAddrs: []string{":" + sentinelPort},
  13. })
  14. Expect(client.FlushDb().Err()).NotTo(HaveOccurred())
  15. })
  16. AfterEach(func() {
  17. Expect(client.Close()).NotTo(HaveOccurred())
  18. })
  19. It("should facilitate failover", func() {
  20. // Set value on master, verify
  21. err := client.Set("foo", "master", 0).Err()
  22. Expect(err).NotTo(HaveOccurred())
  23. val, err := sentinelMaster.Get("foo").Result()
  24. Expect(err).NotTo(HaveOccurred())
  25. Expect(val).To(Equal("master"))
  26. // Wait until replicated
  27. Eventually(func() string {
  28. return sentinelSlave1.Get("foo").Val()
  29. }, "1s", "100ms").Should(Equal("master"))
  30. Eventually(func() string {
  31. return sentinelSlave2.Get("foo").Val()
  32. }, "1s", "100ms").Should(Equal("master"))
  33. // Wait until slaves are picked up by sentinel.
  34. Eventually(func() string {
  35. return sentinel.Info().Val()
  36. }, "10s", "100ms").Should(ContainSubstring("slaves=2"))
  37. // Kill master.
  38. sentinelMaster.Shutdown()
  39. Eventually(func() error {
  40. return sentinelMaster.Ping().Err()
  41. }, "5s", "100ms").Should(HaveOccurred())
  42. // Wait for Redis sentinel to elect new master.
  43. Eventually(func() string {
  44. return sentinelSlave1.Info().Val() + sentinelSlave2.Info().Val()
  45. }, "30s", "1s").Should(ContainSubstring("role:master"))
  46. // Check that client picked up new master.
  47. Eventually(func() error {
  48. return client.Get("foo").Err()
  49. }, "5s", "100ms").ShouldNot(HaveOccurred())
  50. })
  51. It("supports DB selection", func() {
  52. Expect(client.Close()).NotTo(HaveOccurred())
  53. client = redis.NewFailoverClient(&redis.FailoverOptions{
  54. MasterName: sentinelName,
  55. SentinelAddrs: []string{":" + sentinelPort},
  56. DB: 1,
  57. })
  58. err := client.Ping().Err()
  59. Expect(err).NotTo(HaveOccurred())
  60. })
  61. })