@@ -6,6 +6,7 @@ package queue
66
77import (
88 "io/ioutil"
9+ "sync"
910 "testing"
1011
1112 "code.gitea.io/gitea/modules/util"
@@ -22,6 +23,7 @@ func TestPersistableChannelQueue(t *testing.T) {
2223 }
2324 }
2425
26+ lock := sync.Mutex {}
2527 queueShutdown := []func (){}
2628 queueTerminate := []func (){}
2729
@@ -41,8 +43,12 @@ func TestPersistableChannelQueue(t *testing.T) {
4143 assert .NoError (t , err )
4244
4345 go queue .Run (func (shutdown func ()) {
46+ lock .Lock ()
47+ defer lock .Unlock ()
4448 queueShutdown = append (queueShutdown , shutdown )
4549 }, func (terminate func ()) {
50+ lock .Lock ()
51+ defer lock .Unlock ()
4652 queueTerminate = append (queueTerminate , terminate )
4753 })
4854
@@ -69,7 +75,11 @@ func TestPersistableChannelQueue(t *testing.T) {
6975 assert .Error (t , err )
7076
7177 // Now shutdown the queue
72- for _ , callback := range queueShutdown {
78+ lock .Lock ()
79+ callbacks := make ([]func (), len (queueShutdown ))
80+ copy (callbacks , queueShutdown )
81+ lock .Unlock ()
82+ for _ , callback := range callbacks {
7383 callback ()
7484 }
7585
@@ -87,7 +97,11 @@ func TestPersistableChannelQueue(t *testing.T) {
8797 }
8898
8999 // terminate the queue
90- for _ , callback := range queueTerminate {
100+ lock .Lock ()
101+ callbacks = make ([]func (), len (queueTerminate ))
102+ copy (callbacks , queueTerminate )
103+ lock .Unlock ()
104+ for _ , callback := range callbacks {
91105 callback ()
92106 }
93107
@@ -110,8 +124,12 @@ func TestPersistableChannelQueue(t *testing.T) {
110124 assert .NoError (t , err )
111125
112126 go queue .Run (func (shutdown func ()) {
127+ lock .Lock ()
128+ defer lock .Unlock ()
113129 queueShutdown = append (queueShutdown , shutdown )
114130 }, func (terminate func ()) {
131+ lock .Lock ()
132+ defer lock .Unlock ()
115133 queueTerminate = append (queueTerminate , terminate )
116134 })
117135
@@ -122,10 +140,19 @@ func TestPersistableChannelQueue(t *testing.T) {
122140 result4 := <- handleChan
123141 assert .Equal (t , test2 .TestString , result4 .TestString )
124142 assert .Equal (t , test2 .TestInt , result4 .TestInt )
125- for _ , callback := range queueShutdown {
143+
144+ lock .Lock ()
145+ callbacks = make ([]func (), len (queueShutdown ))
146+ copy (callbacks , queueShutdown )
147+ lock .Unlock ()
148+ for _ , callback := range callbacks {
126149 callback ()
127150 }
128- for _ , callback := range queueTerminate {
151+ lock .Lock ()
152+ callbacks = make ([]func (), len (queueTerminate ))
153+ copy (callbacks , queueTerminate )
154+ lock .Unlock ()
155+ for _ , callback := range callbacks {
129156 callback ()
130157 }
131158
0 commit comments