Prevent race in TestPersistableChannelQueue (#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>
This commit is contained in:
zeripath 2021-07-17 18:09:56 +01:00 committed by GitHub
parent 93f31e1897
commit e83abfc289
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 6 deletions

View file

@ -359,7 +359,7 @@ test: test-frontend test-backend
.PHONY: test-backend .PHONY: test-backend
test-backend: test-backend:
@echo "Running go test with -tags '$(TEST_TAGS)'..." @echo "Running go test with $(GOTESTFLAGS) -tags '$(TEST_TAGS)'..."
@$(GO) test $(GOTESTFLAGS) -mod=vendor -tags='$(TEST_TAGS)' $(GO_PACKAGES) @$(GO) test $(GOTESTFLAGS) -mod=vendor -tags='$(TEST_TAGS)' $(GO_PACKAGES)
.PHONY: test-frontend .PHONY: test-frontend
@ -389,7 +389,7 @@ coverage:
.PHONY: unit-test-coverage .PHONY: unit-test-coverage
unit-test-coverage: unit-test-coverage:
@echo "Running unit-test-coverage -tags '$(TEST_TAGS)'..." @echo "Running unit-test-coverage $(GOTESTFLAGS) -tags '$(TEST_TAGS)'..."
@$(GO) test $(GOTESTFLAGS) -mod=vendor -tags='$(TEST_TAGS)' -cover -coverprofile coverage.out $(GO_PACKAGES) && echo "\n==>\033[32m Ok\033[m\n" || exit 1 @$(GO) test $(GOTESTFLAGS) -mod=vendor -tags='$(TEST_TAGS)' -cover -coverprofile coverage.out $(GO_PACKAGES) && echo "\n==>\033[32m Ok\033[m\n" || exit 1
.PHONY: vendor .PHONY: vendor

View file

@ -6,6 +6,7 @@ package queue
import ( import (
"io/ioutil" "io/ioutil"
"sync"
"testing" "testing"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
@ -22,6 +23,7 @@ func TestPersistableChannelQueue(t *testing.T) {
} }
} }
lock := sync.Mutex{}
queueShutdown := []func(){} queueShutdown := []func(){}
queueTerminate := []func(){} queueTerminate := []func(){}
@ -41,8 +43,12 @@ func TestPersistableChannelQueue(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
go queue.Run(func(shutdown func()) { go queue.Run(func(shutdown func()) {
lock.Lock()
defer lock.Unlock()
queueShutdown = append(queueShutdown, shutdown) queueShutdown = append(queueShutdown, shutdown)
}, func(terminate func()) { }, func(terminate func()) {
lock.Lock()
defer lock.Unlock()
queueTerminate = append(queueTerminate, terminate) queueTerminate = append(queueTerminate, terminate)
}) })
@ -69,7 +75,11 @@ func TestPersistableChannelQueue(t *testing.T) {
assert.Error(t, err) assert.Error(t, err)
// Now shutdown the queue // Now shutdown the queue
for _, callback := range queueShutdown { lock.Lock()
callbacks := make([]func(), len(queueShutdown))
copy(callbacks, queueShutdown)
lock.Unlock()
for _, callback := range callbacks {
callback() callback()
} }
@ -87,7 +97,11 @@ func TestPersistableChannelQueue(t *testing.T) {
} }
// terminate the queue // terminate the queue
for _, callback := range queueTerminate { lock.Lock()
callbacks = make([]func(), len(queueTerminate))
copy(callbacks, queueTerminate)
lock.Unlock()
for _, callback := range callbacks {
callback() callback()
} }
@ -110,8 +124,12 @@ func TestPersistableChannelQueue(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
go queue.Run(func(shutdown func()) { go queue.Run(func(shutdown func()) {
lock.Lock()
defer lock.Unlock()
queueShutdown = append(queueShutdown, shutdown) queueShutdown = append(queueShutdown, shutdown)
}, func(terminate func()) { }, func(terminate func()) {
lock.Lock()
defer lock.Unlock()
queueTerminate = append(queueTerminate, terminate) queueTerminate = append(queueTerminate, terminate)
}) })
@ -122,10 +140,19 @@ func TestPersistableChannelQueue(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)
for _, callback := range queueShutdown {
lock.Lock()
callbacks = make([]func(), len(queueShutdown))
copy(callbacks, queueShutdown)
lock.Unlock()
for _, callback := range callbacks {
callback() callback()
} }
for _, callback := range queueTerminate { lock.Lock()
callbacks = make([]func(), len(queueTerminate))
copy(callbacks, queueTerminate)
lock.Unlock()
for _, callback := range callbacks {
callback() callback()
} }