quintodrome/utils/pool/pool_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

57 lines
970 B
Go
Raw Normal View History

package pool
import (
2021-02-19 15:36:55 -09:00
"sync"
"testing"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/tests"
2022-07-26 12:47:16 -08:00
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
2021-02-04 11:44:32 -09:00
func TestPool(t *testing.T) {
tests.Init(t, false)
log.SetLevel(log.LevelCritical)
RegisterFailHandler(Fail)
2021-02-04 11:44:32 -09:00
RunSpecs(t, "Pool Suite")
}
2020-10-27 08:27:26 -08:00
type testItem struct {
ID int
}
2021-02-19 15:36:55 -09:00
var (
processed []int
mutex sync.RWMutex
)
2020-10-27 08:27:26 -08:00
var _ = Describe("Pool", func() {
2020-10-27 08:27:26 -08:00
var pool *Pool
BeforeEach(func() {
processed = nil
2020-12-15 16:46:52 -09:00
pool, _ = NewPool("test", 2, execute)
2020-10-27 08:27:26 -08:00
})
2020-10-27 08:27:26 -08:00
It("processes items", func() {
for i := 0; i < 5; i++ {
pool.Submit(&testItem{ID: i})
}
2021-02-19 15:36:55 -09:00
Eventually(func() []int {
mutex.RLock()
defer mutex.RUnlock()
return processed
}, "10s").Should(HaveLen(5))
2020-10-27 08:27:26 -08:00
Expect(processed).To(ContainElements(0, 1, 2, 3, 4))
})
})
2020-10-27 08:27:26 -08:00
func execute(workload interface{}) {
2021-02-19 15:36:55 -09:00
mutex.Lock()
defer mutex.Unlock()
2020-10-27 08:27:26 -08:00
item := workload.(*testItem)
processed = append(processed, item.ID)
}