From 774870611c1d2b405d67eb73ade628ced5e0e994 Mon Sep 17 00:00:00 2001 From: Steve Kuznetsov Date: Mon, 9 May 2022 08:22:04 -0700 Subject: [PATCH 1/3] etcd3/store: update cancelled watch test to be generic There's no reason to create the watch using the underlying watcher. Signed-off-by: Steve Kuznetsov --- .../src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go index 9267722cf8a..01af44118a9 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go @@ -141,7 +141,10 @@ func TestWatchContextCancel(t *testing.T) { cancel() // When we watch with a canceled context, we should detect that it's context canceled. // We won't take it as error and also close the watcher. - w, err := store.watcher.Watch(canceledCtx, "/abc", 0, false, false, storage.Everything) + w, err := store.Watch(canceledCtx, "/abc", storage.ListOptions{ + ResourceVersion: "0", + Predicate: storage.Everything, + }) if err != nil { t.Fatal(err) } From c0fc8172559f7796560a9b80fd23df92095adb14 Mon Sep 17 00:00:00 2001 From: Steve Kuznetsov Date: Mon, 9 May 2022 08:23:03 -0700 Subject: [PATCH 2/3] etcd3/store: call a generic cancelled watch test Signed-off-by: Steve Kuznetsov --- .../src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go index 01af44118a9..c3503b5255a 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go @@ -137,6 +137,10 @@ func TestWatchError(t *testing.T) { func TestWatchContextCancel(t *testing.T) { ctx, store, _ := testSetup(t) + RunTestWatchContextCancel(ctx, t, store) +} + +func RunTestWatchContextCancel(ctx context.Context, t *testing.T, store storage.Interface) { canceledCtx, cancel := context.WithCancel(ctx) cancel() // When we watch with a canceled context, we should detect that it's context canceled. From 1f24bd91c30a3f0b623c452cffa9f043f202d98b Mon Sep 17 00:00:00 2001 From: Steve Kuznetsov Date: Mon, 9 May 2022 08:23:58 -0700 Subject: [PATCH 3/3] storage/testing: move cancelled watch test to generic package Signed-off-by: Steve Kuznetsov --- .../pkg/storage/etcd3/watcher_test.go | 25 +------------------ .../pkg/storage/testing/watcher_tests.go | 23 +++++++++++++++++ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go index c3503b5255a..f009055698d 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go @@ -137,30 +137,7 @@ func TestWatchError(t *testing.T) { func TestWatchContextCancel(t *testing.T) { ctx, store, _ := testSetup(t) - RunTestWatchContextCancel(ctx, t, store) -} - -func RunTestWatchContextCancel(ctx context.Context, t *testing.T, store storage.Interface) { - canceledCtx, cancel := context.WithCancel(ctx) - cancel() - // When we watch with a canceled context, we should detect that it's context canceled. - // We won't take it as error and also close the watcher. - w, err := store.Watch(canceledCtx, "/abc", storage.ListOptions{ - ResourceVersion: "0", - Predicate: storage.Everything, - }) - if err != nil { - t.Fatal(err) - } - - select { - case _, ok := <-w.ResultChan(): - if ok { - t.Error("ResultChan() should be closed") - } - case <-time.After(wait.ForeverTestTimeout): - t.Errorf("timeout after %v", wait.ForeverTestTimeout) - } + storagetesting.RunTestWatchContextCancel(ctx, t, store) } func TestWatchErrResultNotBlockAfterCancel(t *testing.T) { diff --git a/staging/src/k8s.io/apiserver/pkg/storage/testing/watcher_tests.go b/staging/src/k8s.io/apiserver/pkg/storage/testing/watcher_tests.go index ca8b52ffcc9..56e6525c441 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/testing/watcher_tests.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/testing/watcher_tests.go @@ -148,6 +148,29 @@ func RunTestWatchFromNoneZero(ctx context.Context, t *testing.T, store storage.I TestCheckResult(t, watch.Modified, w, out) } +func RunTestWatchContextCancel(ctx context.Context, t *testing.T, store storage.Interface) { + canceledCtx, cancel := context.WithCancel(ctx) + cancel() + // When we watch with a canceled context, we should detect that it's context canceled. + // We won't take it as error and also close the watcher. + w, err := store.Watch(canceledCtx, "/abc", storage.ListOptions{ + ResourceVersion: "0", + Predicate: storage.Everything, + }) + if err != nil { + t.Fatal(err) + } + + select { + case _, ok := <-w.ResultChan(): + if ok { + t.Error("ResultChan() should be closed") + } + case <-time.After(wait.ForeverTestTimeout): + t.Errorf("timeout after %v", wait.ForeverTestTimeout) + } +} + func RunTestWatchInitializationSignal(ctx context.Context, t *testing.T, store storage.Interface) { ctx, _ = context.WithTimeout(ctx, 5*time.Second) initSignal := utilflowcontrol.NewInitializationSignal()