From fdf12b7244dfdbae06e240387d84fd2052fa49da Mon Sep 17 00:00:00 2001 From: zsystm <124245155+zsystm@users.noreply.github.com> Date: Mon, 23 Sep 2024 14:56:38 +0900 Subject: [PATCH] test(evmengine): add test cases for helper (#131) * add test cases for helper increased test coverage to 100% * fix ci --- .../evmengine/keeper/helpers_internal_test.go | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 client/x/evmengine/keeper/helpers_internal_test.go diff --git a/client/x/evmengine/keeper/helpers_internal_test.go b/client/x/evmengine/keeper/helpers_internal_test.go new file mode 100644 index 00000000..f92fe363 --- /dev/null +++ b/client/x/evmengine/keeper/helpers_internal_test.go @@ -0,0 +1,71 @@ +package keeper + +import ( + "context" + "errors" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestRetryForever(t *testing.T) { + t.Parallel() + attempts := 0 + + tests := []struct { + name string + ctxFunc func() context.Context + fn func(ctx context.Context) (bool, error) + expectedErr string + expectedAttempts int + }{ + { + name: "Success after retries", + ctxFunc: context.Background, + fn: func(ctx context.Context) (bool, error) { + attempts++ + if attempts < 3 { + return false, nil // Retry + } + + return true, nil // Success + }, + expectedErr: "", + expectedAttempts: 3, + }, + { + name: "Context canceled", + ctxFunc: func() context.Context { + ctx, cancel := context.WithCancel(context.Background()) + cancel() // Cancel immediately + + return ctx + }, + fn: func(ctx context.Context) (bool, error) { + return false, nil + }, + expectedErr: "retry canceled", + }, + { + name: "Func returns error", + ctxFunc: context.Background, + fn: func(ctx context.Context) (bool, error) { + return false, errors.New("some error") + }, + expectedErr: "some error", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + err := retryForever(tc.ctxFunc(), tc.fn) + if tc.expectedErr != "" { + require.Error(t, err) + require.Contains(t, err.Error(), tc.expectedErr) + } else { + require.NoError(t, err) + } + }) + } +}