Skip to content

Commit eac5142

Browse files
lunnylafrikstechknowlogick
authored
Fix leveldb test race (#10054)
Co-authored-by: Lauris BH <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent d7f4f87 commit eac5142

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

modules/queue/queue_disk_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"io/ioutil"
1010
"os"
11+
"sync"
1112
"testing"
1213
"time"
1314

@@ -24,6 +25,7 @@ func TestLevelQueue(t *testing.T) {
2425
}
2526
}
2627

28+
var lock sync.Mutex
2729
queueShutdown := []func(){}
2830
queueTerminate := []func(){}
2931

@@ -46,9 +48,13 @@ func TestLevelQueue(t *testing.T) {
4648
assert.NoError(t, err)
4749

4850
go queue.Run(func(_ context.Context, shutdown func()) {
51+
lock.Lock()
4952
queueShutdown = append(queueShutdown, shutdown)
53+
lock.Unlock()
5054
}, func(_ context.Context, terminate func()) {
55+
lock.Lock()
5156
queueTerminate = append(queueTerminate, terminate)
57+
lock.Unlock()
5258
})
5359

5460
test1 := testData{"A", 1}
@@ -72,9 +78,12 @@ func TestLevelQueue(t *testing.T) {
7278
err = queue.Push(test1)
7379
assert.Error(t, err)
7480

81+
lock.Lock()
7582
for _, callback := range queueShutdown {
7683
callback()
7784
}
85+
lock.Unlock()
86+
7887
time.Sleep(200 * time.Millisecond)
7988
err = queue.Push(&test1)
8089
assert.NoError(t, err)
@@ -85,9 +94,11 @@ func TestLevelQueue(t *testing.T) {
8594
assert.Fail(t, "Handler processing should have stopped")
8695
default:
8796
}
97+
lock.Lock()
8898
for _, callback := range queueTerminate {
8999
callback()
90100
}
101+
lock.Unlock()
91102

92103
// Reopen queue
93104
queue, err = NewWrappedQueue(handle,
@@ -109,9 +120,13 @@ func TestLevelQueue(t *testing.T) {
109120
assert.NoError(t, err)
110121

111122
go queue.Run(func(_ context.Context, shutdown func()) {
123+
lock.Lock()
112124
queueShutdown = append(queueShutdown, shutdown)
125+
lock.Unlock()
113126
}, func(_ context.Context, terminate func()) {
127+
lock.Lock()
114128
queueTerminate = append(queueTerminate, terminate)
129+
lock.Unlock()
115130
})
116131

117132
result3 := <-handleChan
@@ -121,10 +136,13 @@ func TestLevelQueue(t *testing.T) {
121136
result4 := <-handleChan
122137
assert.Equal(t, test2.TestString, result4.TestString)
123138
assert.Equal(t, test2.TestInt, result4.TestInt)
139+
140+
lock.Lock()
124141
for _, callback := range queueShutdown {
125142
callback()
126143
}
127144
for _, callback := range queueTerminate {
128145
callback()
129146
}
147+
lock.Unlock()
130148
}

0 commit comments

Comments
 (0)