-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tests):
retry.UntilNoError
function
- Use in `compareBlocksByNumber` - Use in `waitForNode` - Change `GetBlockHash` to not retry RPC - Remove `PostWithRetry`
- Loading branch information
Showing
5 changed files
with
52 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package retry | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// UntilNoError retries the function `f` until it returns a nil error. | ||
// It waits `retryWait` after each failed call to `f`. | ||
// If the context `ctx` is canceled, the function returns | ||
// immediately an error stating the number of failed tries, | ||
// for how long it retried and the last error returned by `f`. | ||
func UntilNoError(ctx context.Context, retryWait time.Duration, | ||
f func() (err error)) (err error) { | ||
failedTries := 0 | ||
for ctx.Err() == nil { | ||
err = f() | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
failedTries++ | ||
waitCtx, waitCancel := context.WithTimeout(ctx, retryWait) | ||
<-waitCtx.Done() | ||
waitCancel() | ||
} | ||
|
||
totalRetryTime := time.Duration(failedTries) * retryWait | ||
tryWord := "try" | ||
if failedTries > 1 { | ||
tryWord = "tries" | ||
} | ||
return fmt.Errorf("failed after %d %s during %s: %w (%s)", | ||
failedTries, tryWord, totalRetryTime, err, ctx.Err()) | ||
} |
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