From 6e6b80cf11bfcaf78a338ab0d3c085785a7bfed6 Mon Sep 17 00:00:00 2001 From: Tevin Zhang Date: Wed, 25 May 2016 14:57:10 +0800 Subject: [PATCH] Add race test --- bool.go | 18 +++++++++--------- bool_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/bool.go b/bool.go index c08570d..8d86be3 100644 --- a/bool.go +++ b/bool.go @@ -14,15 +14,6 @@ func New() *AtomicBool { // *AtomicBool to avoid copy type AtomicBool int32 -// SetTo sets the boolean with given bool -func (ab *AtomicBool) SetTo(yes bool) { - if yes { - atomic.StoreInt32((*int32)(ab), 1) - } else { - atomic.StoreInt32((*int32)(ab), 0) - } -} - // Set sets the bool to true func (ab *AtomicBool) Set() { atomic.StoreInt32((*int32)(ab), 1) @@ -37,3 +28,12 @@ func (ab *AtomicBool) UnSet() { func (ab *AtomicBool) IsSet() bool { return atomic.LoadInt32((*int32)(ab)) == 1 } + +// SetTo sets the boolean with given bool +func (ab *AtomicBool) SetTo(yes bool) { + if yes { + atomic.StoreInt32((*int32)(ab), 1) + } else { + atomic.StoreInt32((*int32)(ab), 0) + } +} diff --git a/bool_test.go b/bool_test.go index 62d7463..060fe3b 100644 --- a/bool_test.go +++ b/bool_test.go @@ -33,6 +33,34 @@ func TestBool(t *testing.T) { } } +func TestRace(t *testing.T) { + repeat := 10000 + var wg sync.WaitGroup + wg.Add(repeat * 3) + v := New() + go func() { + for i := 0; i < repeat; i++ { + v.Set() + wg.Done() + } + }() + + go func() { + for i := 0; i < repeat; i++ { + v.IsSet() + wg.Done() + } + }() + + go func() { + for i := 0; i < repeat; i++ { + v.UnSet() + wg.Done() + } + }() + wg.Wait() +} + func ExampleAtomicBool() { cond := New() // default to false cond.Set() // set to true