8
8
"context"
9
9
"io/ioutil"
10
10
"os"
11
+ "sync"
11
12
"testing"
12
13
"time"
13
14
@@ -24,6 +25,7 @@ func TestLevelQueue(t *testing.T) {
24
25
}
25
26
}
26
27
28
+ var lock sync.Mutex
27
29
queueShutdown := []func (){}
28
30
queueTerminate := []func (){}
29
31
@@ -46,9 +48,13 @@ func TestLevelQueue(t *testing.T) {
46
48
assert .NoError (t , err )
47
49
48
50
go queue .Run (func (_ context.Context , shutdown func ()) {
51
+ lock .Lock ()
49
52
queueShutdown = append (queueShutdown , shutdown )
53
+ lock .Unlock ()
50
54
}, func (_ context.Context , terminate func ()) {
55
+ lock .Lock ()
51
56
queueTerminate = append (queueTerminate , terminate )
57
+ lock .Unlock ()
52
58
})
53
59
54
60
test1 := testData {"A" , 1 }
@@ -72,9 +78,12 @@ func TestLevelQueue(t *testing.T) {
72
78
err = queue .Push (test1 )
73
79
assert .Error (t , err )
74
80
81
+ lock .Lock ()
75
82
for _ , callback := range queueShutdown {
76
83
callback ()
77
84
}
85
+ lock .Unlock ()
86
+
78
87
time .Sleep (200 * time .Millisecond )
79
88
err = queue .Push (& test1 )
80
89
assert .NoError (t , err )
@@ -85,9 +94,11 @@ func TestLevelQueue(t *testing.T) {
85
94
assert .Fail (t , "Handler processing should have stopped" )
86
95
default :
87
96
}
97
+ lock .Lock ()
88
98
for _ , callback := range queueTerminate {
89
99
callback ()
90
100
}
101
+ lock .Unlock ()
91
102
92
103
// Reopen queue
93
104
queue , err = NewWrappedQueue (handle ,
@@ -109,9 +120,13 @@ func TestLevelQueue(t *testing.T) {
109
120
assert .NoError (t , err )
110
121
111
122
go queue .Run (func (_ context.Context , shutdown func ()) {
123
+ lock .Lock ()
112
124
queueShutdown = append (queueShutdown , shutdown )
125
+ lock .Unlock ()
113
126
}, func (_ context.Context , terminate func ()) {
127
+ lock .Lock ()
114
128
queueTerminate = append (queueTerminate , terminate )
129
+ lock .Unlock ()
115
130
})
116
131
117
132
result3 := <- handleChan
@@ -121,10 +136,13 @@ func TestLevelQueue(t *testing.T) {
121
136
result4 := <- handleChan
122
137
assert .Equal (t , test2 .TestString , result4 .TestString )
123
138
assert .Equal (t , test2 .TestInt , result4 .TestInt )
139
+
140
+ lock .Lock ()
124
141
for _ , callback := range queueShutdown {
125
142
callback ()
126
143
}
127
144
for _ , callback := range queueTerminate {
128
145
callback ()
129
146
}
147
+ lock .Unlock ()
130
148
}
0 commit comments