2015-04-08 12:40:45 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2015-05-01 10:42:58 +03:00
|
|
|
func (c *ClusterClient) SlotAddrs(slot int) []string {
|
2016-05-06 21:12:31 +03:00
|
|
|
var addrs []string
|
|
|
|
for _, n := range c.slotNodes(slot) {
|
|
|
|
addrs = append(addrs, n.Addr)
|
|
|
|
}
|
|
|
|
return addrs
|
2015-03-18 13:41:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SwapSlot swaps a slot's master/slave address
|
|
|
|
// for testing MOVED redirects
|
2016-05-06 21:12:31 +03:00
|
|
|
func (c *ClusterClient) SwapSlotNodes(slot int) []string {
|
|
|
|
c.mu.Lock()
|
|
|
|
nodes := c.slots[slot]
|
|
|
|
nodes[0], nodes[1] = nodes[1], nodes[0]
|
|
|
|
c.mu.Unlock()
|
|
|
|
return c.SlotAddrs(slot)
|
2015-03-18 13:41:24 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:40:45 +03:00
|
|
|
var _ = Describe("ClusterClient", func() {
|
|
|
|
var subject *ClusterClient
|
|
|
|
|
|
|
|
var populate = func() {
|
2016-04-09 12:52:43 +03:00
|
|
|
subject.setSlots([]ClusterSlot{
|
|
|
|
{0, 4095, []ClusterNode{{"", "127.0.0.1:7000"}, {"", "127.0.0.1:7004"}}},
|
|
|
|
{12288, 16383, []ClusterNode{{"", "127.0.0.1:7003"}, {"", "127.0.0.1:7007"}}},
|
|
|
|
{4096, 8191, []ClusterNode{{"", "127.0.0.1:7001"}, {"", "127.0.0.1:7005"}}},
|
|
|
|
{8192, 12287, []ClusterNode{{"", "127.0.0.1:7002"}, {"", "127.0.0.1:7006"}}},
|
2015-04-08 12:40:45 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
subject = NewClusterClient(&ClusterOptions{
|
|
|
|
Addrs: []string{"127.0.0.1:6379", "127.0.0.1:7003", "127.0.0.1:7006"},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
2015-12-22 12:44:49 +03:00
|
|
|
_ = subject.Close()
|
2015-04-08 12:40:45 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should initialize", func() {
|
|
|
|
Expect(subject.addrs).To(HaveLen(3))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should update slots cache", func() {
|
|
|
|
populate()
|
2016-05-06 21:12:31 +03:00
|
|
|
Expect(subject.slots[0][0].Addr).To(Equal("127.0.0.1:7000"))
|
|
|
|
Expect(subject.slots[0][1].Addr).To(Equal("127.0.0.1:7004"))
|
|
|
|
Expect(subject.slots[4095][0].Addr).To(Equal("127.0.0.1:7000"))
|
|
|
|
Expect(subject.slots[4095][1].Addr).To(Equal("127.0.0.1:7004"))
|
|
|
|
Expect(subject.slots[4096][0].Addr).To(Equal("127.0.0.1:7001"))
|
|
|
|
Expect(subject.slots[4096][1].Addr).To(Equal("127.0.0.1:7005"))
|
|
|
|
Expect(subject.slots[8191][0].Addr).To(Equal("127.0.0.1:7001"))
|
|
|
|
Expect(subject.slots[8191][1].Addr).To(Equal("127.0.0.1:7005"))
|
|
|
|
Expect(subject.slots[8192][0].Addr).To(Equal("127.0.0.1:7002"))
|
|
|
|
Expect(subject.slots[8192][1].Addr).To(Equal("127.0.0.1:7006"))
|
|
|
|
Expect(subject.slots[12287][0].Addr).To(Equal("127.0.0.1:7002"))
|
|
|
|
Expect(subject.slots[12287][1].Addr).To(Equal("127.0.0.1:7006"))
|
|
|
|
Expect(subject.slots[12288][0].Addr).To(Equal("127.0.0.1:7003"))
|
|
|
|
Expect(subject.slots[12288][1].Addr).To(Equal("127.0.0.1:7007"))
|
|
|
|
Expect(subject.slots[16383][0].Addr).To(Equal("127.0.0.1:7003"))
|
|
|
|
Expect(subject.slots[16383][1].Addr).To(Equal("127.0.0.1:7007"))
|
2015-04-08 12:40:45 +03:00
|
|
|
Expect(subject.addrs).To(Equal([]string{
|
|
|
|
"127.0.0.1:6379",
|
|
|
|
"127.0.0.1:7003",
|
|
|
|
"127.0.0.1:7006",
|
|
|
|
"127.0.0.1:7000",
|
|
|
|
"127.0.0.1:7004",
|
|
|
|
"127.0.0.1:7007",
|
|
|
|
"127.0.0.1:7001",
|
|
|
|
"127.0.0.1:7005",
|
|
|
|
"127.0.0.1:7002",
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2015-04-13 16:33:44 +03:00
|
|
|
It("should close", func() {
|
|
|
|
populate()
|
|
|
|
Expect(subject.Close()).NotTo(HaveOccurred())
|
2016-05-06 21:12:31 +03:00
|
|
|
Expect(subject.addrs).To(BeEmpty())
|
|
|
|
Expect(subject.nodes).To(BeEmpty())
|
|
|
|
Expect(subject.slots).To(BeEmpty())
|
2015-05-01 11:01:01 +03:00
|
|
|
Expect(subject.Ping().Err().Error()).To(Equal("redis: client is closed"))
|
2015-04-13 16:33:44 +03:00
|
|
|
})
|
2015-04-08 12:40:45 +03:00
|
|
|
})
|