From 7a7b23c5a5f1487a0c7c52e5ea5ec759569a9cf7 Mon Sep 17 00:00:00 2001 From: jules00 Date: Wed, 22 Dec 2021 10:33:25 +0200 Subject: [PATCH] - more refactoring work --- core/atomic/flag.go | 42 +++++++++++++++++++++++++++------------- core/atomic/flag_test.go | 39 ++++++++++++++++++++++++++++++++----- 2 files changed, 63 insertions(+), 18 deletions(-) diff --git a/core/atomic/flag.go b/core/atomic/flag.go index d8903f5a6..5d05449ca 100644 --- a/core/atomic/flag.go +++ b/core/atomic/flag.go @@ -1,34 +1,50 @@ package atomic -import "sync/atomic" +import ( + "sync" +) // Flag is an atomic flag type Flag struct { - value uint32 + mut sync.RWMutex + value bool } // SetReturningPrevious sets flag and returns its previous value func (flag *Flag) SetReturningPrevious() bool { - previousValue := atomic.SwapUint32(&flag.value, 1) - return previousValue == 1 + flag.mut.Lock() + previousValue := flag.value + flag.value = true + flag.mut.Unlock() + + return previousValue } // Reset resets the flag, putting it in off position func (flag *Flag) Reset() { - atomic.StoreUint32(&flag.value, 0) + flag.mut.Lock() + flag.value = false + flag.mut.Unlock() } // IsSet checks whether flag is set func (flag *Flag) IsSet() bool { - value := atomic.LoadUint32(&flag.value) - return value == 1 + flag.mut.RLock() + defer flag.mut.RUnlock() + + return flag.value } // Toggle toggles the flag -func (flag *Flag) Toggle(set bool) { - if set { - _ = flag.SetReturningPrevious() - } else { - flag.Reset() - } +func (flag *Flag) Toggle() { + flag.mut.Lock() + flag.value = !flag.value + flag.mut.Unlock() +} + +// SetValue sets the new value in the flag +func (flag *Flag) SetValue(newValue bool) { + flag.mut.Lock() + flag.value = newValue + flag.mut.Unlock() } diff --git a/core/atomic/flag_test.go b/core/atomic/flag_test.go index 276066444..6573c89e9 100644 --- a/core/atomic/flag_test.go +++ b/core/atomic/flag_test.go @@ -1,6 +1,7 @@ package atomic import ( + "github.com/stretchr/testify/assert" "sync" "testing" @@ -8,6 +9,8 @@ import ( ) func TestFlag_Set(t *testing.T) { + t.Parallel() + var flag Flag var wg sync.WaitGroup @@ -30,6 +33,8 @@ func TestFlag_Set(t *testing.T) { } func TestFlag_Reset(t *testing.T) { + t.Parallel() + var flag Flag var wg sync.WaitGroup @@ -52,7 +57,9 @@ func TestFlag_Reset(t *testing.T) { require.False(t, flag.IsSet()) } -func TestFlag_Toggle(t *testing.T) { +func TestFlag_SetValue(t *testing.T) { + t.Parallel() + var flag Flag var wg sync.WaitGroup @@ -60,12 +67,12 @@ func TestFlag_Toggle(t *testing.T) { wg.Add(2) go func() { - flag.Toggle(true) + flag.SetValue(true) wg.Done() }() go func() { - flag.Toggle(true) + flag.SetValue(true) wg.Done() }() @@ -76,15 +83,37 @@ func TestFlag_Toggle(t *testing.T) { wg.Add(2) go func() { - flag.Toggle(false) + flag.SetValue(false) wg.Done() }() go func() { - flag.Toggle(false) + flag.SetValue(false) wg.Done() }() wg.Wait() require.False(t, flag.IsSet()) } + +func TestFlag_Toggle(t *testing.T) { + t.Parallel() + + f := Flag{} + f.SetValue(true) + + numToggles := 51 + wg := sync.WaitGroup{} + wg.Add(numToggles) + for i := 0; i < numToggles; i++ { + go func() { + f.Toggle() + + wg.Done() + }() + } + + wg.Wait() + + assert.False(t, f.IsSet()) +}