-
Notifications
You must be signed in to change notification settings - Fork 8
/
barrel_test.go
155 lines (126 loc) · 3.54 KB
/
barrel_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package barrel
import (
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestInitDefaults(t *testing.T) {
var (
brl = &Barrel{}
assert = assert.New(t)
)
// Create a temp directory for running tests.
tmpDir, err := os.MkdirTemp("", "barreldb")
defer os.RemoveAll(tmpDir)
assert.NoError(err)
t.Run("Init_Defaults", func(t *testing.T) {
brl, err = Init(WithDir(tmpDir))
assert.NoError(err)
assert.NotEmpty(brl)
// Check config.
assert.Equal(brl.opts.dir, tmpDir)
// Check defaults.
assert.Equal(false, brl.opts.debug, "debug is wrongly set")
assert.Equal(false, brl.opts.readOnly, "readOnly is wrongly set")
assert.Equal(false, brl.opts.alwaysFSync, "alwaysFSync is wrongly set")
assert.Equal(defaultMaxActiveFileSize, brl.opts.maxActiveFileSize, "defaultMaxActiveFileSize is wrongly set")
assert.Equal(defaultCompactInterval, brl.opts.compactInterval, "defaultCompactInterval is wrongly set")
assert.Equal(defaultFileSizeInterval, brl.opts.checkFileSizeInterval, "defaultFileSizeInterval is wrongly set")
assert.Nil(brl.opts.syncInterval, "syncInterval is wrongly set")
})
t.Run("Close", func(t *testing.T) {
err = brl.Shutdown()
assert.NoError(err)
})
}
func TestInitWithOpts(t *testing.T) {
var (
brl = &Barrel{}
assert = assert.New(t)
)
// Create a temp directory for running tests.
tmpDir, err := os.MkdirTemp("", "barreldb")
defer os.RemoveAll(tmpDir)
assert.NoError(err)
t.Run("Init_Custom", func(t *testing.T) {
brl, err = Init(WithDir(tmpDir), WithAlwaysSync(), WithDebug(), WithMaxActiveFileSize(int64(1<<4)), WithCheckFileSizeInterval(time.Second*15))
assert.NoError(err)
assert.NotEmpty(brl)
// Check config.
assert.Equal(true, brl.opts.alwaysFSync)
assert.Equal(true, brl.opts.debug)
assert.Equal(int64(1<<4), brl.opts.maxActiveFileSize)
assert.Equal(time.Second*15, brl.opts.checkFileSizeInterval)
})
t.Run("Close", func(t *testing.T) {
err = brl.Shutdown()
assert.NoError(err)
})
}
func TestAPI(t *testing.T) {
var (
brl = &Barrel{}
assert = assert.New(t)
)
// Create a temp directory for running tests.
tmpDir, err := os.MkdirTemp("", "barreldb")
defer os.RemoveAll(tmpDir)
assert.NoError(err)
t.Run("Init", func(t *testing.T) {
brl, err = Init(WithDir(tmpDir))
assert.NoError(err)
})
t.Run("Put", func(t *testing.T) {
err = brl.Put("hello", []byte("world"))
assert.NoError(err)
})
t.Run("Get", func(t *testing.T) {
val, err := brl.Get("hello")
assert.NoError(err)
assert.Equal("world", string(val), "value is not equal")
})
t.Run("List", func(t *testing.T) {
keys := brl.List()
assert.NotEmpty(keys)
assert.Len(keys, 1)
assert.Equal([]string{"hello"}, keys)
})
t.Run("Len", func(t *testing.T) {
len := brl.Len()
assert.Equal(len, 1)
})
t.Run("Fold", func(t *testing.T) {
upper := func(s string) error {
fmt.Println(strings.ToUpper(s))
return nil
}
err = brl.Fold(upper)
assert.NoError(err)
})
t.Run("Expiry", func(t *testing.T) {
err = brl.PutEx("keywithexpiry", []byte("valwithex"), time.Second*2)
assert.NoError(err)
time.Sleep(time.Second * 3)
_, err := brl.Get("keywithexpiry")
assert.Error(err)
assert.ErrorIs(err, ErrExpiredKey)
})
t.Run("Delete", func(t *testing.T) {
err = brl.Delete("hello")
assert.NoError(err)
_, err = brl.Get("hello")
assert.Error(err)
assert.ErrorIs(err, ErrNoKey)
})
t.Run("Sync", func(t *testing.T) {
err = brl.Sync()
assert.NoError(err)
})
t.Run("Close", func(t *testing.T) {
err = brl.Shutdown()
assert.NoError(err)
})
}