Skip to content

Commit

Permalink
Merge pull request #51 from ElrondNetwork/merge-main-feat-scheduled-r…
Browse files Browse the repository at this point in the history
…evert-flags

Merge main feat scheduled revert flags
  • Loading branch information
AdoAdoAdo authored Dec 27, 2021
2 parents 1d90a23 + 296f339 commit dba9fb9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
18 changes: 9 additions & 9 deletions core/atomic/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ type Flag struct {
value uint32
}

// SetReturningPrevious sets flag and returns its previous value
func (flag *Flag) SetReturningPrevious() bool {
// Set sets flag and returns its previous value
func (flag *Flag) Set() bool {
previousValue := atomic.SwapUint32(&flag.value, 1)
return previousValue == 1
}

// Reset resets the flag, putting it in off position
func (flag *Flag) Reset() {
// Unset sets flag
func (flag *Flag) Unset() {
atomic.StoreUint32(&flag.value, 0)
}

Expand All @@ -24,11 +24,11 @@ func (flag *Flag) IsSet() bool {
return value == 1
}

// SetValue sets the new value in the flag
func (flag *Flag) SetValue(newValue bool) {
if newValue {
_ = flag.SetReturningPrevious()
// Toggle toggles the flag
func (flag *Flag) Toggle(set bool) {
if set {
flag.Set()
} else {
flag.Reset()
flag.Unset()
}
}
28 changes: 11 additions & 17 deletions core/atomic/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
)

func TestFlag_Set(t *testing.T) {
t.Parallel()

var flag Flag
var wg sync.WaitGroup

Expand All @@ -18,60 +16,56 @@ func TestFlag_Set(t *testing.T) {
wg.Add(2)

go func() {
_ = flag.SetReturningPrevious()
flag.Set()
wg.Done()
}()

go func() {
_ = flag.SetReturningPrevious()
flag.Set()
wg.Done()
}()

wg.Wait()
require.True(t, flag.IsSet())
}

func TestFlag_Reset(t *testing.T) {
t.Parallel()

func TestFlag_Unset(t *testing.T) {
var flag Flag
var wg sync.WaitGroup

_ = flag.SetReturningPrevious()
flag.Set()
require.True(t, flag.IsSet())

wg.Add(2)

go func() {
flag.Reset()
flag.Unset()
wg.Done()
}()

go func() {
flag.Reset()
flag.Unset()
wg.Done()
}()

wg.Wait()
require.False(t, flag.IsSet())
}

func TestFlag_SetValue(t *testing.T) {
t.Parallel()

func TestFlag_Toggle(t *testing.T) {
var flag Flag
var wg sync.WaitGroup

// First, Toggle(true)
wg.Add(2)

go func() {
flag.SetValue(true)
flag.Toggle(true)
wg.Done()
}()

go func() {
flag.SetValue(true)
flag.Toggle(true)
wg.Done()
}()

Expand All @@ -82,12 +76,12 @@ func TestFlag_SetValue(t *testing.T) {
wg.Add(2)

go func() {
flag.SetValue(false)
flag.Toggle(false)
wg.Done()
}()

go func() {
flag.SetValue(false)
flag.Toggle(false)
wg.Done()
}()

Expand Down

0 comments on commit dba9fb9

Please sign in to comment.