mirror of
https://github.com/arangodb/kube-arangodb.git
synced 2024-12-14 11:57:37 +00:00
42 lines
632 B
Go
42 lines
632 B
Go
package tests
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type interrupt struct {
|
|
}
|
|
|
|
func (i interrupt) Error() string {
|
|
return "interrupted"
|
|
}
|
|
|
|
func isInterrupt(err error) bool {
|
|
_, ok := err.(interrupt)
|
|
return ok
|
|
}
|
|
|
|
func timeout(interval, timeout time.Duration, action func() error) error {
|
|
intervalT := time.NewTicker(interval)
|
|
defer intervalT.Stop()
|
|
|
|
timeoutT := time.NewTimer(timeout)
|
|
defer timeoutT.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-intervalT.C:
|
|
err := action()
|
|
if err != nil {
|
|
if isInterrupt(err) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
break
|
|
case <-timeoutT.C:
|
|
return fmt.Errorf("function timeouted")
|
|
}
|
|
}
|
|
}
|