forked from mirror/redis
More cluster tests.
This commit is contained in:
parent
f5245efa73
commit
639950777c
|
@ -12,6 +12,8 @@ import (
|
|||
"gopkg.in/redis.v5/internal/pool"
|
||||
)
|
||||
|
||||
var errClusterNoNodes = errors.RedisError("redis: cluster has no nodes")
|
||||
|
||||
// ClusterOptions are used to configure a cluster client and should be
|
||||
// passed to NewClusterClient.
|
||||
type ClusterOptions struct {
|
||||
|
@ -155,14 +157,14 @@ func (c *ClusterClient) cmdInfo(name string) *CommandInfo {
|
|||
|
||||
func (c *ClusterClient) getNodes() map[string]*clusterNode {
|
||||
var nodes map[string]*clusterNode
|
||||
c.mu.RLock()
|
||||
if !c.closed {
|
||||
nodes = make(map[string]*clusterNode, len(c.nodes))
|
||||
c.mu.RLock()
|
||||
for addr, node := range c.nodes {
|
||||
nodes[addr] = node
|
||||
}
|
||||
c.mu.RUnlock()
|
||||
}
|
||||
c.mu.RUnlock()
|
||||
return nodes
|
||||
}
|
||||
|
||||
|
@ -261,6 +263,9 @@ func (c *ClusterClient) randomNode() (*clusterNode, error) {
|
|||
return nil, pool.ErrClosed
|
||||
}
|
||||
|
||||
if len(addrs) == 0 {
|
||||
return nil, errClusterNoNodes
|
||||
}
|
||||
n := rand.Intn(len(addrs))
|
||||
|
||||
node, err := c.nodeByAddr(addrs[n])
|
||||
|
|
|
@ -301,6 +301,37 @@ var _ = Describe("ClusterClient", func() {
|
|||
Expect(client.Close()).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("distributes keys", func() {
|
||||
for i := 0; i < 100; i++ {
|
||||
err := client.Set(fmt.Sprintf("key%d", i), "value", 0).Err()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
|
||||
wanted := []string{"keys=31", "keys=29", "keys=40"}
|
||||
for i, master := range cluster.masters() {
|
||||
Expect(master.Info().Val()).To(ContainSubstring(wanted[i]))
|
||||
}
|
||||
})
|
||||
|
||||
It("distributes keys when using EVAL", func() {
|
||||
script := redis.NewScript(`
|
||||
local r = redis.call('SET', KEYS[1], ARGV[1])
|
||||
return r
|
||||
`)
|
||||
|
||||
var key string
|
||||
for i := 0; i < 100; i++ {
|
||||
key = fmt.Sprintf("key%d", i)
|
||||
err := script.Run(client, []string{key}, "value").Err()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
|
||||
wanted := []string{"keys=31", "keys=29", "keys=40"}
|
||||
for i, master := range cluster.masters() {
|
||||
Expect(master.Info().Val()).To(ContainSubstring(wanted[i]))
|
||||
}
|
||||
})
|
||||
|
||||
It("supports Watch", func() {
|
||||
var incr func(string) error
|
||||
|
||||
|
@ -342,7 +373,8 @@ var _ = Describe("ClusterClient", func() {
|
|||
Expect(n).To(Equal(int64(100)))
|
||||
})
|
||||
|
||||
It("supports pipeline", func() {
|
||||
Describe("pipeline", func() {
|
||||
It("follows redirects", func() {
|
||||
slot := hashtag.Slot("A")
|
||||
Expect(client.SwapSlotNodes(slot)).To(Equal([]string{"127.0.0.1:8224", "127.0.0.1:8221"}))
|
||||
|
||||
|
@ -374,7 +406,7 @@ var _ = Describe("ClusterClient", func() {
|
|||
Expect(cmds[13].(*redis.DurationCmd).Val()).To(BeNumerically("~", 7*time.Hour, time.Second))
|
||||
})
|
||||
|
||||
It("supports pipeline with missing keys", func() {
|
||||
It("works with missing keys", func() {
|
||||
Expect(client.Set("A", "A_value", 0).Err()).NotTo(HaveOccurred())
|
||||
Expect(client.Set("C", "C_value", 0).Err()).NotTo(HaveOccurred())
|
||||
|
||||
|
@ -397,6 +429,7 @@ var _ = Describe("ClusterClient", func() {
|
|||
Expect(c.Err()).NotTo(HaveOccurred())
|
||||
Expect(c.Val()).To(Equal("C_value"))
|
||||
})
|
||||
})
|
||||
|
||||
It("calls fn for every master node", func() {
|
||||
for i := 0; i < 10; i++ {
|
||||
|
@ -451,6 +484,25 @@ var _ = Describe("ClusterClient", func() {
|
|||
describeClusterClient()
|
||||
})
|
||||
|
||||
Describe("ClusterClient without nodes", func() {
|
||||
BeforeEach(func() {
|
||||
client = redis.NewClusterClient(&redis.ClusterOptions{})
|
||||
})
|
||||
|
||||
It("returns an error", func() {
|
||||
err := client.Ping().Err()
|
||||
Expect(err).To(MatchError("redis: cluster has no nodes"))
|
||||
})
|
||||
|
||||
It("pipeline returns an error", func() {
|
||||
_, err := client.Pipelined(func(pipe *redis.Pipeline) error {
|
||||
pipe.Ping()
|
||||
return nil
|
||||
})
|
||||
Expect(err).To(MatchError("redis: cluster has no nodes"))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("ClusterClient without valid nodes", func() {
|
||||
BeforeEach(func() {
|
||||
client = redis.NewClusterClient(&redis.ClusterOptions{
|
||||
|
@ -462,6 +514,14 @@ var _ = Describe("ClusterClient", func() {
|
|||
err := client.Ping().Err()
|
||||
Expect(err).To(MatchError("ERR This instance has cluster support disabled"))
|
||||
})
|
||||
|
||||
It("pipeline returns an error", func() {
|
||||
_, err := client.Pipelined(func(pipe *redis.Pipeline) error {
|
||||
pipe.Ping()
|
||||
return nil
|
||||
})
|
||||
Expect(err).To(MatchError("ERR This instance has cluster support disabled"))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
|
74
ring_test.go
74
ring_test.go
|
@ -38,7 +38,7 @@ var _ = Describe("Redis Ring", func() {
|
|||
Expect(ring.Close()).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("uses both shards", func() {
|
||||
It("distributes keys", func() {
|
||||
setRingKeys()
|
||||
|
||||
// Both shards should have some keys now.
|
||||
|
@ -46,6 +46,23 @@ var _ = Describe("Redis Ring", func() {
|
|||
Expect(ringShard2.Info().Val()).To(ContainSubstring("keys=43"))
|
||||
})
|
||||
|
||||
It("distributes keys when using EVAL", func() {
|
||||
script := redis.NewScript(`
|
||||
local r = redis.call('SET', KEYS[1], ARGV[1])
|
||||
return r
|
||||
`)
|
||||
|
||||
var key string
|
||||
for i := 0; i < 100; i++ {
|
||||
key = fmt.Sprintf("key%d", i)
|
||||
err := script.Run(ring, []string{key}, "value").Err()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
|
||||
Expect(ringShard1.Info().Val()).To(ContainSubstring("keys=57"))
|
||||
Expect(ringShard2.Info().Val()).To(ContainSubstring("keys=43"))
|
||||
})
|
||||
|
||||
It("uses single shard when one of the shards is down", func() {
|
||||
// Stop ringShard2.
|
||||
Expect(ringShard2.Close()).NotTo(HaveOccurred())
|
||||
|
@ -89,34 +106,8 @@ var _ = Describe("Redis Ring", func() {
|
|||
Expect(ringShard2.Info().Val()).To(ContainSubstring("keys=100"))
|
||||
})
|
||||
|
||||
It("supports eval key search", func() {
|
||||
script := redis.NewScript(`
|
||||
local r = redis.call('SET', KEYS[1], ARGV[1])
|
||||
return r
|
||||
`)
|
||||
|
||||
var key string
|
||||
for i := 0; i < 100; i++ {
|
||||
key = fmt.Sprintf("key{%d}", i)
|
||||
err := script.Run(ring, []string{key}, "value").Err()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
|
||||
Expect(ringShard1.Info().Val()).To(ContainSubstring("keys=52"))
|
||||
Expect(ringShard2.Info().Val()).To(ContainSubstring("keys=48"))
|
||||
})
|
||||
|
||||
Describe("pipelining", func() {
|
||||
It("returns an error when all shards are down", func() {
|
||||
ring := redis.NewRing(&redis.RingOptions{})
|
||||
_, err := ring.Pipelined(func(pipe *redis.Pipeline) error {
|
||||
pipe.Ping()
|
||||
return nil
|
||||
})
|
||||
Expect(err).To(MatchError("redis: all ring shards are down"))
|
||||
})
|
||||
|
||||
It("uses both shards", func() {
|
||||
Describe("pipeline", func() {
|
||||
It("distributes keys", func() {
|
||||
pipe := ring.Pipeline()
|
||||
for i := 0; i < 100; i++ {
|
||||
err := pipe.Set(fmt.Sprintf("key%d", i), "value", 0).Err()
|
||||
|
@ -175,3 +166,28 @@ var _ = Describe("Redis Ring", func() {
|
|||
})
|
||||
})
|
||||
})
|
||||
|
||||
var _ = Describe("empty Redis Ring", func() {
|
||||
var ring *redis.Ring
|
||||
|
||||
BeforeEach(func() {
|
||||
ring = redis.NewRing(&redis.RingOptions{})
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
Expect(ring.Close()).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("returns an error", func() {
|
||||
err := ring.Ping().Err()
|
||||
Expect(err).To(MatchError("redis: all ring shards are down"))
|
||||
})
|
||||
|
||||
It("pipeline returns an error", func() {
|
||||
_, err := ring.Pipelined(func(pipe *redis.Pipeline) error {
|
||||
pipe.Ping()
|
||||
return nil
|
||||
})
|
||||
Expect(err).To(MatchError("redis: all ring shards are down"))
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue