-
Notifications
You must be signed in to change notification settings - Fork 402
/
test_crud.go
153 lines (134 loc) · 3.98 KB
/
test_crud.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package testsuite
import (
"bytes"
"math/rand"
"sort"
"testing"
"github.com/stretchr/testify/require"
"storj.io/common/testcontext"
"storj.io/storj/storage"
)
func testCRUD(t *testing.T, ctx *testcontext.Context, store storage.KeyValueStore) {
items := storage.Items{
// newItem("0", "", false), // TODO: broken
newItem("\x00", "\x00", false),
newItem("a/b", "\x01\x00", false),
newItem("a\\b", "\xFF", false),
newItem("full/path/1", "\x00\xFF\xFF\x00", false),
newItem("full/path/2", "\x00\xFF\xFF\x01", false),
newItem("full/path/3", "\x00\xFF\xFF\x02", false),
newItem("full/path/4", "\x00\xFF\xFF\x03", false),
newItem("full/path/5", "\x00\xFF\xFF\x04", false),
newItem("öö", "üü", false),
}
rand.Shuffle(len(items), items.Swap)
defer cleanupItems(t, ctx, store, items)
t.Run("Put", func(t *testing.T) {
for _, item := range items {
err := store.Put(ctx, item.Key, item.Value)
if err != nil {
t.Fatalf("failed to put %q = %v: %v", item.Key, item.Value, err)
}
}
})
rand.Shuffle(len(items), items.Swap)
t.Run("Get", func(t *testing.T) {
for _, item := range items {
value, err := store.Get(ctx, item.Key)
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)
}
}
})
t.Run("GetAll", func(t *testing.T) {
subset := items[:len(items)/2]
keys := subset.GetKeys()
values, err := store.GetAll(ctx, keys)
if err != nil {
t.Fatalf("failed to GetAll %q: %v", keys, err)
}
if len(values) != len(keys) {
t.Fatalf("failed to GetAll %q: got %q", keys, values)
}
for i, item := range subset {
if !bytes.Equal([]byte(values[i]), []byte(item.Value)) {
t.Fatalf("invalid GetAll %q = %v: got %v", item.Key, item.Value, values[i])
}
}
})
t.Run("Update", func(t *testing.T) {
for i, item := range items {
next := items[(i+1)%len(items)]
err := store.CompareAndSwap(ctx, item.Key, item.Value, next.Value)
if err != nil {
t.Fatalf("failed to update %q: %v -> %v: %v", item.Key, item.Value, next.Value, err)
}
}
for i, item := range items {
next := items[(i+1)%len(items)]
value, err := store.Get(ctx, item.Key)
if err != nil {
t.Fatalf("failed to get updated %q = %v: %v", item.Key, next.Value, err)
}
if !bytes.Equal([]byte(value), []byte(next.Value)) {
t.Fatalf("invalid updated value for %q = %v: got %v", item.Key, next.Value, value)
}
}
})
t.Run("Delete", func(t *testing.T) {
k := len(items) / 2
batch, nonbatch := items[:k], items[k:]
var list []storage.Key
for _, item := range batch {
list = append(list, item.Key)
}
var expected storage.Items
for _, item := range batch {
value, err := store.Get(ctx, item.Key)
if err != nil {
t.Fatalf("failed to get %v: %v", item.Key, value)
}
expected = append(expected, storage.ListItem{
Key: item.Key,
Value: value,
})
}
deleted, err := store.DeleteMultiple(ctx, list)
if err != nil {
t.Fatalf("failed to batch delete: %v", err)
}
sort.Slice(expected, func(i, k int) bool {
return expected[i].Key.Less(expected[k].Key)
})
sort.Slice(deleted, func(i, k int) bool {
return deleted[i].Key.Less(deleted[k].Key)
})
require.Equal(t, expected, deleted)
// Duplicate delete should also be fine.
retry, err := store.DeleteMultiple(ctx, list)
if err != nil {
t.Fatalf("failed to batch delete: %v", err)
}
if len(retry) != 0 {
t.Fatalf("expected delete to return nothing: %v", len(retry))
}
// individual deletes
for _, item := range nonbatch {
err := store.Delete(ctx, item.Key)
if err != nil {
t.Fatalf("failed to delete %v: %v", item.Key, err)
}
}
for _, item := range items {
value, err := store.Get(ctx, item.Key)
if err == nil {
t.Fatalf("got deleted value %q = %v", item.Key, value)
}
}
})
}