-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_test.go
78 lines (66 loc) · 1.63 KB
/
thread_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
package lmdb
import (
"github.com/facebookgo/ensure"
"io/ioutil"
"os"
"testing"
"time"
)
func TestThread(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 {
txn.Put(testBucket, []byte("foo"), []byte("bar"))
return nil
})
db.TransactionalRW(func(txn *ReadWriteTxn) error {
txn.Put(testBucket, []byte("foo2"), []byte("bar2"))
db.TransactionalR(func(txnR ReadTxner) {
val, exist := txnR.Get(testBucket, []byte("foo"))
ensure.True(t, exist)
ensure.DeepEqual(t, val, []byte("bar"))
_, exist2 := txnR.Get(testBucket, []byte("foo2"))
ensure.False(t, exist2)
})
return nil
})
}
func TestThread2(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)
}
go db.TransactionalRW(func(txn *ReadWriteTxn) error {
t.Log("first RW txn begins")
txn.Put(testBucket, []byte("foo"), []byte("bar"))
time.Sleep(1 * time.Second)
_, exist := txn.Get(testBucket, []byte("foo1"))
ensure.False(t, exist)
t.Log("first RW txn ends")
return nil
})
time.Sleep(100 * time.Millisecond)
go db.TransactionalRW(func(txn *ReadWriteTxn) error {
t.Log("second RW txn begins")
txn.Put(testBucket, []byte("foo1"), []byte("bar1"))
t.Log("second RW txn ends")
return nil
})
time.Sleep(2 * time.Second)
}