Skip to content

Commit

Permalink
Merge pull request redis#738 from go-redis/fix/blpop-race-test
Browse files Browse the repository at this point in the history
Add race test for BLPop
  • Loading branch information
vmihailenco authored Mar 8, 2018
2 parents 9133634 + 11ca0e6 commit 396e13a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,8 @@ var _ = Describe("ClusterClient timeout", func() {
Context("read/write timeout", func() {
BeforeEach(func() {
opt := redisClusterOptions()
opt.ReadTimeout = 200 * time.Millisecond
opt.WriteTimeout = 200 * time.Millisecond
opt.ReadTimeout = 300 * time.Millisecond
opt.WriteTimeout = 300 * time.Millisecond
opt.MaxRedirects = 1
client = cluster.clusterClient(opt)

Expand Down
7 changes: 6 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func redisRingOptions() *redis.RingOptions {
}
}

func perform(n int, cbs ...func(int)) {
func performAsync(n int, cbs ...func(int)) *sync.WaitGroup {
var wg sync.WaitGroup
for _, cb := range cbs {
for i := 0; i < n; i++ {
Expand All @@ -155,6 +155,11 @@ func perform(n int, cbs ...func(int)) {
}(cb, i)
}
}
return &wg
}

func perform(n int, cbs ...func(int)) {
wg := performAsync(n, cbs...)
wg.Wait()
}

Expand Down
25 changes: 25 additions & 0 deletions race_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"strconv"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -258,6 +259,30 @@ var _ = Describe("races", func() {
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(N)))
})

It("should BLPop", func() {
var received uint32
wg := performAsync(C, func(id int) {
for {
v, err := client.BLPop(3*time.Second, "list").Result()
if err != nil {
break
}
Expect(v).To(Equal([]string{"list", "hello"}))
atomic.AddUint32(&received, 1)
}
})

perform(C, func(id int) {
for i := 0; i < N; i++ {
err := client.LPush("list", "hello").Err()
Expect(err).NotTo(HaveOccurred())
}
})

wg.Wait()
Expect(received).To(Equal(uint32(C * N)))
})
})

func bigVal() []byte {
Expand Down

0 comments on commit 396e13a

Please sign in to comment.