-
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(end-to-end): refactor
TestSystemRPC
- Add empty skipped tests for missing cases - Split each subtest individually - Keep on retrying until main context is canceled - Fix `networkState` test case - Assert more fields - Fix #2161 and #807
- Loading branch information
Showing
2 changed files
with
196 additions
and
104 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2022 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package retry | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// UntilOK retries the function `f` until it returns a true | ||
// value for `ok` or a non 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 context error. | ||
func UntilOK(ctx context.Context, retryWait time.Duration, | ||
f func() (ok bool, err error)) (err error) { | ||
failedTries := 0 | ||
for ctx.Err() == nil { | ||
ok, err := f() | ||
if ok { | ||
return nil | ||
} else if err != nil { | ||
return fmt.Errorf("stop retrying function: %w", err) | ||
} | ||
|
||
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)", | ||
failedTries, tryWord, totalRetryTime, ctx.Err()) | ||
} |