diff options
author | zeripath <art27@cantab.net> | 2021-07-17 18:09:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-17 19:09:56 +0200 |
commit | e83abfc2898e499c64276fa1bf03e6ccf5aa0390 (patch) | |
tree | 200b456d3a1a8956d81fe4d1d7f9deea9a8151d6 /modules | |
parent | 93f31e1897562a588bed1227e46d1a5b34f52524 (diff) | |
download | gitea-e83abfc2898e499c64276fa1bf03e6ccf5aa0390.tar.gz gitea-e83abfc2898e499c64276fa1bf03e6ccf5aa0390.zip |
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>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/queue/queue_disk_channel_test.go | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/modules/queue/queue_disk_channel_test.go b/modules/queue/queue_disk_channel_test.go index 561f98ca90..bbe8a5ecbd 100644 --- a/modules/queue/queue_disk_channel_test.go +++ b/modules/queue/queue_disk_channel_test.go @@ -6,6 +6,7 @@ package queue import ( "io/ioutil" + "sync" "testing" "code.gitea.io/gitea/modules/util" @@ -22,6 +23,7 @@ func TestPersistableChannelQueue(t *testing.T) { } } + lock := sync.Mutex{} queueShutdown := []func(){} queueTerminate := []func(){} @@ -41,8 +43,12 @@ func TestPersistableChannelQueue(t *testing.T) { assert.NoError(t, err) go queue.Run(func(shutdown func()) { + lock.Lock() + defer lock.Unlock() queueShutdown = append(queueShutdown, shutdown) }, func(terminate func()) { + lock.Lock() + defer lock.Unlock() queueTerminate = append(queueTerminate, terminate) }) @@ -69,7 +75,11 @@ func TestPersistableChannelQueue(t *testing.T) { assert.Error(t, err) // 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() } @@ -87,7 +97,11 @@ func TestPersistableChannelQueue(t *testing.T) { } // terminate the queue - for _, callback := range queueTerminate { + lock.Lock() + callbacks = make([]func(), len(queueTerminate)) + copy(callbacks, queueTerminate) + lock.Unlock() + for _, callback := range callbacks { callback() } @@ -110,8 +124,12 @@ func TestPersistableChannelQueue(t *testing.T) { assert.NoError(t, err) go queue.Run(func(shutdown func()) { + lock.Lock() + defer lock.Unlock() queueShutdown = append(queueShutdown, shutdown) }, func(terminate func()) { + lock.Lock() + defer lock.Unlock() queueTerminate = append(queueTerminate, terminate) }) @@ -122,10 +140,19 @@ func TestPersistableChannelQueue(t *testing.T) { result4 := <-handleChan assert.Equal(t, test2.TestString, result4.TestString) 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() } - for _, callback := range queueTerminate { + lock.Lock() + callbacks = make([]func(), len(queueTerminate)) + copy(callbacks, queueTerminate) + lock.Unlock() + for _, callback := range callbacks { callback() } |