Skip to content

Commit

Permalink
refactor: change DoAgain to DoForce
Browse files Browse the repository at this point in the history
Signed-off-by: m-murad <[email protected]>
  • Loading branch information
m-murad committed Aug 8, 2020
1 parent eb10a11 commit ec6abcf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
6 changes: 3 additions & 3 deletions once.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ func (o *Once) Do(f func()) {
}
}

// DoAgain will execute the function even if Do has already been called on Once.
// DoAgain will not call f directly because that will not be a thread safe call.
func (o *Once) DoAgain(f func()) {
// DoForce will execute the function even if Do has already been called on Once.
// It will not call f directly because that will not be a thread safe call.
func (o *Once) DoForce(f func()) {
atomic.StoreUint32(&o.done, 0)
o.doSlow(f)
}
Expand Down
42 changes: 21 additions & 21 deletions once_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,39 +55,39 @@ func TestOnce_Do(t *testing.T) {
}
}

func TestOnce_DoAgain(t *testing.T) {
func TestOnce_DoForce(t *testing.T) {
const (
Do = iota
DoAgain
DoForce
)
tt := []struct {
name string
funCalls []int
val int
shouldPass bool
}{
{"Func calls - [DoAgain]. Expect one", []int{DoAgain}, 1, true},
{"Func calls - [DoAgain]. Expect two", []int{DoAgain}, 2, false},
{"Func calls - [DoForce]. Expect one", []int{DoForce}, 1, true},
{"Func calls - [DoForce]. Expect two", []int{DoForce}, 2, false},

{"Func calls - [Do, DoAgain]. Expect one", []int{Do, DoAgain}, 1, false},
{"Func calls - [Do, DoAgain]. Expect two", []int{Do, DoAgain}, 2, true},
{"Func calls - [DoAgain, Do]. Expect one", []int{DoAgain, Do}, 1, true},
{"Func calls - [DoAgain, Do]. Expect two", []int{DoAgain, Do}, 2, false},
{"Func calls - [DoAgain, DoAgain]. Expect one", []int{DoAgain, DoAgain}, 1, false},
{"Func calls - [DoAgain, DoAgain]. Expect two", []int{DoAgain, DoAgain}, 2, true},
{"Func calls - [Do, DoForce]. Expect one", []int{Do, DoForce}, 1, false},
{"Func calls - [Do, DoForce]. Expect two", []int{Do, DoForce}, 2, true},
{"Func calls - [DoForce, Do]. Expect one", []int{DoForce, Do}, 1, true},
{"Func calls - [DoForce, Do]. Expect two", []int{DoForce, Do}, 2, false},
{"Func calls - [DoForce, DoForce]. Expect one", []int{DoForce, DoForce}, 1, false},
{"Func calls - [DoForce, DoForce]. Expect two", []int{DoForce, DoForce}, 2, true},

{"Func calls - [DoAgain, Do, Do]. Expect one", []int{DoAgain, Do, Do}, 1, true},
{"Func calls - [DoAgain, Do, Do]. Expect three", []int{DoAgain, Do, Do}, 3, false},
{"Func calls - [DoForce, Do, Do]. Expect one", []int{DoForce, Do, Do}, 1, true},
{"Func calls - [DoForce, Do, Do]. Expect three", []int{DoForce, Do, Do}, 3, false},

{"Func calls - [Do, DoAgain, Do]. Expect one", []int{Do, DoAgain, Do}, 1, false},
{"Func calls - [Do, DoAgain, Do]. Expect two", []int{Do, DoAgain, Do}, 2, true},
{"Func calls - [Do, DoAgain, Do]. Expect two", []int{Do, DoAgain, Do}, 3, false},
{"Func calls - [Do, DoForce, Do]. Expect one", []int{Do, DoForce, Do}, 1, false},
{"Func calls - [Do, DoForce, Do]. Expect two", []int{Do, DoForce, Do}, 2, true},
{"Func calls - [Do, DoForce, Do]. Expect two", []int{Do, DoForce, Do}, 3, false},

{"Func calls - [Do, DoAgain, DoAgain]. Expect three", []int{Do, DoAgain, DoAgain}, 3, true},
{"Func calls - [Do, DoAgain, DoAgain]. Expect two", []int{Do, DoAgain, DoAgain}, 2, false},
{"Func calls - [Do, DoForce, DoForce]. Expect three", []int{Do, DoForce, DoForce}, 3, true},
{"Func calls - [Do, DoForce, DoForce]. Expect two", []int{Do, DoForce, DoForce}, 2, false},

{"Func calls - [DoAgain, DoAgain, DoAgain]. Expect three", []int{DoAgain, DoAgain, DoAgain}, 3, true},
{"Func calls - [DoAgain, DoAgain, DoAgain]. Expect one", []int{DoAgain, DoAgain, DoAgain}, 1, false},
{"Func calls - [DoForce, DoForce, DoForce]. Expect three", []int{DoForce, DoForce, DoForce}, 3, true},
{"Func calls - [DoForce, DoForce, DoForce]. Expect one", []int{DoForce, DoForce, DoForce}, 1, false},
}

for _, tc := range tt {
Expand All @@ -99,8 +99,8 @@ func TestOnce_DoAgain(t *testing.T) {
switch tc.funCalls[i] {
case Do:
o.Do(func() { c.Increment() })
case DoAgain:
o.DoAgain(func() { c.Increment() })
case DoForce:
o.DoForce(func() { c.Increment() })
}
}

Expand Down
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

sync-once is similar to [sync.Once](https://golang.org/pkg/sync/#Once) of the standard library.

It also has struct `Once` but has two additional methods `DoAgain()` and `Reset()`.
It also has struct `Once` but has two additional methods `DoForce()` and `Reset()`.

### why

Expand All @@ -25,14 +25,14 @@ func main() {
// This will work similar to the Once.Do(f) method of the sync package. The function f is only called once
o.Do(loadConfig())
// To call the function either for the first time or again you need to use the sync.DoAgain() method
// To call the function either for the first time or again you need to use the sync.DoForce() method
// This will execute irrespective of weather o.Do() was called earlier or not and mark o (Once) as done.
// Call to o.Do() after o.DoAgain() will not execute the function.
o.DoAgain(loadConfig())
// Call to o.Do() after o.DoForce() will not execute the function.
o.DoForce(loadConfig())
// To reset o (sync.Once) you need to call the Reset() method.
// This will mark o as not executed but will no call the Do() method. You need to call Do() or DoAgain() after this.
// Calls to Do() and DoAgain() after this will work as described above.
// This will mark o as not executed but will no call the Do() method. You need to call Do() or DoForce() after this.
// Calls to Do() and DoForce() after this will work as described above.
o.Reset()
}
Expand Down

0 comments on commit ec6abcf

Please sign in to comment.