-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathatomic-bool_test.go
56 lines (48 loc) · 909 Bytes
/
atomic-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
package ewp
import (
"testing"
"time"
)
func TestAtomicBool_Single(t *testing.T) {
ab := &AtomicBool{}
if b := ab.Get(); b {
t.Fatalf("1. Expected init false. Got %v", b)
}
ab.Set(true)
if b := ab.Get(); !b {
t.Fatalf("2. Expected set true. Got %v", b)
}
ab.Set(false)
if b := ab.Get(); b {
t.Fatalf("2. Expected set false. Got %v", b)
}
}
var _tmpAB bool
func TestAtomicBool_Concurrent(t *testing.T) {
defer func() {
if err := recover(); err != nil {
t.Fatalf("1. Expected no race condition, caught: %v", err)
}
}()
ab, workerNum, runNum := &AtomicBool{}, 4, 10000000
for i := 0; i < workerNum; i++ {
go func() {
for j := 0; j < runNum; j++ {
if j%2 == 0 {
ab.Set(true)
} else {
ab.Set(false)
}
}
}()
}
go func() {
for i := 0; i < runNum; i++ {
_tmpAB = ab.Get()
}
}()
select {
case <-time.After(3 * time.Second):
return
}
}