48 lines
969 B
Go
48 lines
969 B
Go
|
|
package wincmd
|
||
|
|
|
||
|
|
import "time"
|
||
|
|
|
||
|
|
func waitUntil(timeout time.Duration, interval time.Duration, timeoutMsg string, check func() (bool, error)) error {
|
||
|
|
if timeout <= 0 {
|
||
|
|
timeout = time.Second
|
||
|
|
}
|
||
|
|
return waitUntilStrict(timeout, interval, timeoutMsg, check)
|
||
|
|
}
|
||
|
|
|
||
|
|
func waitUntilStrict(timeout time.Duration, interval time.Duration, timeoutMsg string, check func() (bool, error)) error {
|
||
|
|
if timeout <= 0 {
|
||
|
|
done, err := check()
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if done {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
if timeoutMsg == "" {
|
||
|
|
timeoutMsg = "wait condition timeout"
|
||
|
|
}
|
||
|
|
return wrapTimeoutError(timeoutMsg)
|
||
|
|
}
|
||
|
|
if interval <= 0 {
|
||
|
|
interval = 10 * time.Millisecond
|
||
|
|
}
|
||
|
|
|
||
|
|
deadline := time.Now().Add(timeout)
|
||
|
|
for {
|
||
|
|
done, err := check()
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if done {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
if time.Now().After(deadline) {
|
||
|
|
if timeoutMsg == "" {
|
||
|
|
timeoutMsg = "wait condition timeout"
|
||
|
|
}
|
||
|
|
return wrapTimeoutError(timeoutMsg)
|
||
|
|
}
|
||
|
|
time.Sleep(interval)
|
||
|
|
}
|
||
|
|
}
|