Skip to content

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

Open
@noel-yap

Description

@noel-yap
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions