storj/storage/testsuite/test_list.go
paul cannon 94651921c3 storage/testsuite: pass ctx in to bulk setup methods
to make them cancelable. Also,

* rename BulkDelete->BulkDeleteAll

this leaves room for a new method `BulkDelete(items storage.Items)` that
does a bulk deletion of a specified list of items, as opposed to
deleting _everything_. such a method would be used in the
`cleanupItems()` function found in utils.go, because when individual
deletes are fairly slow, that step takes way too long during tests.

* use BulkDelete method if available

nothing currently provides `BulkDelete(items storage.Items) error`,
but we made use of it with the Bigtable testing and code, and may make
use of it again when adding new kv backends.

* and eliminate the global context in test_iterate.go

Change-Id: I171c7a3818beffbad969b131e98b9bbe3f324bf2
2019-12-10 20:22:08 +00:00

76 lines
1.8 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package testsuite
import (
"math/rand"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"storj.io/storj/private/testcontext"
"storj.io/storj/storage"
)
func testList(t *testing.T, ctx *testcontext.Context, store storage.KeyValueStore) {
items := storage.Items{
newItem("path/0", "\x00\xFF\x00", false),
newItem("path/1", "\x01\xFF\x01", false),
newItem("path/2", "\x02\xFF\x02", false),
newItem("path/3", "\x03\xFF\x03", false),
newItem("path/4", "\x04\xFF\x04", false),
newItem("path/5", "\x05\xFF\x05", false),
}
rand.Shuffle(len(items), items.Swap)
defer cleanupItems(t, ctx, store, items)
if err := storage.PutAll(ctx, store, items...); err != nil {
t.Fatalf("failed to setup: %v", err)
}
type Test struct {
Name string
First storage.Key
Limit int
Expected storage.Keys
}
newKeys := func(xs ...string) storage.Keys {
var keys storage.Keys
for _, x := range xs {
keys = append(keys, storage.Key(x))
}
return keys
}
tests := []Test{
{"without key",
nil, 3,
newKeys("path/0", "path/1", "path/2")},
{"without key, limit 0",
nil, 0,
newKeys("path/0", "path/1", "path/2", "path/3", "path/4", "path/5")},
{"with key",
storage.Key("path/2"), 3,
newKeys("path/2", "path/3", "path/4")},
{"without key 100",
nil, 100,
newKeys("path/0", "path/1", "path/2", "path/3", "path/4", "path/5")},
}
for _, test := range tests {
var keys storage.Keys
var err error
keys, err = store.List(ctx, test.First, test.Limit)
if err != nil {
t.Errorf("%s: %s", test.Name, err)
continue
}
if diff := cmp.Diff(test.Expected, keys, cmpopts.EquateEmpty()); diff != "" {
t.Errorf("%s: (-want +got)\n%s", test.Name, diff)
}
}
}