diff --git a/test/v1alpha1/wait.go b/test/v1alpha1/wait.go index 10ae6e40a5c..44e838f9045 100644 --- a/test/v1alpha1/wait.go +++ b/test/v1alpha1/wait.go @@ -65,6 +65,17 @@ const ( // ConditionAccessorFn is a condition function used polling functions type ConditionAccessorFn func(ca apis.ConditionAccessor) (bool, error) +func pollImmediateWithContext(ctx context.Context, fn func() (bool, error)) error { + return wait.PollImmediate(interval, timeout, func() (bool, error) { + select { + case <-ctx.Done(): + return true, ctx.Err() + default: + } + return fn() + }) +} + // WaitForTaskRunState polls the status of the TaskRun called name from client every // interval until inState returns `true` indicating it is done, returns an // error or timeout. desc will be used to name the metric that is emitted to @@ -74,12 +85,7 @@ func WaitForTaskRunState(ctx context.Context, c *clients, name string, inState C _, span := trace.StartSpan(context.Background(), metricName) defer span.End() - return wait.PollImmediate(interval, timeout, func() (bool, error) { - select { - case <-ctx.Done(): - return true, ctx.Err() - default: - } + return pollImmediateWithContext(ctx, func() (bool, error) { r, err := c.TaskRunClient.Get(ctx, name, metav1.GetOptions{}) if err != nil { return true, err @@ -97,12 +103,7 @@ func WaitForDeploymentState(ctx context.Context, c *clients, name string, namesp _, span := trace.StartSpan(context.Background(), metricName) defer span.End() - return wait.PollImmediate(interval, timeout, func() (bool, error) { - select { - case <-ctx.Done(): - return true, ctx.Err() - default: - } + return pollImmediateWithContext(ctx, func() (bool, error) { d, err := c.KubeClient.Kube.AppsV1().Deployments(namespace).Get(ctx, name, metav1.GetOptions{}) if err != nil { return true, err @@ -120,12 +121,7 @@ func WaitForPodState(ctx context.Context, c *clients, name string, namespace str _, span := trace.StartSpan(context.Background(), metricName) defer span.End() - return wait.PollImmediate(interval, timeout, func() (bool, error) { - select { - case <-ctx.Done(): - return true, ctx.Err() - default: - } + return pollImmediateWithContext(ctx, func() (bool, error) { r, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{}) if err != nil { return true, err @@ -166,12 +162,7 @@ func WaitForServiceExternalIPState(ctx context.Context, c *clients, namespace, n _, span := trace.StartSpan(context.Background(), metricName) defer span.End() - return wait.PollImmediate(interval, timeout, func() (bool, error) { - select { - case <-ctx.Done(): - return true, ctx.Err() - default: - } + return pollImmediateWithContext(ctx, func() (bool, error) { r, err := c.KubeClient.Kube.CoreV1().Services(namespace).Get(ctx, name, metav1.GetOptions{}) if err != nil { return true, err diff --git a/test/wait.go b/test/wait.go index 10ae6e40a5c..44e838f9045 100644 --- a/test/wait.go +++ b/test/wait.go @@ -65,6 +65,17 @@ const ( // ConditionAccessorFn is a condition function used polling functions type ConditionAccessorFn func(ca apis.ConditionAccessor) (bool, error) +func pollImmediateWithContext(ctx context.Context, fn func() (bool, error)) error { + return wait.PollImmediate(interval, timeout, func() (bool, error) { + select { + case <-ctx.Done(): + return true, ctx.Err() + default: + } + return fn() + }) +} + // WaitForTaskRunState polls the status of the TaskRun called name from client every // interval until inState returns `true` indicating it is done, returns an // error or timeout. desc will be used to name the metric that is emitted to @@ -74,12 +85,7 @@ func WaitForTaskRunState(ctx context.Context, c *clients, name string, inState C _, span := trace.StartSpan(context.Background(), metricName) defer span.End() - return wait.PollImmediate(interval, timeout, func() (bool, error) { - select { - case <-ctx.Done(): - return true, ctx.Err() - default: - } + return pollImmediateWithContext(ctx, func() (bool, error) { r, err := c.TaskRunClient.Get(ctx, name, metav1.GetOptions{}) if err != nil { return true, err @@ -97,12 +103,7 @@ func WaitForDeploymentState(ctx context.Context, c *clients, name string, namesp _, span := trace.StartSpan(context.Background(), metricName) defer span.End() - return wait.PollImmediate(interval, timeout, func() (bool, error) { - select { - case <-ctx.Done(): - return true, ctx.Err() - default: - } + return pollImmediateWithContext(ctx, func() (bool, error) { d, err := c.KubeClient.Kube.AppsV1().Deployments(namespace).Get(ctx, name, metav1.GetOptions{}) if err != nil { return true, err @@ -120,12 +121,7 @@ func WaitForPodState(ctx context.Context, c *clients, name string, namespace str _, span := trace.StartSpan(context.Background(), metricName) defer span.End() - return wait.PollImmediate(interval, timeout, func() (bool, error) { - select { - case <-ctx.Done(): - return true, ctx.Err() - default: - } + return pollImmediateWithContext(ctx, func() (bool, error) { r, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{}) if err != nil { return true, err @@ -166,12 +162,7 @@ func WaitForServiceExternalIPState(ctx context.Context, c *clients, namespace, n _, span := trace.StartSpan(context.Background(), metricName) defer span.End() - return wait.PollImmediate(interval, timeout, func() (bool, error) { - select { - case <-ctx.Done(): - return true, ctx.Err() - default: - } + return pollImmediateWithContext(ctx, func() (bool, error) { r, err := c.KubeClient.Kube.CoreV1().Services(namespace).Get(ctx, name, metav1.GetOptions{}) if err != nil { return true, err