2020-05-15 17:59:37 +03:00
|
|
|
// +build kvdb_etcd
|
|
|
|
|
2020-02-18 21:26:57 +03:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
2021-02-09 22:19:31 +03:00
|
|
|
"context"
|
2020-02-18 21:26:57 +03:00
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-02-18 21:26:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func reverseKVs(a []KV) []KV {
|
|
|
|
for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutToEmpty(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := NewEtcdTestFixture(t)
|
2021-02-09 22:19:31 +03:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
txQueue := NewCommitQueue(ctx)
|
2020-08-10 17:50:06 +03:00
|
|
|
defer func() {
|
2021-02-09 22:19:31 +03:00
|
|
|
cancel()
|
2020-08-10 17:50:06 +03:00
|
|
|
f.Cleanup()
|
|
|
|
txQueue.Wait()
|
|
|
|
}()
|
2020-02-18 21:26:57 +03:00
|
|
|
|
2021-02-09 22:19:31 +03:00
|
|
|
db, err := newEtcdBackend(ctx, f.BackendConfig())
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
apply := func(stm STM) error {
|
|
|
|
stm.Put("123", "abc")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:50:06 +03:00
|
|
|
err = RunSTM(db.cli, apply, txQueue)
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, "abc", f.Get("123"))
|
2020-02-18 21:26:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPutDel(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := NewEtcdTestFixture(t)
|
2021-02-09 22:19:31 +03:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
txQueue := NewCommitQueue(ctx)
|
2020-08-10 17:50:06 +03:00
|
|
|
defer func() {
|
2021-02-09 22:19:31 +03:00
|
|
|
cancel()
|
2020-08-10 17:50:06 +03:00
|
|
|
f.Cleanup()
|
|
|
|
txQueue.Wait()
|
|
|
|
}()
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
testKeyValues := []KV{
|
|
|
|
{"a", "1"},
|
|
|
|
{"b", "2"},
|
|
|
|
{"c", "3"},
|
|
|
|
{"d", "4"},
|
|
|
|
{"e", "5"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range testKeyValues {
|
|
|
|
f.Put(kv.key, kv.val)
|
|
|
|
}
|
|
|
|
|
2021-02-09 22:19:31 +03:00
|
|
|
db, err := newEtcdBackend(ctx, f.BackendConfig())
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
apply := func(stm STM) error {
|
|
|
|
// Get some non existing keys.
|
|
|
|
v, err := stm.Get("")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
v, err = stm.Get("x")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// Get all existing keys.
|
|
|
|
for _, kv := range testKeyValues {
|
|
|
|
v, err = stm.Get(kv.key)
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []byte(kv.val), v)
|
2020-02-18 21:26:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overwrite, then delete an existing key.
|
|
|
|
stm.Put("c", "6")
|
|
|
|
|
|
|
|
v, err = stm.Get("c")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []byte("6"), v)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
stm.Del("c")
|
|
|
|
|
|
|
|
v, err = stm.Get("c")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// Re-add the deleted key.
|
|
|
|
stm.Put("c", "7")
|
|
|
|
|
|
|
|
v, err = stm.Get("c")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []byte("7"), v)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// Add a new key.
|
|
|
|
stm.Put("x", "x")
|
|
|
|
|
|
|
|
v, err = stm.Get("x")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []byte("x"), v)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:50:06 +03:00
|
|
|
err = RunSTM(db.cli, apply, txQueue)
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, "1", f.Get("a"))
|
|
|
|
require.Equal(t, "2", f.Get("b"))
|
|
|
|
require.Equal(t, "7", f.Get("c"))
|
|
|
|
require.Equal(t, "4", f.Get("d"))
|
|
|
|
require.Equal(t, "5", f.Get("e"))
|
|
|
|
require.Equal(t, "x", f.Get("x"))
|
2020-02-18 21:26:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFirstLastNextPrev(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := NewEtcdTestFixture(t)
|
2021-02-09 22:19:31 +03:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
txQueue := NewCommitQueue(ctx)
|
2020-08-10 17:50:06 +03:00
|
|
|
defer func() {
|
2021-02-09 22:19:31 +03:00
|
|
|
cancel()
|
2020-08-10 17:50:06 +03:00
|
|
|
f.Cleanup()
|
|
|
|
txQueue.Wait()
|
|
|
|
}()
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
testKeyValues := []KV{
|
|
|
|
{"kb", "1"},
|
|
|
|
{"kc", "2"},
|
|
|
|
{"kda", "3"},
|
|
|
|
{"ke", "4"},
|
|
|
|
{"w", "w"},
|
|
|
|
}
|
|
|
|
for _, kv := range testKeyValues {
|
|
|
|
f.Put(kv.key, kv.val)
|
|
|
|
}
|
|
|
|
|
2021-02-09 22:19:31 +03:00
|
|
|
db, err := newEtcdBackend(ctx, f.BackendConfig())
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
apply := func(stm STM) error {
|
|
|
|
// First/Last on valid multi item interval.
|
|
|
|
kv, err := stm.First("k")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &KV{"kb", "1"}, kv)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
kv, err = stm.Last("k")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &KV{"ke", "4"}, kv)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// First/Last on single item interval.
|
|
|
|
kv, err = stm.First("w")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &KV{"w", "w"}, kv)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
kv, err = stm.Last("w")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &KV{"w", "w"}, kv)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// Next/Prev on start/end.
|
|
|
|
kv, err = stm.Next("k", "ke")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, kv)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
kv, err = stm.Prev("k", "kb")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, kv)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// Next/Prev in the middle.
|
|
|
|
kv, err = stm.Next("k", "kc")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &KV{"kda", "3"}, kv)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
kv, err = stm.Prev("k", "ke")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &KV{"kda", "3"}, kv)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// Delete first item, then add an item before the
|
|
|
|
// deleted one. Check that First/Next will "jump"
|
|
|
|
// over the deleted item and return the new first.
|
|
|
|
stm.Del("kb")
|
|
|
|
stm.Put("ka", "0")
|
|
|
|
|
|
|
|
kv, err = stm.First("k")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &KV{"ka", "0"}, kv)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
kv, err = stm.Prev("k", "kc")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &KV{"ka", "0"}, kv)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// Similarly test that a new end is returned if
|
|
|
|
// the old end is deleted first.
|
|
|
|
stm.Del("ke")
|
|
|
|
stm.Put("kf", "5")
|
|
|
|
|
|
|
|
kv, err = stm.Last("k")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &KV{"kf", "5"}, kv)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
kv, err = stm.Next("k", "kda")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &KV{"kf", "5"}, kv)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// Overwrite one in the middle.
|
|
|
|
stm.Put("kda", "6")
|
|
|
|
|
|
|
|
kv, err = stm.Next("k", "kc")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &KV{"kda", "6"}, kv)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// Add three in the middle, then delete one.
|
|
|
|
stm.Put("kdb", "7")
|
|
|
|
stm.Put("kdc", "8")
|
|
|
|
stm.Put("kdd", "9")
|
|
|
|
stm.Del("kdc")
|
|
|
|
|
|
|
|
// Check that stepping from first to last returns
|
|
|
|
// the expected sequence.
|
|
|
|
var kvs []KV
|
|
|
|
|
|
|
|
curr, err := stm.First("k")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
for curr != nil {
|
|
|
|
kvs = append(kvs, *curr)
|
|
|
|
curr, err = stm.Next("k", curr.key)
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:26:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
expected := []KV{
|
|
|
|
{"ka", "0"},
|
|
|
|
{"kc", "2"},
|
|
|
|
{"kda", "6"},
|
|
|
|
{"kdb", "7"},
|
|
|
|
{"kdd", "9"},
|
|
|
|
{"kf", "5"},
|
|
|
|
}
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, expected, kvs)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// Similarly check that stepping from last to first
|
|
|
|
// returns the expected sequence.
|
|
|
|
kvs = []KV{}
|
|
|
|
|
|
|
|
curr, err = stm.Last("k")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
for curr != nil {
|
|
|
|
kvs = append(kvs, *curr)
|
|
|
|
curr, err = stm.Prev("k", curr.key)
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:26:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
expected = reverseKVs(expected)
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, expected, kvs)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:50:06 +03:00
|
|
|
err = RunSTM(db.cli, apply, txQueue)
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, "0", f.Get("ka"))
|
|
|
|
require.Equal(t, "2", f.Get("kc"))
|
|
|
|
require.Equal(t, "6", f.Get("kda"))
|
|
|
|
require.Equal(t, "7", f.Get("kdb"))
|
|
|
|
require.Equal(t, "9", f.Get("kdd"))
|
|
|
|
require.Equal(t, "5", f.Get("kf"))
|
|
|
|
require.Equal(t, "w", f.Get("w"))
|
2020-02-18 21:26:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommitError(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := NewEtcdTestFixture(t)
|
2021-02-09 22:19:31 +03:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
txQueue := NewCommitQueue(ctx)
|
2020-08-10 17:50:06 +03:00
|
|
|
defer func() {
|
2021-02-09 22:19:31 +03:00
|
|
|
cancel()
|
2020-08-10 17:50:06 +03:00
|
|
|
f.Cleanup()
|
|
|
|
txQueue.Wait()
|
|
|
|
}()
|
2020-02-18 21:26:57 +03:00
|
|
|
|
2021-02-09 22:19:31 +03:00
|
|
|
db, err := newEtcdBackend(ctx, f.BackendConfig())
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// Preset DB state.
|
|
|
|
f.Put("123", "xyz")
|
|
|
|
|
|
|
|
// Count the number of applies.
|
|
|
|
cnt := 0
|
|
|
|
|
|
|
|
apply := func(stm STM) error {
|
|
|
|
// STM must have the key/value.
|
|
|
|
val, err := stm.Get("123")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
if cnt == 0 {
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, []byte("xyz"), val)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// Put a conflicting key/value during the first apply.
|
|
|
|
f.Put("123", "def")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'd expect to
|
|
|
|
stm.Put("123", "abc")
|
|
|
|
|
|
|
|
cnt++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:50:06 +03:00
|
|
|
err = RunSTM(db.cli, apply, txQueue)
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, cnt)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, "abc", f.Get("123"))
|
2020-02-18 21:26:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestManualTxError(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := NewEtcdTestFixture(t)
|
2021-02-09 22:19:31 +03:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
txQueue := NewCommitQueue(ctx)
|
2020-08-10 17:50:06 +03:00
|
|
|
defer func() {
|
2021-02-09 22:19:31 +03:00
|
|
|
cancel()
|
2020-08-10 17:50:06 +03:00
|
|
|
f.Cleanup()
|
|
|
|
txQueue.Wait()
|
|
|
|
}()
|
2020-02-18 21:26:57 +03:00
|
|
|
|
2021-02-09 22:19:31 +03:00
|
|
|
db, err := newEtcdBackend(ctx, f.BackendConfig())
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// Preset DB state.
|
|
|
|
f.Put("123", "xyz")
|
|
|
|
|
2020-08-10 17:50:06 +03:00
|
|
|
stm := NewSTM(db.cli, txQueue)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
val, err := stm.Get("123")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []byte("xyz"), val)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// Put a conflicting key/value.
|
|
|
|
f.Put("123", "def")
|
|
|
|
|
|
|
|
// Should still get the original version.
|
|
|
|
val, err = stm.Get("123")
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []byte("xyz"), val)
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// Commit will fail with CommitError.
|
|
|
|
err = stm.Commit()
|
|
|
|
var e CommitError
|
2020-06-29 15:53:38 +03:00
|
|
|
require.True(t, errors.As(err, &e))
|
2020-02-18 21:26:57 +03:00
|
|
|
|
|
|
|
// We expect that the transacton indeed did not commit.
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, "def", f.Get("123"))
|
2020-02-18 21:26:57 +03:00
|
|
|
}
|