/
concurrency.go
50 lines (39 loc) · 962 Bytes
/
concurrency.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package testsupport
import (
"sync"
. "github.com/onsi/ginkgo/v2"
)
type ParallelRunner struct {
NumWorkers int
}
func (p *ParallelRunner) RunOnSlice(items []interface{}, workFunc func(item interface{})) {
queue := make(chan interface{})
go func() {
for _, item := range items {
queue <- item
}
close(queue)
}()
p.RunOnChannel(queue, workFunc)
}
func (p *ParallelRunner) RunOnChannel(queue chan interface{}, workFunc func(item interface{})) {
var wg sync.WaitGroup
for i := 0; i < p.NumWorkers; i++ {
wg.Add(1)
go func() {
defer GinkgoRecover() //not tested
defer wg.Done()
for item := range queue {
workFunc(item)
}
}()
}
wg.Wait()
}
func (p *ParallelRunner) RunOnSliceStrings(someStrings []string, workFunc func(aString string)) {
items := []interface{}{}
for _, aString := range someStrings {
items = append(items, aString)
}
p.RunOnSlice(items, func(item interface{}) { workFunc(item.(string)) })
}