1
0
Fork 0
mirror of https://github.com/arangodb/kube-arangodb.git synced 2024-12-14 11:57:37 +00:00
kube-arangodb/tests/timeout.go
2019-09-27 13:04:23 +02:00

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")
}
}
}