-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathonce_test.go
157 lines (135 loc) · 4.1 KB
/
once_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package once_test
import (
sync "github.com/m-murad/sync-once"
"testing"
)
type counter int
func (c *counter) Increment() {
*c++
}
func (c *counter) Value() int {
return int(*c)
}
func TestOnce_Do(t *testing.T) {
tt := []struct {
name string
calls int
val int
shouldPass bool
}{
{"dont call expect zero", 0, 0, true},
{"dont call expect one", 0, 1, false},
{"call once expect one", 1, 1, true},
{"call thrice expect one", 3, 1, true},
{"call twice expect two", 2, 2, false},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
c := new(counter)
o := new(sync.Once)
ch := make(chan bool)
for i := 0; i < tc.calls; i++ {
go func() {
o.Do(func() { c.Increment() })
ch <- true
}()
}
for i := 0; i < tc.calls; i++ {
<- ch
}
res := c.Value()
if (tc.val == res && tc.shouldPass) || (tc.val != res && !tc.shouldPass) {
t.Logf(`Test "%s" paassed`, tc.name)
} else {
t.Fatalf(`Test "%s" failed. Expected %d received %d`, tc.name, tc.val, res)
}
})
}
}
func TestOnce_DoForce(t *testing.T) {
const (
Do = iota
DoForce
)
tt := []struct {
name string
funCalls []int
val int
shouldPass bool
}{
{"Func calls - [DoForce]. Expect one", []int{DoForce}, 1, true},
{"Func calls - [DoForce]. Expect two", []int{DoForce}, 2, false},
{"Func calls - [Do, DoForce]. Expect one", []int{Do, DoForce}, 1, false},
{"Func calls - [Do, DoForce]. Expect two", []int{Do, DoForce}, 2, true},
{"Func calls - [DoForce, Do]. Expect one", []int{DoForce, Do}, 1, true},
{"Func calls - [DoForce, Do]. Expect two", []int{DoForce, Do}, 2, false},
{"Func calls - [DoForce, DoForce]. Expect one", []int{DoForce, DoForce}, 1, false},
{"Func calls - [DoForce, DoForce]. Expect two", []int{DoForce, DoForce}, 2, true},
{"Func calls - [DoForce, Do, Do]. Expect one", []int{DoForce, Do, Do}, 1, true},
{"Func calls - [DoForce, Do, Do]. Expect three", []int{DoForce, Do, Do}, 3, false},
{"Func calls - [Do, DoForce, Do]. Expect one", []int{Do, DoForce, Do}, 1, false},
{"Func calls - [Do, DoForce, Do]. Expect two", []int{Do, DoForce, Do}, 2, true},
{"Func calls - [Do, DoForce, Do]. Expect two", []int{Do, DoForce, Do}, 3, false},
{"Func calls - [Do, DoForce, DoForce]. Expect three", []int{Do, DoForce, DoForce}, 3, true},
{"Func calls - [Do, DoForce, DoForce]. Expect two", []int{Do, DoForce, DoForce}, 2, false},
{"Func calls - [DoForce, DoForce, DoForce]. Expect three", []int{DoForce, DoForce, DoForce}, 3, true},
{"Func calls - [DoForce, DoForce, DoForce]. Expect one", []int{DoForce, DoForce, DoForce}, 1, false},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
c := new(counter)
o := new(sync.Once)
for i := 0; i < len(tc.funCalls); i++ {
switch tc.funCalls[i] {
case Do:
o.Do(func() { c.Increment() })
case DoForce:
o.DoForce(func() { c.Increment() })
}
}
res := c.Value()
if (tc.val == res && tc.shouldPass) || (tc.val != res && !tc.shouldPass) {
t.Logf(`Test "%s" paassed`, tc.name)
} else {
t.Fatalf(`Test "%s" failed. Expected %d received %d`, tc.name, tc.val, res)
}
})
}
}
func TestOnce_Reset(t *testing.T) {
const (
Do = iota
Reset
)
tt := []struct {
name string
funCalls []int
val int
shouldPass bool
}{
{"Func calls - [Do, Do]. Expect one", []int{Do, Do}, 1, true},
{"Func calls - [Do, Do]. Expect two", []int{Do, Do}, 2, false},
{"Func calls - [Do, Reset, Do]. Expect two", []int{Do, Reset, Do}, 2, true},
{"Func calls - [Do, Reset, Do]. Expect one", []int{Do, Reset, Do}, 1, false},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
c := new(counter)
o := new(sync.Once)
for i := 0; i < len(tc.funCalls); i++ {
switch tc.funCalls[i] {
case Do:
o.Do(func() { c.Increment() })
case Reset:
o.Reset()
}
}
res := c.Value()
if (tc.val == res && tc.shouldPass) || (tc.val != res && !tc.shouldPass) {
t.Logf(`Test "%s" paassed`, tc.name)
} else {
t.Fatalf(`Test "%s" failed. Expected %d received %d`, tc.name, tc.val, res)
}
})
}
}