-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: fix flaky message pool integration tests
Using the same pattern described in my previous commit. I also added the CircuitBreaker to the itests kit as it may be useful for other integration tests when debugging flakyness caused by timeouts.
- Loading branch information
Showing
2 changed files
with
175 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package kit | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
/* | ||
CircuitBreaker implements a simple time-based circuit breaker used for waiting for async operations to finish. | ||
This is how it works: | ||
- It runs the `cb` function until it returns true, | ||
- waiting for `throttle` duration between each iteration, | ||
- or at most `timeout` duration until it breaks test execution. | ||
You can use it if t.Deadline() is not "granular" enough, and you want to know which specific piece of code timed out, | ||
or you need to set different deadlines in the same test. | ||
*/ | ||
func CircuitBreaker(t *testing.T, label string, throttle, timeout time.Duration, cb func() bool) { | ||
tmo := time.After(timeout) | ||
for { | ||
if cb() { | ||
break | ||
} | ||
select { | ||
case <-tmo: | ||
t.Fatal("timeout: ", label) | ||
default: | ||
fmt.Printf("waiting: %s\n", label) | ||
time.Sleep(throttle) | ||
} | ||
} | ||
} |
Oops, something went wrong.