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

fix: the Retry function throws an error when the opts parameter is passed as nil #150

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ func Retry[T any](ctx context.Context, operation Operation[T], opts ...RetryOpti

// Apply user-provided options to the default settings.
for _, opt := range opts {
opt(args)
if opt != nil {
astak16 marked this conversation as resolved.
Show resolved Hide resolved
opt(args)
}
}

defer args.Timer.Stop()
Expand Down
27 changes: 27 additions & 0 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,33 @@ func TestRetry(t *testing.T) {
}
}

func TestRetryOptionIsNil(t *testing.T) {
const successOn = 3
var i = 0

// This function is successful on "successOn" calls.
f := func() (bool, error) {
i++
log.Printf("function is called %d. time\n", i)

if i == successOn {
log.Println("OK")
return true, nil
}

log.Println("error")
return false, errors.New("error")
}

_, err := Retry(context.Background(), f, nil, nil, nil)
if err != nil {
t.Errorf("unexpected error: %s", err.Error())
}
if i != successOn {
t.Errorf("invalid number of retries: %d", i)
}
}

func TestRetryWithData(t *testing.T) {
const successOn = 3
var i = 0
Expand Down
Loading