-
Notifications
You must be signed in to change notification settings - Fork 5
/
cdb_test.go
219 lines (170 loc) · 4.56 KB
/
cdb_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package cdb
import (
"hash/fnv"
"io/ioutil"
"os"
"strconv"
"sync"
"testing"
"github.com/stretchr/testify/suite"
)
type testCDBRecord struct {
key []byte
val []byte
}
type CDBTestSuite struct {
suite.Suite
cdbFile *os.File
cdbHandle *CDB
testRecords []testCDBRecord
}
func TestCDBTestSuite(t *testing.T) {
suite.Run(t, new(CDBTestSuite))
}
func (suite *CDBTestSuite) getCDBReader() Reader {
// initialize reader
reader, err := suite.cdbHandle.GetReader(suite.cdbFile)
suite.Require().Nilf(err, "Can't get CDB reader: %#v", err)
return reader
}
func (suite *CDBTestSuite) getCDBWriter() Writer {
// initialize writer
writer, err := suite.cdbHandle.GetWriter(suite.cdbFile)
suite.Require().Nilf(err, "Can't get CDB writer: %#v", err)
return writer
}
func (suite *CDBTestSuite) SetupTest() {
f, err := ioutil.TempFile("", "test_*.cdb")
suite.Require().Nilf(err, "Can't open temporary file: %#v", err)
suite.cdbFile = f
suite.cdbHandle = New() // create new cdb handle
// generate test records
testRecords := make([]testCDBRecord, 10)
for i := 0; i < len(testRecords); i++ {
stri := strconv.Itoa(i)
testRecords[i].key = []byte("key" + stri)
testRecords[i].val = []byte("val" + stri)
}
suite.testRecords = testRecords
}
func (suite *CDBTestSuite) TearDownTest() {
err := suite.cdbFile.Close()
suite.Nilf(err, "Can't close cdb file: %#v", err)
err = os.Remove(suite.cdbFile.Name())
suite.Nilf(err, "Can't remove cdb file: %#v", err)
}
func (suite *CDBTestSuite) fillTestCDB() {
writer := suite.getCDBWriter()
defer func() {
err := writer.Close()
suite.Require().Nilf(err, "Can't close cdb writer: %#v", err)
}()
for _, rec := range suite.testRecords {
err := writer.Put(rec.key, rec.val)
suite.Require().Nilf(err, "Cant put new value to cdb: %#v", err)
}
}
func (suite *CDBTestSuite) TestWriteCDB() {
suite.fillTestCDB()
}
func (suite *CDBTestSuite) TestShouldReturnAllValues() {
suite.fillTestCDB()
reader := suite.getCDBReader()
for _, rec := range suite.testRecords {
value, err := reader.Get(rec.key)
suite.Nilf(err, "Can't get from cdb key: %s", string(rec.key))
suite.Equalf(value, rec.val, "Test record value differs from the cdb's one")
exists, err := reader.Has(rec.key)
suite.Nilf(err, "error on check has key")
suite.True(exists)
}
suite.Equal(reader.Size(), len(suite.testRecords), "check records count")
}
func (suite *CDBTestSuite) writeEmptyCDB() {
writer := suite.getCDBWriter()
err := writer.Close()
suite.Require().Nilf(err, "Can't close writer %#v", err)
}
func (suite *CDBTestSuite) TestShouldReturnNilOnNonExistingKeys() {
suite.writeEmptyCDB()
reader := suite.getCDBReader()
for _, rec := range suite.testRecords {
value, err := reader.Get(rec.key)
suite.EqualError(err, ErrEntryNotFound.Error(), "Can't get from cdb key: %s", string(rec.key))
suite.Nil(value, "Value must be nil for non existing keys")
exists, err := reader.Has(rec.key)
suite.Nilf(err, "error on check has key")
suite.False(exists)
}
}
func (suite *CDBTestSuite) TestConcurrentGet() {
suite.fillTestCDB()
reader := suite.getCDBReader()
wg := &sync.WaitGroup{}
for i := 0; i < 20; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for _, rec := range suite.testRecords {
value, err := reader.Get(rec.key)
suite.Nilf(err, "Error while getting key")
suite.Equal(value, rec.val)
}
}()
}
wg.Wait()
}
func (suite *CDBTestSuite) TestSetHash() {
suite.cdbHandle.SetHash(fnv.New32)
suite.TestShouldReturnAllValues()
}
func BenchmarkGetReader(b *testing.B) {
n := 1000
f, _ := os.Create("test.cdb")
defer f.Close()
defer os.Remove("test.cdb")
handle := New()
writer, _ := handle.GetWriter(f)
keys := make([][]byte, n)
for i := 0; i < n; i++ {
keys[i] = []byte(strconv.Itoa(i))
writer.Put(keys[i], keys[i])
}
writer.Close()
b.ResetTimer()
for j := 0; j < b.N; j++ {
handle.GetReader(f)
}
}
func BenchmarkReaderGet(b *testing.B) {
n := 1000
f, _ := os.Create("test.cdb")
defer f.Close()
defer os.Remove("test.cdb")
handle := New()
writer, _ := handle.GetWriter(f)
keys := make([][]byte, n)
for i := 0; i < n; i++ {
keys[i] = []byte(strconv.Itoa(i))
writer.Put(keys[i], keys[i])
}
writer.Close()
reader, _ := handle.GetReader(f)
b.ResetTimer()
for j := 0; j < b.N; j++ {
reader.Get(keys[j%n])
}
}
func BenchmarkWriterPut(b *testing.B) {
f, _ := os.Create("test.cdb")
defer f.Close()
defer os.Remove("test.cdb")
handle := New()
writer, _ := handle.GetWriter(f)
b.ResetTimer()
for j := 0; j < b.N; j++ {
key := []byte(strconv.Itoa(j))
writer.Put(key, key)
}
writer.Close()
}