-
Notifications
You must be signed in to change notification settings - Fork 25
/
bool_test.go
326 lines (276 loc) · 6.12 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package abool
import (
"encoding/json"
"sync"
"sync/atomic"
"testing"
)
func TestDefaultValue(t *testing.T) {
t.Parallel()
v := New()
if v.IsSet() {
t.Fatal("Empty value of AtomicBool should be false")
}
v = NewBool(true)
if !v.IsSet() {
t.Fatal("NewValue(true) should be true")
}
v = NewBool(false)
if v.IsSet() {
t.Fatal("NewValue(false) should be false")
}
}
func TestIsNotSet(t *testing.T) {
t.Parallel()
v := New()
if v.IsSet() == v.IsNotSet() {
t.Fatal("AtomicBool.IsNotSet() should be the opposite of IsSet()")
}
}
func TestSetUnSet(t *testing.T) {
t.Parallel()
v := New()
v.Set()
if !v.IsSet() {
t.Fatal("AtomicBool.Set() failed")
}
v.UnSet()
if v.IsSet() {
t.Fatal("AtomicBool.UnSet() failed")
}
}
func TestSetTo(t *testing.T) {
t.Parallel()
v := New()
v.SetTo(true)
if !v.IsSet() {
t.Fatal("AtomicBool.SetTo(true) failed")
}
v.SetTo(false)
if v.IsSet() {
t.Fatal("AtomicBool.SetTo(false) failed")
}
if set := v.SetToIf(true, false); set || v.IsSet() {
t.Fatal("AtomicBool.SetTo(true, false) failed")
}
if set := v.SetToIf(false, true); !set || !v.IsSet() {
t.Fatal("AtomicBool.SetTo(false, true) failed")
}
}
func TestToggle(t *testing.T) {
t.Parallel()
v := New()
_ = v.Toggle()
if !v.IsSet() {
t.Fatal("AtomicBool.Toggle() to true failed")
}
prev := v.Toggle()
if v.IsSet() == prev {
t.Fatal("AtomicBool.Toggle() to false failed")
}
}
func TestToogleMultipleTimes(t *testing.T) {
t.Parallel()
v := New()
pre := !v.IsSet()
for i := 0; i < 100; i++ {
v.SetTo(false)
for j := 0; j < i; j++ {
pre = v.Toggle()
}
expected := i%2 != 0
if v.IsSet() != expected {
t.Fatalf("AtomicBool.Toogle() doesn't work after %d calls, expected: %v, got %v", i, expected, v.IsSet())
}
if pre == v.IsSet() {
t.Fatalf("AtomicBool.Toogle() returned wrong value at the %dth calls, expected: %v, got %v", i, !v.IsSet(), pre)
}
}
}
func TestRace(t *testing.T) {
repeat := 10000
var wg sync.WaitGroup
wg.Add(repeat * 3)
v := New()
// Writer
go func() {
for i := 0; i < repeat; i++ {
v.Set()
wg.Done()
}
}()
// Reader
go func() {
for i := 0; i < repeat; i++ {
v.IsSet()
wg.Done()
}
}()
// Writer
go func() {
for i := 0; i < repeat; i++ {
v.UnSet()
wg.Done()
}
}()
wg.Wait()
}
func TestSetToIfAfterMultipleToggles(t *testing.T) {
v := New() // false
v.Toggle() // true
v.Toggle() // false
v.Toggle() // true
// As v is true, it should now be flipped to false
v.SetToIf(true, false)
expected := false
if v.IsSet() != expected {
t.Fatalf("Toggling the value atleast 3 times, until it's true, `SetToIf(true, false)` should flip v to false, expected: %v, got %v", expected, v.IsSet())
}
}
func TestJSONCompatibleWithBuiltinBool(t *testing.T) {
for _, value := range []bool{true, false} {
// Test bool -> bytes -> AtomicBool
// act 1. bool -> bytes
buf, err := json.Marshal(value)
if err != nil {
t.Fatalf("json.Marshal(%t) failed: %s", value, err)
}
// act 2. bytes -> AtomicBool
//
// Try to unmarshall the JSON byte slice
// of a normal boolean into an AtomicBool
//
// Create an AtomicBool with the oppsite default to ensure the unmarshal process did the work
ab := NewBool(!value)
err = json.Unmarshal(buf, ab)
if err != nil {
t.Fatalf(`json.Unmarshal("%s", %T) failed: %s`, buf, ab, err)
}
// assert
if ab.IsSet() != value {
t.Fatalf("Expected AtomicBool to represent %t but actual value was %t", value, ab.IsSet())
}
// Test AtomicBool -> bytes -> bool
// act 3. AtomicBool -> bytes
buf, err = json.Marshal(ab)
if err != nil {
t.Fatalf("json.Marshal(%T) failed: %s", ab, err)
}
// using the opposite value for the same reason as the former case
b := ab.IsNotSet()
// act 4. bytes -> bool
err = json.Unmarshal(buf, &b)
if err != nil {
t.Fatalf(`json.Unmarshal("%s", %T) failed: %s`, buf, &b, err)
}
// assert
if b != ab.IsSet() {
t.Fatalf(`json.Unmarshal("%s", %T) didn't work, expected %t, got %t`, buf, ab, ab.IsSet(), b)
}
}
}
func TestUnmarshalJSONErrorNoWrite(t *testing.T) {
for _, val := range []bool{true, false} {
ab := NewBool(val)
oldVal := ab.IsSet()
buf := []byte("invalid-json")
err := json.Unmarshal(buf, ab)
if err == nil {
t.Fatalf(`Error expected from json.Unmarshal("%s", %T)`, buf, ab)
}
if oldVal != ab.IsSet() {
t.Fatal("Failed json.Unmarshal modified the value of AtomicBool which is not expected")
}
}
}
func ExampleAtomicBool() {
cond := New() // default to false
any := true
old := any
new := !any
cond.Set() // Sets to true
cond.IsSet() // Returns true
cond.UnSet() // Sets to false
cond.IsNotSet() // Returns true
cond.SetTo(any) // Sets to whatever you want
cond.SetToIf(new, old) // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded
}
func BenchmarkAtomicBoolToggle(b *testing.B) {
v := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = v.Toggle()
}
}
// Benchmark Read
func BenchmarkMutexRead(b *testing.B) {
var m sync.RWMutex
var v bool
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.RLock()
_ = v
m.RUnlock()
}
}
func BenchmarkAtomicValueRead(b *testing.B) {
var v atomic.Value
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = v.Load() != nil
}
}
func BenchmarkAtomicBoolRead(b *testing.B) {
v := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = v.IsSet()
}
}
// Benchmark Write
func BenchmarkMutexWrite(b *testing.B) {
var m sync.RWMutex
var v bool
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.RLock()
v = true
m.RUnlock()
}
b.StopTimer()
_ = v
}
func BenchmarkAtomicValueWrite(b *testing.B) {
var v atomic.Value
b.ResetTimer()
for i := 0; i < b.N; i++ {
v.Store(true)
}
}
func BenchmarkAtomicBoolWrite(b *testing.B) {
v := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
v.Set()
}
}
// Benchmark CAS
func BenchmarkMutexCAS(b *testing.B) {
var m sync.RWMutex
var v bool
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Lock()
if !v {
v = true
}
m.Unlock()
}
}
func BenchmarkAtomicBoolCAS(b *testing.B) {
v := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
v.SetToIf(false, true)
}
}