/
box_test.go
117 lines (100 loc) · 3.28 KB
/
box_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
package bh
import (
"bytes"
"errors"
"os"
"path"
"testing"
"github.com/golangplus/bytes"
"github.com/golangplus/errors"
"github.com/golangplus/testing/assert"
)
func TestRefCountBox_Basic(t *testing.T) {
fn := path.Join(os.TempDir(), "TestRefCountBox.bolt")
assert.NoError(t, os.RemoveAll(fn))
b := RefCountBox{
DataPath: func() string { return fn },
}
db, err := b.Alloc()
assert.NoError(t, err)
assert.ValueShould(t, "db.DB", db.DB, db.DB != nil, "is nil")
assert.Equal(t, "b.count", b.count, 1)
assert.ValueShould(t, "b.db.DB", b.db.DB, b.db.DB != nil, "is nil")
b.Free()
assert.Equal(t, "b.count", b.count, 0)
assert.ValueShould(t, "b.db.DB", b.db.DB, b.db.DB == nil, "is not nil")
}
func TestRefCountBox_OpenFunc(t *testing.T) {
fn := path.Join(os.TempDir(), "TestRefCountBox.bolt")
assert.NoError(t, os.RemoveAll(fn))
openFuncCalled := false
b := RefCountBox{
DataPath: func() string { return fn },
OpenFunc: func(path string) (DB, error) {
openFuncCalled = true
return Open(path, 0644, nil)
},
}
db, err := b.Alloc()
assert.NoError(t, err)
assert.ValueShould(t, "db.DB", db.DB, db.DB != nil, "is nil")
assert.Equal(t, "b.count", b.count, 1)
assert.ValueShould(t, "b.db.DB", b.db.DB, b.db.DB != nil, "is nil")
assert.True(t, "openFuncCalled", openFuncCalled)
b.Free()
assert.Equal(t, "b.count", b.count, 0)
assert.ValueShould(t, "b.db.DB", b.db.DB, b.db.DB == nil, "is not nil")
}
func TestRefCountBox_UnsetDataPath(t *testing.T) {
fn := path.Join(os.TempDir(), "TestRefCountBox.bolt")
assert.NoError(t, os.RemoveAll(fn))
b := RefCountBox{}
db, err := b.Alloc()
assert.Equal(t, "err", errorsp.Cause(err), ErrBoxDataPathNotSpecified)
assert.ValueShould(t, "db.DB", db.DB, db.DB == nil, "is not nil")
assert.ValueShould(t, "b.db.DB", b.db.DB, b.db.DB == nil, "is not nil")
assert.Equal(t, "b.count", b.count, 0)
}
func TestRefCountBox_OpenFuncFailed(t *testing.T) {
fn := path.Join(os.TempDir(), "TestRefCountBox.bolt")
assert.NoError(t, os.RemoveAll(fn))
openFuncCalled := false
failedErr := errors.New("failed")
b := RefCountBox{
DataPath: func() string { return fn },
OpenFunc: func(path string) (DB, error) {
openFuncCalled = true
return DB{}, failedErr
},
}
db, err := b.Alloc()
assert.Equal(t, "err", errorsp.Cause(err), failedErr)
assert.ValueShould(t, "db.DB", db.DB, db.DB == nil, "is not nil")
assert.ValueShould(t, "b.db.DB", b.db.DB, b.db.DB == nil, "is not nil")
assert.Equal(t, "b.count", b.count, 0)
assert.True(t, "openFuncCalled", openFuncCalled)
}
func TestRefCountBox_UpdateView(t *testing.T) {
fn := path.Join(os.TempDir(), "TestRefCountBox_UpdateView.bolt")
assert.NoError(t, os.RemoveAll(fn))
b := RefCountBox{
DataPath: func() string { return fn },
}
k := bytes.Split([]byte("a.b"), []byte("."))
v := "hello"
assert.NoErrorOrDie(t, b.Update(func(tx Tx) error {
return tx.Put(k, []byte(v))
}))
assert.Equal(t, "b.count", b.count, 0)
assert.ValueShould(t, "b.db.DB", b.db.DB, b.db.DB == nil, "is not nil")
found := false
assert.NoErrorOrDie(t, b.View(func(tx Tx) error {
return tx.Value(k, func(bs bytesp.Slice) error {
found = true
assert.Equal(t, "bs", string(v), string(bs))
return nil
})
}))
assert.Equal(t, "b.count", b.count, 0)
assert.ValueShould(t, "b.db.DB", b.db.DB, b.db.DB == nil, "is not nil")
}