From 3c284e35cccc7f597791b80c8e6919fb3fb32a33 Mon Sep 17 00:00:00 2001 From: amyangfei Date: Fri, 2 Oct 2020 13:29:52 +0800 Subject: [PATCH] fix data race in test case --- redlock/redlock_test.go | 15 ++++++--------- test.sh | 2 +- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/redlock/redlock_test.go b/redlock/redlock_test.go index d4a0eba..403d6ae 100644 --- a/redlock/redlock_test.go +++ b/redlock/redlock_test.go @@ -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() } diff --git a/test.sh b/test.sh index 0ff7516..b000dd8 100755 --- a/test.sh +++ b/test.sh @@ -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}}'