Merge pull request #765 from go-redis/fix/race-test

Add more race tests
This commit is contained in:
Vladimir Mihailenco 2018-05-17 15:01:41 +03:00 committed by GitHub
commit 467749ade8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 39 deletions

View File

@ -220,7 +220,7 @@ type clusterNodes struct {
nodeCreateGroup singleflight.Group nodeCreateGroup singleflight.Group
generation uint32 _generation uint32 // atomic
} }
func newClusterNodes(opt *ClusterOptions) *clusterNodes { func newClusterNodes(opt *ClusterOptions) *clusterNodes {
@ -277,8 +277,7 @@ func (c *clusterNodes) Addrs() ([]string, error) {
} }
func (c *clusterNodes) NextGeneration() uint32 { func (c *clusterNodes) NextGeneration() uint32 {
c.generation++ return atomic.AddUint32(&c._generation, 1)
return c.generation
} }
// GC removes unused nodes. // GC removes unused nodes.

View File

@ -519,8 +519,7 @@ var _ = Describe("ClusterClient", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(res).To(HaveLen(3)) Expect(res).To(HaveLen(3))
wanted := []redis.ClusterSlot{ wanted := []redis.ClusterSlot{{
{
Start: 0, Start: 0,
End: 4999, End: 4999,
Nodes: []redis.ClusterNode{{ Nodes: []redis.ClusterNode{{
@ -550,8 +549,7 @@ var _ = Describe("ClusterClient", func() {
Id: "", Id: "",
Addr: "127.0.0.1:8225", Addr: "127.0.0.1:8225",
}}, }},
}, }}
}
Expect(assertSlotsEqual(res, wanted)).NotTo(HaveOccurred()) Expect(assertSlotsEqual(res, wanted)).NotTo(HaveOccurred())
}) })
@ -634,16 +632,18 @@ var _ = Describe("ClusterClient", func() {
opt.MaxRetryBackoff = time.Second opt.MaxRetryBackoff = time.Second
client = cluster.clusterClient(opt) client = cluster.clusterClient(opt)
_ = client.ForEachSlave(func(slave *redis.Client) error { err := client.ForEachMaster(func(master *redis.Client) error {
defer GinkgoRecover()
_ = client.ForEachMaster(func(master *redis.Client) error {
return master.FlushDB().Err() return master.FlushDB().Err()
}) })
Expect(err).NotTo(HaveOccurred())
_ = client.ForEachSlave(func(slave *redis.Client) error {
defer GinkgoRecover()
Eventually(func() int64 { Eventually(func() int64 {
return slave.DBSize().Val() return slave.DBSize().Val()
}, 30*time.Second).Should(Equal(int64(0))) }, 30*time.Second).Should(Equal(int64(0)))
return slave.ClusterFailover().Err() return slave.ClusterFailover().Err()
}) })
}) })

View File

@ -219,7 +219,7 @@ var _ = Describe("Commands", func() {
It("Should Command", func() { It("Should Command", func() {
cmds, err := client.Command().Result() cmds, err := client.Command().Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(len(cmds)).To(BeNumerically("~", 185, 10)) Expect(len(cmds)).To(BeNumerically("~", 200, 20))
cmd := cmds["mget"] cmd := cmds["mget"]
Expect(cmd.Name).To(Equal("mget")) Expect(cmd.Name).To(Equal("mget"))

View File

@ -316,6 +316,16 @@ var _ = Describe("cluster races", func() {
}) })
}) })
It("should get", func() {
perform(C, func(id int) {
for i := 0; i < N; i++ {
key := fmt.Sprintf("key_%d_%d", id, i)
_, err := client.Get(key).Result()
Expect(err).To(Equal(redis.Nil))
}
})
})
It("should incr", func() { It("should incr", func() {
key := "TestIncrFromGoroutines" key := "TestIncrFromGoroutines"