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