added error handling tests to IterateContent()

This commit is contained in:
Jarek Kowalski
2019-07-27 18:50:48 -07:00
parent b365d3414c
commit 54d3c2071f

View File

@@ -7,7 +7,6 @@
cryptorand "crypto/rand"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"math/rand"
"reflect"
@@ -18,6 +17,7 @@
"time"
logging "github.com/op/go-logging"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/blobtesting"
"github.com/kopia/kopia/repo/blob"
@@ -601,10 +601,12 @@ func TestIterateContents(t *testing.T) {
t.Logf("contentID3: %v", contentID3)
t.Logf("contentID4: %v", contentID4)
someError := errors.New("some error")
cases := []struct {
desc string
options IterateOptions
want map[ID]bool
fail error
}{
{
desc: "default options",
@@ -630,6 +632,18 @@ func TestIterateContents(t *testing.T) {
contentID3: true,
},
},
{
desc: "failure",
options: IterateOptions{},
fail: someError,
},
{
desc: "failure-parallel",
options: IterateOptions{
Parallel: 10,
},
fail: someError,
},
{
desc: "parallel, include deleted",
options: IterateOptions{
@@ -668,18 +682,23 @@ func TestIterateContents(t *testing.T) {
got := map[ID]bool{}
err := bm.IterateContents(tc.options, func(ci Info) error {
if tc.fail != nil {
return tc.fail
}
mu.Lock()
got[ci.ID] = true
mu.Unlock()
return nil
})
if err != nil {
if (err != nil) != (tc.fail != nil) {
t.Errorf("error iterating: %v", err)
}
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("invalid content IDs got: %v, want %v", got, tc.want)
if err == nil {
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("invalid content IDs got: %v, want %v", got, tc.want)
}
}
})
}