From 34facee367d1efc471832627e9d16dc7f732334e Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Thu, 17 May 2018 14:36:51 +0300 Subject: [PATCH] Add more race tests --- cluster.go | 5 ++-- cluster_test.go | 70 ++++++++++++++++++++++++------------------------ commands_test.go | 2 +- race_test.go | 10 +++++++ 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/cluster.go b/cluster.go index 4f5f5b5..5991afd 100644 --- a/cluster.go +++ b/cluster.go @@ -220,7 +220,7 @@ type clusterNodes struct { nodeCreateGroup singleflight.Group - generation uint32 + _generation uint32 // atomic } func newClusterNodes(opt *ClusterOptions) *clusterNodes { @@ -277,8 +277,7 @@ func (c *clusterNodes) Addrs() ([]string, error) { } func (c *clusterNodes) NextGeneration() uint32 { - c.generation++ - return c.generation + return atomic.AddUint32(&c._generation, 1) } // GC removes unused nodes. diff --git a/cluster_test.go b/cluster_test.go index 58bf738..24ea4e1 100644 --- a/cluster_test.go +++ b/cluster_test.go @@ -519,39 +519,37 @@ var _ = Describe("ClusterClient", func() { Expect(err).NotTo(HaveOccurred()) Expect(res).To(HaveLen(3)) - wanted := []redis.ClusterSlot{ - { - Start: 0, - End: 4999, - Nodes: []redis.ClusterNode{{ - Id: "", - Addr: "127.0.0.1:8220", - }, { - Id: "", - Addr: "127.0.0.1:8223", - }}, + wanted := []redis.ClusterSlot{{ + Start: 0, + End: 4999, + Nodes: []redis.ClusterNode{{ + Id: "", + Addr: "127.0.0.1:8220", }, { - Start: 5000, - End: 9999, - Nodes: []redis.ClusterNode{{ - Id: "", - Addr: "127.0.0.1:8221", - }, { - Id: "", - Addr: "127.0.0.1:8224", - }}, + Id: "", + Addr: "127.0.0.1:8223", + }}, + }, { + Start: 5000, + End: 9999, + Nodes: []redis.ClusterNode{{ + Id: "", + Addr: "127.0.0.1:8221", }, { - Start: 10000, - End: 16383, - Nodes: []redis.ClusterNode{{ - Id: "", - Addr: "127.0.0.1:8222", - }, { - Id: "", - Addr: "127.0.0.1:8225", - }}, - }, - } + Id: "", + Addr: "127.0.0.1:8224", + }}, + }, { + Start: 10000, + End: 16383, + Nodes: []redis.ClusterNode{{ + Id: "", + Addr: "127.0.0.1:8222", + }, { + Id: "", + Addr: "127.0.0.1:8225", + }}, + }} Expect(assertSlotsEqual(res, wanted)).NotTo(HaveOccurred()) }) @@ -634,16 +632,18 @@ var _ = Describe("ClusterClient", func() { opt.MaxRetryBackoff = time.Second client = cluster.clusterClient(opt) + err := client.ForEachMaster(func(master *redis.Client) error { + return master.FlushDB().Err() + }) + Expect(err).NotTo(HaveOccurred()) + _ = client.ForEachSlave(func(slave *redis.Client) error { defer GinkgoRecover() - _ = client.ForEachMaster(func(master *redis.Client) error { - return master.FlushDB().Err() - }) - Eventually(func() int64 { return slave.DBSize().Val() }, 30*time.Second).Should(Equal(int64(0))) + return slave.ClusterFailover().Err() }) }) diff --git a/commands_test.go b/commands_test.go index 595581f..f4f794f 100644 --- a/commands_test.go +++ b/commands_test.go @@ -219,7 +219,7 @@ var _ = Describe("Commands", func() { It("Should Command", func() { cmds, err := client.Command().Result() Expect(err).NotTo(HaveOccurred()) - Expect(len(cmds)).To(BeNumerically("~", 185, 10)) + Expect(len(cmds)).To(BeNumerically("~", 200, 20)) cmd := cmds["mget"] Expect(cmd.Name).To(Equal("mget")) diff --git a/race_test.go b/race_test.go index acecf85..04effc4 100644 --- a/race_test.go +++ b/race_test.go @@ -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() { key := "TestIncrFromGoroutines"