Skip to content

Commit

Permalink
fix data race in test case
Browse files Browse the repository at this point in the history
  • Loading branch information
amyangfei committed Oct 2, 2020
1 parent 2b6e7c7 commit 3c284e3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
15 changes: 6 additions & 9 deletions redlock/redlock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,24 +227,21 @@ func TestAcquireLockFailed(t *testing.T) {
}

func TestKVCache(t *testing.T) {
lock, err := NewRedLock(redisServers)
assert.Nil(t, err)

var wg sync.WaitGroup
for i := 0; i < 4; i++ {
wg.Add(1)
i := i
go func() {
defer wg.Done()
lock, err := NewRedLock(redisServers)
assert.Nil(t, err)
for j := 0; j < 100; j++ {
key := fmt.Sprintf("foo-%d-%d", i, j)
_, err = lock.Lock(key, 200)
_, err = lock.Lock("foo", 200)
assert.Nil(t, err)
err = lock.UnLock(key)
err = lock.UnLock("foo")
assert.Nil(t, err)
}
assert.Zero(t, lock.cache.Size())
}()
}
wg.Done()
assert.Zero(t, lock.cache.Size())
wg.Wait()
}
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ PACKAGES=$(go list ./... | grep -vE 'vendor|examples')
FILES=$(find . -name "*.go" | grep -vE "vendor|examples")

echo "Running tests..."
GO111MODULE=on go test -race -v -cover ${PACKAGES}
GO111MODULE=on go test -race -count=1 -v -cover ${PACKAGES}

echo "Checking gofmt..."
gofmt -s -l -w ${FILES} 2>&1 | awk '{print} END{if(NR>0) {exit 1}}'
Expand Down

0 comments on commit 3c284e3

Please sign in to comment.