Skip to content

Commit

Permalink
test: add test cases for Reset method
Browse files Browse the repository at this point in the history
Signed-off-by: m-murad <[email protected]>
  • Loading branch information
m-murad committed Jul 22, 2020
1 parent 96be156 commit b6ccc92
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions once_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,44 @@ func TestOnce_DoAgain(t *testing.T) {
}

}

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

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
c := new(counter)
o := new(sync.Once)

for i := 0; i < len(tc.funCalls); i++ {
switch tc.funCalls[i] {
case Do:
o.Do(func() { c.Increment() })
case Reset:
o.Reset()
}
}

res := c.Value()
if (tc.val == res && tc.shouldPass) || (tc.val != res && !tc.shouldPass) {
t.Logf(`Test "%s" paassed`, tc.name)
} else {
t.Fatalf(`Test "%s" failed. Expected %d received %d`, tc.name, tc.val, res)
}
})
}
}

0 comments on commit b6ccc92

Please sign in to comment.