2020-05-15 17:59:37 +03:00
|
|
|
// +build kvdb_etcd
|
|
|
|
|
2020-02-18 21:35:53 +03:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-05-25 19:00:22 +03:00
|
|
|
"context"
|
2020-02-18 21:35:53 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
2020-06-29 15:53:38 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-02-18 21:35:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCopy(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := NewEtcdTestFixture(t)
|
|
|
|
defer f.Cleanup()
|
|
|
|
|
2021-02-09 22:19:31 +03:00
|
|
|
db, err := newEtcdBackend(context.TODO(), f.BackendConfig())
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
err = db.Update(func(tx walletdb.ReadWriteTx) error {
|
|
|
|
// "apple"
|
|
|
|
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, apple)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, apple.Put([]byte("key"), []byte("val")))
|
2020-02-18 21:35:53 +03:00
|
|
|
return nil
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {})
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
// Expect non-zero copy.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, db.Copy(&buf))
|
|
|
|
require.Greater(t, buf.Len(), 0)
|
|
|
|
require.Nil(t, err)
|
2020-02-18 21:35:53 +03:00
|
|
|
|
|
|
|
expected := map[string]string{
|
2021-07-06 17:31:24 +03:00
|
|
|
BucketKey("apple"): BucketVal("apple"),
|
|
|
|
ValueKey("key", "apple"): "val",
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, expected, f.Dump())
|
2020-02-18 21:35:53 +03:00
|
|
|
}
|
2020-05-25 19:00:22 +03:00
|
|
|
|
|
|
|
func TestAbortContext(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := NewEtcdTestFixture(t)
|
|
|
|
defer f.Cleanup()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
config := f.BackendConfig()
|
|
|
|
|
|
|
|
// Pass abort context and abort right away.
|
2021-02-09 22:19:31 +03:00
|
|
|
db, err := newEtcdBackend(ctx, config)
|
2020-06-29 15:53:38 +03:00
|
|
|
require.NoError(t, err)
|
2020-05-25 19:00:22 +03:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
// Expect that the update will fail.
|
|
|
|
err = db.Update(func(tx walletdb.ReadWriteTx) error {
|
|
|
|
_, err := tx.CreateTopLevelBucket([]byte("bucket"))
|
2020-08-10 17:50:06 +03:00
|
|
|
require.Error(t, err, "context canceled")
|
2020-05-25 19:00:22 +03:00
|
|
|
|
|
|
|
return nil
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {})
|
2020-05-25 19:00:22 +03:00
|
|
|
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Error(t, err, "context canceled")
|
2020-05-25 19:00:22 +03:00
|
|
|
|
|
|
|
// No changes in the DB.
|
2020-06-29 15:53:38 +03:00
|
|
|
require.Equal(t, map[string]string{}, f.Dump())
|
2020-05-25 19:00:22 +03:00
|
|
|
}
|