From 538e4c6d2c1dedb0590756dbaff162f28f807a01 Mon Sep 17 00:00:00 2001 From: jules00 Date: Mon, 27 Dec 2021 11:33:49 +0200 Subject: [PATCH] - renamed 2 functions from the atomic.Flag --- core/atomic/flag.go | 18 +++++++++--------- core/atomic/flag_test.go | 30 ++++++++++++++++++------------ 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/core/atomic/flag.go b/core/atomic/flag.go index f31ec4c19..daca6d4fe 100644 --- a/core/atomic/flag.go +++ b/core/atomic/flag.go @@ -7,14 +7,14 @@ type Flag struct { value uint32 } -// Set sets flag and returns its previous value -func (flag *Flag) Set() bool { +// SetReturningPrevious sets flag and returns its previous value +func (flag *Flag) SetReturningPrevious() bool { previousValue := atomic.SwapUint32(&flag.value, 1) return previousValue == 1 } -// Unset sets flag -func (flag *Flag) Unset() { +// Reset resets the flag, putting it in off position +func (flag *Flag) Reset() { atomic.StoreUint32(&flag.value, 0) } @@ -24,11 +24,11 @@ func (flag *Flag) IsSet() bool { return value == 1 } -// Toggle toggles the flag -func (flag *Flag) Toggle(set bool) { - if set { - flag.Set() +// SetValue sets the new value in the flag +func (flag *Flag) SetValue(newValue bool) { + if newValue { + _ = flag.SetReturningPrevious() } else { - flag.Unset() + flag.Reset() } } diff --git a/core/atomic/flag_test.go b/core/atomic/flag_test.go index ec4a4212c..82e9b13e2 100644 --- a/core/atomic/flag_test.go +++ b/core/atomic/flag_test.go @@ -7,7 +7,9 @@ import ( "github.com/stretchr/testify/require" ) -func TestFlag_Set(t *testing.T) { +func TestFlag_SetReturningPrevious(t *testing.T) { + t.Parallel() + var flag Flag var wg sync.WaitGroup @@ -16,12 +18,12 @@ func TestFlag_Set(t *testing.T) { wg.Add(2) go func() { - flag.Set() + _ = flag.SetReturningPrevious() wg.Done() }() go func() { - flag.Set() + _ = flag.SetReturningPrevious() wg.Done() }() @@ -29,22 +31,24 @@ func TestFlag_Set(t *testing.T) { require.True(t, flag.IsSet()) } -func TestFlag_Unset(t *testing.T) { +func TestFlag_Reset(t *testing.T) { + t.Parallel() + var flag Flag var wg sync.WaitGroup - flag.Set() + _ = flag.SetReturningPrevious() require.True(t, flag.IsSet()) wg.Add(2) go func() { - flag.Unset() + flag.Reset() wg.Done() }() go func() { - flag.Unset() + flag.Reset() wg.Done() }() @@ -52,7 +56,9 @@ func TestFlag_Unset(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 +66,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,12 +82,12 @@ 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() }()