Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit ScanView if context has been cancelled #7419

Merged
merged 1 commit into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sdk/logical/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func ScanView(ctx context.Context, view ClearableView, cb func(path string)) err

// Handle the contents in the directory
for _, c := range contents {
// Exit if the context has been canceled
if ctx.Err() != nil {
return ctx.Err()
}
fullPath := current + c
if strings.HasSuffix(c, "/") {
frontier = append(frontier, fullPath)
Expand Down
18 changes: 18 additions & 0 deletions sdk/logical/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ func TestScanView(t *testing.T) {
}
}

func TestScanView_CancelContext(t *testing.T) {
s := prepKeyStorage(t)

ctx, cancelCtx := context.WithCancel(context.Background())
var i int
err := ScanView(ctx, s, func(path string) {
cancelCtx()
i++
})

if err == nil {
t.Error("Want context cancel err, got none")
}
if i != 1 {
t.Errorf("Want i==1, got %d", i)
}
}

func TestCollectKeys(t *testing.T) {
s := prepKeyStorage(t)

Expand Down