storj/storage/testsuite/test_parallel.go

77 lines
1.9 KiB
Go
Raw Normal View History

2019-01-24 20:15:10 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
2018-11-20 15:26:20 +00:00
// See LICENSE for copying information.
package testsuite
import (
"bytes"
"math/rand"
"strconv"
"testing"
"storj.io/common/testcontext"
2018-11-20 15:26:20 +00:00
"storj.io/storj/storage"
)
func testParallel(t *testing.T, ctx *testcontext.Context, store storage.KeyValueStore) {
2018-11-20 15:26:20 +00:00
items := storage.Items{
newItem("a", "1", false),
newItem("b", "2", false),
newItem("c", "3", false),
}
rand.Shuffle(len(items), items.Swap)
defer cleanupItems(t, ctx, store, items)
2018-11-20 15:26:20 +00:00
for idx := range items {
i := idx
2018-11-20 15:26:20 +00:00
item := items[i]
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
// Put
err := store.Put(ctx, item.Key, item.Value)
2018-11-20 15:26:20 +00:00
if err != nil {
t.Fatalf("failed to put %q = %v: %v", item.Key, item.Value, err)
}
// Get
value, err := store.Get(ctx, item.Key)
2018-11-20 15:26:20 +00:00
if err != nil {
t.Fatalf("failed to get %q = %v: %v", item.Key, item.Value, err)
}
if !bytes.Equal([]byte(value), []byte(item.Value)) {
t.Fatalf("invalid value for %q = %v: got %v", item.Key, item.Value, value)
}
// GetAll
values, err := store.GetAll(ctx, []storage.Key{item.Key})
2018-11-20 15:26:20 +00:00
if len(values) != 1 {
t.Fatalf("failed to GetAll: %v", err)
}
if !bytes.Equal([]byte(values[0]), []byte(item.Value)) {
t.Fatalf("invalid GetAll %q = %v: got %v", item.Key, item.Value, values[i])
}
// Update value
nextValue := storage.Value(string(item.Value) + "X")
err = store.CompareAndSwap(ctx, item.Key, item.Value, nextValue)
2018-11-20 15:26:20 +00:00
if err != nil {
t.Fatalf("failed to update %q = %v: %v", item.Key, nextValue, err)
}
value, err = store.Get(ctx, item.Key)
2018-11-20 15:26:20 +00:00
if err != nil {
t.Fatalf("failed to get %q = %v: %v", item.Key, nextValue, err)
}
if !bytes.Equal([]byte(value), []byte(nextValue)) {
t.Fatalf("invalid updated value for %q = %v: got %v", item.Key, nextValue, value)
}
err = store.Delete(ctx, item.Key)
2018-11-20 15:26:20 +00:00
if err != nil {
t.Fatalf("failed to delete %v: %v", item.Key, err)
}
})
}
}