storj/storage/testsuite/test_iterate.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

133 lines
3.2 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package testsuite
import (
"math/rand"
"testing"
"storj.io/storj/private/testcontext"
"storj.io/storj/storage"
)
func testIterate(t *testing.T, ctx *testcontext.Context, store storage.KeyValueStore) {
items := storage.Items{
newItem("a", "a", false),
newItem("b/1", "b/1", false),
newItem("b/2", "b/2", false),
newItem("b/3", "b/3", false),
newItem("c", "c", false),
newItem("c/", "c/", false),
newItem("c//", "c//", false),
newItem("c/1", "c/1", false),
newItem("g", "g", false),
newItem("h", "h", 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)
}
testIterations(t, ctx, store, []iterationTest{
{"no limits",
storage.IterateOptions{}, storage.Items{
newItem("a", "a", false),
newItem("b/", "", true),
newItem("c", "c", false),
newItem("c/", "", true),
newItem("g", "g", false),
newItem("h", "h", false),
}},
{"at a",
storage.IterateOptions{
First: storage.Key("a"),
}, storage.Items{
newItem("a", "a", false),
newItem("b/", "", true),
newItem("c", "c", false),
newItem("c/", "", true),
newItem("g", "g", false),
newItem("h", "h", false),
}},
{"after a",
storage.IterateOptions{
First: storage.NextKey(storage.Key("a")),
}, storage.Items{
newItem("b/", "", true),
newItem("c", "c", false),
newItem("c/", "", true),
newItem("g", "g", false),
newItem("h", "h", false),
}},
{"at b",
storage.IterateOptions{
First: storage.Key("b"),
}, storage.Items{
newItem("b/", "", true),
newItem("c", "c", false),
newItem("c/", "", true),
newItem("g", "g", false),
newItem("h", "h", false),
}},
{"after b",
storage.IterateOptions{
First: storage.NextKey(storage.Key("b")),
}, storage.Items{
newItem("b/", "", true),
newItem("c", "c", false),
newItem("c/", "", true),
newItem("g", "g", false),
newItem("h", "h", false),
}},
{"after c",
storage.IterateOptions{
First: storage.NextKey(storage.Key("c")),
}, storage.Items{
newItem("c/", "", true),
newItem("g", "g", false),
newItem("h", "h", false),
}},
{"at e",
storage.IterateOptions{
First: storage.Key("e"),
}, storage.Items{
newItem("g", "g", false),
newItem("h", "h", false),
}},
{"after e",
storage.IterateOptions{
First: storage.NextKey(storage.Key("e")),
}, storage.Items{
newItem("g", "g", false),
newItem("h", "h", false),
}},
{"prefix b slash",
storage.IterateOptions{
Prefix: storage.Key("b/"),
}, storage.Items{
newItem("b/1", "b/1", false),
newItem("b/2", "b/2", false),
newItem("b/3", "b/3", false),
}},
{"prefix c slash",
storage.IterateOptions{
Prefix: storage.Key("c/"),
}, storage.Items{
newItem("c/", "c/", false),
newItem("c//", "", true),
newItem("c/1", "c/1", false),
}},
{"prefix c slash slash",
storage.IterateOptions{
Prefix: storage.Key("c//"),
}, storage.Items{
newItem("c//", "c//", false),
}},
})
}