Skip to content

Commit

Permalink
[release-1.11] Flags for setting poll timeout and poll interval (#633)
Browse files Browse the repository at this point in the history
* use context poll interval/timeout in DeleteResources (#626)

* use context poll interval/timeout in DeleteResources

* move timings to state to break dep cycle

* comments

* Flags for setting poll timeout and poll interval (#630)

* Flags for setting poll timeout and poll interval

* Rebase on latest changes when timings were moved to state package

* Move setting default pollInterval and pollTimeout before applying opts

---------

Co-authored-by: Marek Schmidt <[email protected]>
  • Loading branch information
mgencur and maschmid authored Nov 21, 2023
1 parent 1f72fcb commit 52a86c4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 23 deletions.
8 changes: 7 additions & 1 deletion pkg/environment/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import (
"fmt"
"strconv"
"strings"
"time"

"knative.dev/reconciler-test/pkg/feature"
"knative.dev/reconciler-test/pkg/state"
)

var (
Expand All @@ -34,6 +36,9 @@ var (

ipFilePath = new(string)
teardownOnFail = new(bool)

pollTimeout = new(time.Duration)
pollInterval = new(time.Duration)
)

// InitFlags registers the requirement and state filter flags supported by the
Expand Down Expand Up @@ -62,7 +67,8 @@ func InitFlags(fs *flag.FlagSet) {

fs.StringVar(ipFilePath, "images.producer.file", "", "file path for file-based image producer")
fs.StringVar(testNamespace, "environment.namespace", "", "Test namespace")

fs.DurationVar(pollTimeout, "poll.timeout", state.DefaultPollTimeout, "Poll timeout")
fs.DurationVar(pollInterval, "poll.interval", state.DefaultPollInterval, "Poll interval")
fs.BoolVar(teardownOnFail, "teardown.on.fail", false, "Set this flag to do teardown even if test fails.")
}

Expand Down
1 change: 1 addition & 0 deletions pkg/environment/magic.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ func (mr *MagicGlobalEnvironment) Environment(opts ...EnvOpts) (context.Context,
}

ctx := ContextWith(mr.c, env)
ctx = ContextWithPollTimings(ctx, *pollInterval, *pollTimeout)

for _, opt := range opts {
if nctx, err := opt(ctx, env); err != nil {
Expand Down
27 changes: 8 additions & 19 deletions pkg/environment/timings.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,26 @@ package environment
import (
"context"
"time"

"knative.dev/reconciler-test/pkg/state"
)

// this has been moved to state pkg to break cycle between environment and feature package,
// keeping the consts here for backwards API compatibility
const (
DefaultPollInterval = 3 * time.Second
DefaultPollTimeout = 2 * time.Minute
)

type timingsKey struct{}
type timingsType struct {
interval time.Duration
timeout time.Duration
}

// PollTimingsFromContext will get the previously set poll timing from context,
// or return the defaults if not found.
// - values from from context.
// - defaults.
// ContextWithPollTimings returns a context with poll timings set
func ContextWithPollTimings(ctx context.Context, interval, timeout time.Duration) context.Context {
return context.WithValue(ctx, timingsKey{}, timingsType{
interval: interval,
timeout: timeout,
})
return state.ContextWithPollTimings(ctx, interval, timeout)
}

// PollTimingsFromContext will get the previously set poll timing from context,
// or return the defaults if not found.
// - values from from context.
// - values from context.
// - defaults.
func PollTimingsFromContext(ctx context.Context) (time.Duration, time.Duration) {
if t, ok := ctx.Value(timingsKey{}).(timingsType); ok {
return t.interval, t.timeout
}
return DefaultPollInterval, DefaultPollTimeout
return state.PollTimingsFromContext(ctx)
}
5 changes: 2 additions & 3 deletions pkg/feature/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"runtime"
"strings"
"sync"
"time"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -32,7 +31,6 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"knative.dev/pkg/apis"
"knative.dev/pkg/injection/clients/dynamicclient"

"knative.dev/reconciler-test/pkg/state"
)

Expand Down Expand Up @@ -229,7 +227,8 @@ func DeleteResources(ctx context.Context, t T, refs []corev1.ObjectReference) er

var lastResource corev1.ObjectReference // One still present resource

err := wait.Poll(time.Second, 4*time.Minute, func() (bool, error) {
interval, timeout := state.PollTimingsFromContext(ctx)
err := wait.Poll(interval, timeout, func() (bool, error) {
for _, ref := range refs {
gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
Expand Down
52 changes: 52 additions & 0 deletions pkg/state/timings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2021 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package state

import (
"context"
"time"
)

const (
DefaultPollInterval = 3 * time.Second
DefaultPollTimeout = 2 * time.Minute
)

type timingsKey struct{}
type timingsType struct {
interval time.Duration
timeout time.Duration
}

// ContextWithPollTimings returns a context with poll timings set
func ContextWithPollTimings(ctx context.Context, interval, timeout time.Duration) context.Context {
return context.WithValue(ctx, timingsKey{}, timingsType{
interval: interval,
timeout: timeout,
})
}

// PollTimingsFromContext will get the previously set poll timing from context,
// or return the defaults if not found.
// - values from context.
// - defaults.
func PollTimingsFromContext(ctx context.Context) (time.Duration, time.Duration) {
if t, ok := ctx.Value(timingsKey{}).(timingsType); ok {
return t.interval, t.timeout
}
panic("no poll timings found in context")
}

0 comments on commit 52a86c4

Please sign in to comment.