-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(utils): fix bug and add test for retry method
- Loading branch information
Showing
4 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package wait | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func doSuccessOnNth(n int) func(context.Context) error { | ||
count := 0 | ||
return func(context.Context) error { | ||
count = count + 1 | ||
if count == n { | ||
return nil | ||
} | ||
return fmt.Errorf("failed on %vth try", count) | ||
} | ||
} | ||
|
||
func TestRetryWithInterval(t *testing.T) { | ||
tests := map[string]struct { | ||
do func(context.Context) error | ||
retry int | ||
expectErr bool | ||
}{ | ||
"success wtih no retry on first try": { | ||
do: doSuccessOnNth(1), | ||
expectErr: false, | ||
}, | ||
"success on retry": { | ||
do: doSuccessOnNth(2), | ||
retry: 2, | ||
expectErr: false, | ||
}, | ||
"fail with no retry": { | ||
do: doSuccessOnNth(2), | ||
expectErr: true, | ||
}, | ||
"fail with retry": { | ||
do: doSuccessOnNth(3), | ||
retry: 1, | ||
expectErr: true, | ||
}, | ||
} | ||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
test := test | ||
t.Parallel() | ||
err := RetryWithInterval(context.Background(), test.retry, 0, test.do) | ||
if (err != nil) != test.expectErr { | ||
t.Errorf("expectErr: %v, got %v", test.expectErr, err) | ||
} | ||
}) | ||
} | ||
} |