From 1f24bd91c30a3f0b623c452cffa9f043f202d98b Mon Sep 17 00:00:00 2001 From: Steve Kuznetsov Date: Mon, 9 May 2022 08:23:58 -0700 Subject: [PATCH] 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()