forked from tehsphinx/concurrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbool_test.go
78 lines (67 loc) · 1.14 KB
/
bool_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package concurrent
import (
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBool(t *testing.T) {
s := NewBool()
s.Set(false)
assert.Equal(t, false, s.Get())
s.Set(true)
assert.Equal(t, true, s.Get())
s.Set(false)
assert.Equal(t, false, s.Get())
s.Set(false)
assert.Equal(t, false, s.Get())
s.Set(true)
assert.Equal(t, true, s.Get())
}
func TestBoolConcurrent(t *testing.T) {
s := NewBool()
wg := sync.WaitGroup{}
wg.Add(3)
go func() {
for i := 0; i < 1<<30; i++ {
s.Get()
}
wg.Done()
}()
for i := 0; i < 2; i++ {
go func(b bool) {
for i := 0; i < 1<<20; i++ {
s.Set(b)
}
wg.Done()
}(i%2 == 0)
}
wg.Wait()
}
func BenchmarkBool_Get(b *testing.B) {
s := NewBool()
wg := &sync.WaitGroup{}
wg.Add(10)
for i := 0; i < 10; i++ {
go func(wg *sync.WaitGroup) {
for i := 0; i < b.N/10; i++ {
s.Get()
}
wg.Done()
}(wg)
}
wg.Wait()
}
func BenchmarkBool_Set(b *testing.B) {
s := NewBool()
wg := &sync.WaitGroup{}
wg.Add(10)
for i := 0; i < 10; i++ {
go func(wg *sync.WaitGroup) {
for i := 0; i < b.N/10; i++ {
s.Set(i%2 == 0)
}
wg.Done()
}(wg)
}
wg.Wait()
}