Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
storage/testing: move cancelled watch test to generic package
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>

Kubernetes-commit: 1f24bd91c30a3f0b623c452cffa9f043f202d98b
  • Loading branch information
stevekuznetsov authored and k8s-publishing-bot committed May 9, 2022
1 parent fe17bc7 commit a4bcbc5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
25 changes: 1 addition & 24 deletions pkg/storage/etcd3/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 23 additions & 0 deletions pkg/storage/testing/watcher_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a4bcbc5

Please sign in to comment.