Allow setting timeout on event watcher in functional tests
@@ -12,6 +12,7 @@ import (
"k8s.io/client-go/pkg/util/rand"
"kubevirt.io/kubevirt/pkg/api/v1"
"kubevirt.io/kubevirt/pkg/kubecli"
+ "time"
)
type ProcessFunc func(event *kubev1.Event) (done bool)
@@ -19,12 +20,18 @@ type ProcessFunc func(event *kubev1.Event) (done bool)
type ObjectEventWatcher struct {
object runtime.Object
process ProcessFunc
+ timeout *time.Duration
}
func NewObjectEventWatcher(object runtime.Object, process ProcessFunc) *ObjectEventWatcher {
return &ObjectEventWatcher{object: object, process: process}
}
+func (w *ObjectEventWatcher) Timeout(duration time.Duration) *ObjectEventWatcher {
+ w.timeout = &duration
+ return w
+}
+
func (w *ObjectEventWatcher) Watch() {
cli, err := kubecli.Get()
if err != nil {
@@ -38,12 +45,30 @@ func (w *ObjectEventWatcher) Watch() {
panic(err)
}
defer eventWatcher.Stop()
+ timedOut := false
+ done := make(chan struct{})
+ go func() {
for obj := range eventWatcher.ResultChan() {
+ if timedOut {
+ // If some events are still in the queue, make sure we don't process them anymore
+ break
+ }
if w.process(obj.Object.(*kubev1.Event)) {
+ close(done)
break
}
}
+ }()
+
+ if w.timeout != nil {
+ select {
+ case <-done:
+ case <-time.After(*w.timeout):
+ }
+ } else {
+ <-done
+ }
}
func MustCleanup() {
