Skip to content

Commit

Permalink
test: fix flaky message pool integration tests
Browse files Browse the repository at this point in the history
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
TheDivic committed Feb 12, 2022
1 parent aca2a0f commit 3438732
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 105 deletions.
34 changes: 34 additions & 0 deletions itests/kit/circuit.go
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)
}
}
}
Loading

0 comments on commit 3438732

Please sign in to comment.