Fix race condition in test

This commit is contained in:
Deluan
2021-02-19 19:36:55 -05:00
parent 64ceb5371b
commit a140c222c2
+13 -2
View File
@@ -1,6 +1,7 @@
package pool package pool
import ( import (
"sync"
"testing" "testing"
"github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/log"
@@ -20,7 +21,10 @@ type testItem struct {
ID int ID int
} }
var processed []int var (
processed []int
mutex sync.RWMutex
)
var _ = Describe("Pool", func() { var _ = Describe("Pool", func() {
var pool *Pool var pool *Pool
@@ -34,12 +38,19 @@ var _ = Describe("Pool", func() {
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
pool.Submit(&testItem{ID: i}) pool.Submit(&testItem{ID: i})
} }
Eventually(func() []int { return processed }, "10s").Should(HaveLen(5)) Eventually(func() []int {
mutex.RLock()
defer mutex.RUnlock()
return processed
}, "10s").Should(HaveLen(5))
Expect(processed).To(ContainElements(0, 1, 2, 3, 4)) Expect(processed).To(ContainElements(0, 1, 2, 3, 4))
}) })
}) })
func execute(workload interface{}) { func execute(workload interface{}) {
mutex.Lock()
defer mutex.Unlock()
item := workload.(*testItem) item := workload.(*testItem)
processed = append(processed, item.ID) processed = append(processed, item.ID)
} }