mirror of
https://github.com/status-im/consul.git
synced 2025-01-11 06:16:08 +00:00
eddb1af603
This patch removes duplicate internal copies of constants in the structs package which are also defined in the api package. The api.KVOp type with all its values for the TXN endpoint and the api.HealthXXX constants are now used throughout the codebase. This resulted in some circular dependencies in the testutil package which have been resolved by copying code and constants and moving the WaitForLeader function into a separate testrpc package.
40 lines
608 B
Go
40 lines
608 B
Go
package testutil
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type testFn func() (bool, error)
|
|
|
|
const (
|
|
baseWait = 1 * time.Millisecond
|
|
maxWait = 100 * time.Millisecond
|
|
)
|
|
|
|
func WaitForResult(try testFn) error {
|
|
var err error
|
|
wait := baseWait
|
|
for retries := 100; retries > 0; retries-- {
|
|
var success bool
|
|
success, err = try()
|
|
if success {
|
|
time.Sleep(25 * time.Millisecond)
|
|
return nil
|
|
}
|
|
|
|
time.Sleep(wait)
|
|
wait *= 2
|
|
if wait > maxWait {
|
|
wait = maxWait
|
|
}
|
|
}
|
|
if err != nil {
|
|
return errors.Wrap(err, "timed out with error")
|
|
} else {
|
|
return fmt.Errorf("timed out")
|
|
}
|
|
}
|