mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 03:27:03 -07:00
Added CheckUntil() for tests
This commit is contained in:
parent
879cfac61e
commit
affba7dfa3
1 changed files with 27 additions and 0 deletions
27
internal/util/util.go
Normal file
27
internal/util/util.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// CheckUntil regularly check a predicate until it's true or time out is reached.
|
||||
func CheckUntil(interval time.Duration, timeout time.Duration, predicate func() (bool, error)) error {
|
||||
timeoutCh := time.After(timeout)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-time.After(interval):
|
||||
predTrue, err := predicate()
|
||||
if predTrue {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case <-timeoutCh:
|
||||
return fmt.Errorf("timeout of %ds reached", int64(timeout/time.Second))
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue