From affba7dfa30068f5f948f551255c2a48b850c71a Mon Sep 17 00:00:00 2001 From: David Allen Date: Thu, 19 Sep 2024 16:38:46 -0600 Subject: [PATCH] Added CheckUntil() for tests --- internal/util/util.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 internal/util/util.go diff --git a/internal/util/util.go b/internal/util/util.go new file mode 100644 index 0000000..3edeeff --- /dev/null +++ b/internal/util/util.go @@ -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)) + } + } +}