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/storj/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testParallel(t *testing.T, store storage.KeyValueStore) {
|
|
|
|
items := storage.Items{
|
|
|
|
newItem("a", "1", false),
|
|
|
|
newItem("b", "2", false),
|
|
|
|
newItem("c", "3", false),
|
|
|
|
}
|
|
|
|
rand.Shuffle(len(items), items.Swap)
|
|
|
|
defer cleanupItems(store, items)
|
|
|
|
|
2019-05-29 14:30:16 +01: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
|
2019-06-05 15:23:10 +01:00
|
|
|
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
|
2019-06-05 15:23:10 +01:00
|
|
|
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
|
2019-06-05 15:23:10 +01:00
|
|
|
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")
|
2019-07-23 20:46:33 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-06-05 15:23:10 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-06-05 15:23:10 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|