internal_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package redis
  2. import (
  3. . "github.com/onsi/ginkgo"
  4. . "github.com/onsi/gomega"
  5. )
  6. var _ = Describe("newClusterState", func() {
  7. var state *clusterState
  8. createClusterState := func(slots []ClusterSlot) *clusterState {
  9. nodes := newClusterNodes(&ClusterOptions{})
  10. state, err := newClusterState(nodes, slots, "10.10.10.10:1234")
  11. Expect(err).NotTo(HaveOccurred())
  12. return state
  13. }
  14. Describe("sorting", func() {
  15. BeforeEach(func() {
  16. state = createClusterState([]ClusterSlot{{
  17. Start: 1000,
  18. End: 1999,
  19. }, {
  20. Start: 0,
  21. End: 999,
  22. }, {
  23. Start: 2000,
  24. End: 2999,
  25. }})
  26. })
  27. It("sorts slots", func() {
  28. Expect(state.slots).To(Equal([]*clusterSlot{
  29. {start: 0, end: 999, nodes: nil},
  30. {start: 1000, end: 1999, nodes: nil},
  31. {start: 2000, end: 2999, nodes: nil},
  32. }))
  33. })
  34. })
  35. Describe("loopback", func() {
  36. BeforeEach(func() {
  37. state = createClusterState([]ClusterSlot{{
  38. Nodes: []ClusterNode{{Addr: "127.0.0.1:7001"}},
  39. }, {
  40. Nodes: []ClusterNode{{Addr: "127.0.0.1:7002"}},
  41. }, {
  42. Nodes: []ClusterNode{{Addr: "1.2.3.4:1234"}},
  43. }, {
  44. Nodes: []ClusterNode{{Addr: ":1234"}},
  45. }})
  46. })
  47. It("replaces loopback hosts in addresses", func() {
  48. slotAddr := func(slot *clusterSlot) string {
  49. return slot.nodes[0].Client.Options().Addr
  50. }
  51. Expect(slotAddr(state.slots[0])).To(Equal("10.10.10.10:7001"))
  52. Expect(slotAddr(state.slots[1])).To(Equal("10.10.10.10:7002"))
  53. Expect(slotAddr(state.slots[2])).To(Equal("1.2.3.4:1234"))
  54. Expect(slotAddr(state.slots[3])).To(Equal(":1234"))
  55. })
  56. })
  57. })