forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadwrite_tx_test.go
49 lines (37 loc) · 1.18 KB
/
readwrite_tx_test.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
package kvdb
import (
"testing"
"github.com/btcsuite/btcwallet/walletdb"
"github.com/stretchr/testify/require"
)
func testTxManualCommit(t *testing.T, db walletdb.DB) {
tx, err := db.BeginReadWriteTx()
require.NoError(t, err)
require.NotNil(t, tx)
committed := false
tx.OnCommit(func() {
committed = true
})
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
require.NoError(t, err)
require.NotNil(t, apple)
require.NoError(t, apple.Put([]byte("testKey"), []byte("testVal")))
banana, err := tx.CreateTopLevelBucket([]byte("banana"))
require.NoError(t, err)
require.NotNil(t, banana)
require.NoError(t, banana.Put([]byte("testKey"), []byte("testVal")))
require.NoError(t, tx.DeleteTopLevelBucket([]byte("banana")))
require.NoError(t, tx.Commit())
require.True(t, committed)
}
func testTxRollback(t *testing.T, db walletdb.DB) {
tx, err := db.BeginReadWriteTx()
require.Nil(t, err)
require.NotNil(t, tx)
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
require.Nil(t, err)
require.NotNil(t, apple)
require.NoError(t, apple.Put([]byte("testKey"), []byte("testVal")))
require.NoError(t, tx.Rollback())
require.Error(t, walletdb.ErrTxClosed, tx.Commit())
}