Skip to content

Commit e83abfc

Browse files
zeripath6543
andauthored
Prevent race in TestPersistableChannelQueue (go-gitea#16468)
* Prevent race in TestPersistableChannelQueue A slight race has become apparent in the TestPersistableChannelQueue. This PR simply adds locking to prevent the race. * make print value of "$(GOTESTFLAGS)" on test-backend and unit-test-coverage Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de>
1 parent 93f31e1 commit e83abfc

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ test: test-frontend test-backend
359359

360360
.PHONY: test-backend
361361
test-backend:
362-
@echo "Running go test with -tags '$(TEST_TAGS)'..."
362+
@echo "Running go test with $(GOTESTFLAGS) -tags '$(TEST_TAGS)'..."
363363
@$(GO) test $(GOTESTFLAGS) -mod=vendor -tags='$(TEST_TAGS)' $(GO_PACKAGES)
364364

365365
.PHONY: test-frontend
@@ -389,7 +389,7 @@ coverage:
389389

390390
.PHONY: unit-test-coverage
391391
unit-test-coverage:
392-
@echo "Running unit-test-coverage -tags '$(TEST_TAGS)'..."
392+
@echo "Running unit-test-coverage $(GOTESTFLAGS) -tags '$(TEST_TAGS)'..."
393393
@$(GO) test $(GOTESTFLAGS) -mod=vendor -tags='$(TEST_TAGS)' -cover -coverprofile coverage.out $(GO_PACKAGES) && echo "\n==>\033[32m Ok\033[m\n" || exit 1
394394

395395
.PHONY: vendor

modules/queue/queue_disk_channel_test.go

+31-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package queue
66

77
import (
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

Comments
 (0)