Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retry.MaxTries has an off-by-one error #30

Open
noel-yap opened this issue Apr 1, 2021 · 0 comments
Open

retry.MaxTries has an off-by-one error #30

noel-yap opened this issue Apr 1, 2021 · 0 comments

Comments

@noel-yap
Copy link

noel-yap commented Apr 1, 2021

func TestMaxRetries(t *testing.T) {
	retryImmediately := retry.Backoff(0, 0, 0)

	type testArgs struct {
		retryPolicy retry.Policy
		fn          func(*int) error
	}
	testCases := []struct {
		testName string
		args     testArgs
		expected int
	}{
		{
			testName: "function always fails",
			args: testArgs{
				retryPolicy: retry.MaxTries(retryImmediately, 1),
				fn: func(callCount *int) error {
					*callCount++

					return fmt.Errorf("always fail")
				},
			},
			expected: 1,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.testName, func(t *testing.T) {
			callCount := 0

			retry.WaitForFn(context.Background(), tc.args.retryPolicy, tc.args.fn, &callCount)

			assert.Equal(t, tc.expected, callCount)
		})
	}
}

Result:

=== RUN   TestMaxRetries
=== RUN   TestMaxRetries/function_always_fails
    fn_test.go:127: 
        	Error Trace:	fn_test.go:127
        	Error:      	Not equal: 
        	            	expected: 1
        	            	actual  : 2
        	Test:       	TestMaxRetries/function_always_fails
--- FAIL: TestMaxRetries (0.00s)
    --- FAIL: TestMaxRetries/function_always_fails (0.00s)


Expected :1
Actual   :2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant