-
Notifications
You must be signed in to change notification settings - Fork 0
/
nested_txn_test.go
140 lines (122 loc) · 3.1 KB
/
nested_txn_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
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
package lmdb
import (
"errors"
"io/ioutil"
"os"
"testing"
)
const (
testBucket string = "Bucket0"
)
func SubTxns(txn *ReadWriteTxn, t *testing.T) {
if n := txn.BucketStat(testBucket).Entries; n != 0 {
t.Fatal("bucket not empty: ", n)
}
// first tx: success
txn.TransactionalRW(func(txn *ReadWriteTxn) error {
txn.Put(testBucket, []byte("txn00"), []byte("bar00"))
txn.Put(testBucket, []byte("txn01"), []byte("bar01"))
return nil
})
if n := txn.BucketStat(testBucket).Entries; n != 2 {
t.Fatalf("assertion failed. expect 2, got %d", n)
}
// second tx: fail
txn.TransactionalRW(func(txn *ReadWriteTxn) error {
txn.Put(testBucket, []byte("txn10"), []byte("bar10"))
txn.Put(testBucket, []byte("txn11"), []byte("bar11"))
return errors.New("whatever error")
})
if n := txn.BucketStat(testBucket).Entries; n != 2 {
t.Fatalf("assertion failed. expect 2, got %d", n)
}
// third tx: success
txn.TransactionalRW(func(txn *ReadWriteTxn) error {
txn.Put(testBucket, []byte("txn20"), []byte("bar20"))
txn.Put(testBucket, []byte("txn21"), []byte("bar21"))
txn.Put(testBucket, []byte("txn00"), []byte("bar99"))
return nil
})
if n := txn.BucketStat(testBucket).Entries; n != 4 {
t.Fatalf("assertion failed. expect 4, got %d", n)
}
}
func TestNestedTxn1(t *testing.T) {
path, err := ioutil.TempDir("", "lmdb_test")
if err != nil {
panic(err)
}
defer os.RemoveAll(path)
bucketNames := []string{testBucket}
db, err := Open(path, bucketNames)
defer db.Close()
if err != nil {
panic(err)
}
db.TransactionalRW(func(txn *ReadWriteTxn) error {
SubTxns(txn, t)
// commit all
return nil
})
db.TransactionalR(func(txn ReadTxner) {
if n := txn.BucketStat(testBucket).Entries; n != 4 {
t.Fatalf("assertion failed. expect 4, got %d", n)
}
type kvCases struct {
key string
value string
}
cases1 := []kvCases{
{"txn00", "bar99"},
{"txn01", "bar01"},
{"txn20", "bar20"},
{"txn21", "bar21"},
}
for _, c := range cases1 {
v, b := txn.Get(testBucket, []byte(c.key))
if !b {
t.Fatalf("key not found: %s", c.key)
}
if string(v) != c.value {
t.Fatalf("wrong value for %s. expected %s, got %s", c.key, c.value, string(v))
}
}
cases2 := []string{"txn10", "txn11", "txn99"}
for _, key := range cases2 {
_, b := txn.Get(testBucket, []byte(key))
if b {
t.Fatalf("unexpected key: %s", key)
}
}
})
}
func TestNestedTxn2(t *testing.T) {
path, err := ioutil.TempDir("", "lmdb_test")
if err != nil {
panic(err)
}
defer os.RemoveAll(path)
bucketNames := []string{testBucket}
db, err := Open(path, bucketNames)
defer db.Close()
if err != nil {
panic(err)
}
db.TransactionalRW(func(txn *ReadWriteTxn) error {
SubTxns(txn, t)
// rollback all
return errors.New("give me an error!")
})
db.TransactionalR(func(txn ReadTxner) {
if n := txn.BucketStat(testBucket).Entries; n != 0 {
t.Fatalf("assertion failed. expect 0, got %d", n)
}
cases := []string{"txn00", "txn10", "txn11", "txn99"}
for _, key := range cases {
_, b := txn.Get(testBucket, []byte(key))
if b {
t.Fatalf("unexpected key: %s", key)
}
}
})
}