abool/bool_test.go

215 lines
3.2 KiB
Go
Raw Normal View History

2016-05-25 06:42:19 +03:00
package abool
import (
"sync"
"sync/atomic"
"testing"
)
func TestBool(t *testing.T) {
2016-05-25 10:06:49 +03:00
v := NewBool(true)
if !v.IsSet() {
t.Fatal("NewValue(true) failed")
}
v = NewBool(false)
if v.IsSet() {
t.Fatal("NewValue(false) failed")
}
v = New()
2016-05-25 06:42:19 +03:00
if v.IsSet() {
t.Fatal("Empty value of AtomicBool should be false")
}
v.Set()
if !v.IsSet() {
t.Fatal("AtomicBool.Set() failed")
}
v.UnSet()
if v.IsSet() {
t.Fatal("AtomicBool.UnSet() failed")
}
v.SetTo(true)
if !v.IsSet() {
t.Fatal("AtomicBool.SetTo(true) failed")
}
v.SetTo(false)
if v.IsSet() {
t.Fatal("AtomicBool.SetTo(false) failed")
}
2016-06-02 06:47:41 +03:00
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")
}
2018-09-07 17:17:26 +03:00
_ = v.Toggle() // expected false
2018-09-07 17:17:26 +03:00
if v.IsSet() {
t.Fatal("AtomicBool.Toggle() failed")
2018-09-07 17:17:26 +03:00
}
2016-05-25 06:42:19 +03:00
}
2016-05-25 09:57:10 +03:00
func TestRace(t *testing.T) {
repeat := 10000
var wg sync.WaitGroup
2018-09-07 17:17:26 +03:00
wg.Add(repeat * 4)
2016-05-25 09:57:10 +03:00
v := New()
2016-05-25 10:06:49 +03:00
// Writer
2016-05-25 09:57:10 +03:00
go func() {
for i := 0; i < repeat; i++ {
v.Set()
wg.Done()
}
}()
2016-05-25 10:06:49 +03:00
// Reader
2016-05-25 09:57:10 +03:00
go func() {
for i := 0; i < repeat; i++ {
v.IsSet()
wg.Done()
}
}()
2016-05-25 10:06:49 +03:00
// Writer
2016-05-25 09:57:10 +03:00
go func() {
for i := 0; i < repeat; i++ {
v.UnSet()
wg.Done()
}
}()
2018-09-07 17:17:26 +03:00
// Reader And Writer
go func() {
for i := 0; i < repeat; i++ {
v.Toggle()
2018-09-07 17:17:26 +03:00
wg.Done()
}
}()
2016-05-25 09:57:10 +03:00
wg.Wait()
}
2016-05-25 09:16:55 +03:00
func ExampleAtomicBool() {
cond := New() // default to false
cond.Set() // set to true
cond.IsSet() // returns true
cond.UnSet() // set to false
cond.SetTo(true) // set to whatever you want
cond.Toggle() // toggles the boolean value
2016-05-25 09:16:55 +03:00
}
2016-05-25 06:42:19 +03:00
// 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()
}
}
2016-06-02 06:47:41 +03:00
// 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()
}
b.StopTimer()
}
func BenchmarkAtomicBoolCAS(b *testing.B) {
v := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
v.SetToIf(false, true)
}
}
2018-09-07 17:17:26 +03:00
// Benchmark toggle boolean value
2018-09-07 17:17:26 +03:00
func BenchmarkMutexToggle(b *testing.B) {
2018-09-07 17:17:26 +03:00
var m sync.RWMutex
var v bool
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Lock()
v = !v
m.Unlock()
}
b.StopTimer()
}
func BenchmarkAtomicBoolToggle(b *testing.B) {
2018-09-07 17:17:26 +03:00
v := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
v.Toggle()
2018-09-07 17:17:26 +03:00
}
}