-
Notifications
You must be signed in to change notification settings - Fork 6
/
multi_thread_safe_test.go
48 lines (38 loc) · 985 Bytes
/
multi_thread_safe_test.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
package movavg_test
import (
"sync"
. "github.com/mxmCherry/movavg"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("MultiThreadSafe", func() {
It("should use underlying MultiMA object", func() {
underlying := &mockMultiMA{
addRes: []float64{0.1, 0.2, 0.3}, // dummy
avgRes: []float64{0.4, 0.5, 0.6}, // dummy
}
subject := MultiThreadSafe(underlying)
vsets := [][]float64{
{1, 1, 1, 1},
{2, 2, 2, 2},
}
var wg sync.WaitGroup
for _, vset := range vsets {
wg.Add(1)
go func(vset []float64) {
for _, v := range vset {
subject.Add(v)
}
wg.Done()
}(vset)
}
wg.Wait()
Expect(subject.Add(3)).To(Equal([]float64{0.1, 0.2, 0.3})) // dummy
Expect(subject.Avg()).To(Equal([]float64{0.4, 0.5, 0.6})) // dummy
Expect(underlying.addArgs).To(ConsistOf([]float64{
1, 1, 1, 1, // four ones (added concurrently)
2, 2, 2, 2, // four twos (added concurrently)
3, // added last
}))
})
})