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