Skip to content

Commit

Permalink
feat(testutil): add unit tests and remove unused functions
Browse files Browse the repository at this point in the history
- Added `pkg/util/testutil/testutil_test.go` for unit testing.
- Removed unused functions from `pkg/util/testutil/testutil.go`.
  • Loading branch information
ijsong committed Feb 3, 2025
1 parent 3821d9b commit c9d5fa8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 58 deletions.
58 changes: 0 additions & 58 deletions pkg/util/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"strings"
"time"

"github.com/pkg/errors"

"github.com/kakao/varlog/internal/vtesting"
)

Expand Down Expand Up @@ -36,62 +34,6 @@ func CompareWaitN(factor int64, cmp func() bool) bool {
return CompareWait(cmp, vtesting.TimeoutUnitTimesFactor(factor))
}

func CompareWait100(cmp func() bool) bool {
return CompareWaitN(100, cmp)
}

func CompareWait10(cmp func() bool) bool {
return CompareWaitN(10, cmp)
}

func CompareWait1(cmp func() bool) bool {
return CompareWaitN(1, cmp)
}

func CompareWaitErrorWithRetryInterval(cmp func() (bool, error), timeout time.Duration, retryInterval time.Duration) error {
after := time.NewTimer(timeout)
defer after.Stop()

numTries := 0
for {
select {
case <-after.C:
return errors.Errorf("compare wait timeout (%s,tries=%d)", timeout.String(), numTries)
default:
numTries++
ok, err := cmp()
if err != nil {
return err
}

if ok {
return nil
}
time.Sleep(retryInterval)
}
}
}

func CompareWaitError(cmp func() (bool, error), timeout time.Duration) error {
return CompareWaitErrorWithRetryInterval(cmp, timeout, time.Millisecond)
}

func CompareWaitErrorWithRetryIntervalN(factor int64, retryInterval time.Duration, cmp func() (bool, error)) error {
if factor < 1 {
factor = 1
}

return CompareWaitErrorWithRetryInterval(cmp, vtesting.TimeoutUnitTimesFactor(factor), retryInterval)
}

func CompareWaitErrorN(factor int64, cmp func() (bool, error)) error {
if factor < 1 {
factor = 1
}

return CompareWaitError(cmp, vtesting.TimeoutUnitTimesFactor(factor))
}

func GetFunctionName(i interface{}) string {
a := runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
s := strings.Split(a, "/")
Expand Down
62 changes: 62 additions & 0 deletions pkg/util/testutil/testutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package testutil_test

import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/kakao/varlog/pkg/util/testutil"
)

func TestCompareWait(t *testing.T) {
tcs := []struct {
cmp func() bool
want bool
}{
{cmp: func() bool { return true }, want: true},
{cmp: func() bool { return false }, want: false},
}

for _, tc := range tcs {
got := testutil.CompareWait(tc.cmp, time.Second)
require.Equal(t, tc.want, got)
}
}

func TestCompareWaitN_Factor0(t *testing.T) {
ts := time.Now()
testutil.CompareWaitN(0, func() bool {
return false
})
factor0 := time.Since(ts)

ts = time.Now()
testutil.CompareWaitN(1, func() bool {
return false
})
factor1 := time.Since(ts)

require.InEpsilon(t, 1.0, factor1/factor0, float64((10 * time.Millisecond).Nanoseconds()))
}

func TestCompareWaitN_Factor2(t *testing.T) {
ts := time.Now()
testutil.CompareWaitN(1, func() bool {
return false
})
factor1 := time.Since(ts)

ts = time.Now()
testutil.CompareWaitN(2, func() bool {
return false
})
factor2 := time.Since(ts)

require.InEpsilon(t, 2.0, factor2/factor1, float64((10 * time.Millisecond).Nanoseconds()))
}

func TestGetFunctionName(t *testing.T) {
got := testutil.GetFunctionName(TestGetFunctionName)
require.Equal(t, "testutil_test.TestGetFunctionName", got)
}

0 comments on commit c9d5fa8

Please sign in to comment.