From 6bb7f1d82aa9b0a6e0281f9e7e647a30a862a66a Mon Sep 17 00:00:00 2001 From: Injun Song Date: Thu, 12 Dec 2024 18:38:34 +0900 Subject: [PATCH] feat(testutil): add unit tests and remove unused functions - Added `pkg/util/testutil/testutil_test.go` for unit testing. - Removed unused functions from `pkg/util/testutil/testutil.go`. --- pkg/util/testutil/testutil.go | 58 ---------------------------- pkg/util/testutil/testutil_test.go | 62 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 58 deletions(-) create mode 100644 pkg/util/testutil/testutil_test.go diff --git a/pkg/util/testutil/testutil.go b/pkg/util/testutil/testutil.go index dbd3521f2..d245d513e 100644 --- a/pkg/util/testutil/testutil.go +++ b/pkg/util/testutil/testutil.go @@ -6,8 +6,6 @@ import ( "strings" "time" - "github.com/pkg/errors" - "github.com/kakao/varlog/internal/vtesting" ) @@ -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, "/") diff --git a/pkg/util/testutil/testutil_test.go b/pkg/util/testutil/testutil_test.go new file mode 100644 index 000000000..392389bd5 --- /dev/null +++ b/pkg/util/testutil/testutil_test.go @@ -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) +}