Fix leveldb test race (#10054)

Co-authored-by: Lauris BH <lauris@nix.lv>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
Lunny Xiao 2020-01-31 00:09:39 +08:00 committed by GitHub
parent d7f4f87aaf
commit eac5142ac7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,6 +8,7 @@ import (
"context" "context"
"io/ioutil" "io/ioutil"
"os" "os"
"sync"
"testing" "testing"
"time" "time"
@ -24,6 +25,7 @@ func TestLevelQueue(t *testing.T) {
} }
} }
var lock sync.Mutex
queueShutdown := []func(){} queueShutdown := []func(){}
queueTerminate := []func(){} queueTerminate := []func(){}
@ -46,9 +48,13 @@ func TestLevelQueue(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
go queue.Run(func(_ context.Context, shutdown func()) { go queue.Run(func(_ context.Context, shutdown func()) {
lock.Lock()
queueShutdown = append(queueShutdown, shutdown) queueShutdown = append(queueShutdown, shutdown)
lock.Unlock()
}, func(_ context.Context, terminate func()) { }, func(_ context.Context, terminate func()) {
lock.Lock()
queueTerminate = append(queueTerminate, terminate) queueTerminate = append(queueTerminate, terminate)
lock.Unlock()
}) })
test1 := testData{"A", 1} test1 := testData{"A", 1}
@ -72,9 +78,12 @@ func TestLevelQueue(t *testing.T) {
err = queue.Push(test1) err = queue.Push(test1)
assert.Error(t, err) assert.Error(t, err)
lock.Lock()
for _, callback := range queueShutdown { for _, callback := range queueShutdown {
callback() callback()
} }
lock.Unlock()
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)
err = queue.Push(&test1) err = queue.Push(&test1)
assert.NoError(t, err) assert.NoError(t, err)
@ -85,9 +94,11 @@ func TestLevelQueue(t *testing.T) {
assert.Fail(t, "Handler processing should have stopped") assert.Fail(t, "Handler processing should have stopped")
default: default:
} }
lock.Lock()
for _, callback := range queueTerminate { for _, callback := range queueTerminate {
callback() callback()
} }
lock.Unlock()
// Reopen queue // Reopen queue
queue, err = NewWrappedQueue(handle, queue, err = NewWrappedQueue(handle,
@ -109,9 +120,13 @@ func TestLevelQueue(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
go queue.Run(func(_ context.Context, shutdown func()) { go queue.Run(func(_ context.Context, shutdown func()) {
lock.Lock()
queueShutdown = append(queueShutdown, shutdown) queueShutdown = append(queueShutdown, shutdown)
lock.Unlock()
}, func(_ context.Context, terminate func()) { }, func(_ context.Context, terminate func()) {
lock.Lock()
queueTerminate = append(queueTerminate, terminate) queueTerminate = append(queueTerminate, terminate)
lock.Unlock()
}) })
result3 := <-handleChan result3 := <-handleChan
@ -121,10 +136,13 @@ func TestLevelQueue(t *testing.T) {
result4 := <-handleChan result4 := <-handleChan
assert.Equal(t, test2.TestString, result4.TestString) assert.Equal(t, test2.TestString, result4.TestString)
assert.Equal(t, test2.TestInt, result4.TestInt) assert.Equal(t, test2.TestInt, result4.TestInt)
lock.Lock()
for _, callback := range queueShutdown { for _, callback := range queueShutdown {
callback() callback()
} }
for _, callback := range queueTerminate { for _, callback := range queueTerminate {
callback() callback()
} }
lock.Unlock()
} }