base icon indicating copy to clipboard operation
base copied to clipboard

retry.MaxTries has an off-by-one error

Open noel-yap opened this issue 4 years ago • 0 comments

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

noel-yap avatar Apr 01 '21 17:04 noel-yap