-
Notifications
You must be signed in to change notification settings - Fork 3
/
db_manager_test.go
283 lines (212 loc) · 7.43 KB
/
db_manager_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"math/rand"
"os"
"reflect"
"strconv"
"testing"
"time"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
)
var dbManHelper = TestHelpers{}
func closeDB(dbMgr DBManager) {
if dbMgr != nil {
dbMgr.Close()
}
}
// ----------------------------------------------
// NewDBManager TESTS
// ----------------------------------------------
func Test_NewDBManager(t *testing.T) {
// create db file for testing
dbPath := testHelpers.DBPath(t)
defer os.Remove(dbPath)
conf := &Config{DBPath: dbPath}
// create DBManager
dbMgr, err := NewDBManager(conf)
defer closeDB(dbMgr)
assert.EnsureNil(t, err, "NewDBManager() returned an unexpected error: %#v", err)
// validate that expected DBManager was returned
assert.Equal(t, reflect.TypeOf(dbMgr), reflect.TypeOf(&levelDBManager{}), "NewDBManager() returned an unexpected object type")
}
// ----------------------------------------------
// levelDBManager.NewDatastore TESTS
// ----------------------------------------------
func Test_levelDBManager_NewDatastore(t *testing.T) {
// create db file for testing
dbPath := testHelpers.DBPath(t)
defer os.Remove(dbPath)
conf := &Config{DBPath: dbPath}
// create DBManager
dbMgr, err := NewDBManager(conf)
defer closeDB(dbMgr)
assert.EnsureNil(t, err, "NewDBManager() returned an unexpected error: %#v", err)
// create BackendDB
db := dbMgr.NewDatastore()
assert.EnsureNotNil(t, db, "levelDBManager.NewDatastore() returned a nil value")
// validate that expected BackendDB was returned
assert.Equal(t, reflect.TypeOf(db), reflect.TypeOf(&levelDBDatastore{}), "levelDBManager.NewDatastore() returned an unexpected object type")
}
// ----------------------------------------------
// openLevelDBFromFile TESTS
// ----------------------------------------------
func Test_openLevelDBFromFile(t *testing.T) {
// create db file for testing
dbPath := testHelpers.DBPath(t)
defer os.Remove(dbPath)
openDB, recoverDB := getMockRecoverableOpenerAndRecoverer()
// validate the function
_, err := openLevelDBFromFile(openDB, recoverDB, dbPath, nil)
assert.EnsureNil(t, err, "openLevelDBFromFile() returned an unexpected error: %v", err)
}
func Test_openLevelDBFromFile_Concurrently(t *testing.T) {
t.Skip("Skipping test. This test will deadlock.")
type Entry struct {
ID int `json:"id"`
Data string `json:"data"`
}
max := 3
ch := make(chan Entry, max)
name, err := ioutil.TempDir("", "conduit_test_db")
defer os.Remove(name)
if err != nil {
t.Fatalf("Unexpected error creating the temp database: %s", err.Error())
}
tester := func(dbPath string, index int) {
log.Printf("[GoRoutine %d] Opening database at: %v\n", index, dbPath)
db, err := openLevelDBFromFile(leveldb.OpenFile, leveldb.RecoverFile, name, nil)
if err != nil {
t.Fatalf("Unexpected error opening the database file: %v", err.Error())
}
log.Printf("[GoRoutine %d] Opened database at: %v\n", index, dbPath)
data := strconv.FormatInt(rand.Int63(), 10)
log.Printf("[GoRoutine %d] Setting data field to: %v\n", index, data)
dbEntry, err := json.Marshal(Entry{ID: index, Data: data})
log.Printf("[GoRoutine %d] Generated data: %s\n", index, string(dbEntry))
if err != nil {
t.Fatalf("Unexpected error converting entry to a []byte: %v", err.Error)
}
err = db.Put([]byte(string(index)), dbEntry, nil)
if err != nil {
t.Fatalf("Unexpected error writing to the database: %v", err.Error())
}
waitSeconds := rand.Intn(10)
log.Printf("[GoRoutine %d] Sleeping for %d seconds...\n", index, waitSeconds)
select {
case <-time.After(time.Duration(waitSeconds) * time.Second):
found, err := db.Get([]byte(string(index)), nil)
if err != nil {
t.Fatalf("Unexpected error reading from the database: %v", err.Error())
}
log.Printf("[GoRoutine %d] Retrieved data: %v\n", index, string(found))
result := &Entry{}
err = json.Unmarshal(found, result)
if err != nil {
t.Fatalf("Unexpected error converting retrieved result to an &entry{}: %v", err.Error())
}
db.Close()
ch <- *result
}
}
for i := 0; i < max; i++ {
go tester(name, i)
}
count := 0
for count < max {
r := <-ch
log.Printf("[GoRoutine %d] Exited with data: %v\n", r.ID, r.Data)
count++
}
}
func Test_openLevelDBFromFile_AsSingleton(t *testing.T) {
if testing.Short() {
t.Skip("This test always works, but it can take up to five seconds to finish. Skipping for speed.")
}
type Entry struct {
ID int `json:"id"`
Data string `json:"data"`
}
// Vary the random data a little
rand.Seed(time.Now().UnixNano())
ch := make(chan Entry)
// Create a temporary location for the leveldb data files
name, err := ioutil.TempDir("", "conduit_test_db")
defer os.Remove(name)
if err != nil {
t.Fatalf("Unexpected error creating the temp database: %s", err.Error())
}
// Open the database; The application should keep the DB open until the app shuts down
log.Printf("Opening database at: %v\n", name)
db, err := openLevelDBFromFile(leveldb.OpenFile, leveldb.RecoverFile, name, nil)
defer db.Close()
if err != nil {
t.Fatalf("Unexpected error opening the database file: %v", err.Error())
}
log.Printf("Opened database at: %v\n", name)
// A helper function to interact with the data
tester := func(db leveldb.DB, index, waitSeconds int) {
data := strconv.FormatInt(rand.Int63(), 10)
log.Printf("[GoRoutine %d] Setting data field to: %v\n", index, data)
dbEntry, err := json.Marshal(Entry{ID: index, Data: data})
log.Printf("[GoRoutine %d] Generated data: %s\n", index, string(dbEntry))
if err != nil {
t.Fatalf("Unexpected error converting entry to a []byte: %v", err.Error)
}
err = db.Put([]byte(string(index)), dbEntry, nil)
if err != nil {
t.Fatalf("Unexpected error writing to the database: %v", err.Error())
}
log.Printf("[GoRoutine %d] Sleeping for %d seconds...\n", index, waitSeconds)
select {
case <-time.After(time.Duration(waitSeconds) * time.Second):
found, err := db.Get([]byte(string(index)), nil)
if err != nil {
t.Fatalf("Unexpected error reading from the database: %v", err.Error())
}
log.Printf("[GoRoutine %d] Retrieved data: %v\n", index, string(found))
result := &Entry{}
err = json.Unmarshal(found, result)
if err != nil {
t.Fatalf("Unexpected error converting retrieved result to an &entry{}: %v", err.Error())
}
ch <- *result
}
}
// Open separate goroutines
max := 3
for i := 0; i < max; i++ {
go tester(*db, i, rand.Intn(6))
}
// Process results
count := 0
for count < max {
r := <-ch
log.Printf("[GoRoutine %d] Exited with data: %v\n", r.ID, r.Data)
count++
}
}
func getMockOpenerAndRecoverer(allowRecovery bool) (dbOpener, dbRecoverer) {
openDB := func(dbPath string, o *opt.Options) (*leveldb.DB, error) {
corrupted := leveldb.ErrCorrupted{Type: leveldb.CorruptedManifest, Err: errors.New("leveldb: manifest file missing")}
return nil, corrupted
}
recoverDB := func(dbPath string, o *opt.Options) (*leveldb.DB, error) {
if allowRecovery {
return leveldb.OpenFile(dbPath, o)
}
corrupted := leveldb.ErrCorrupted{Type: leveldb.CorruptedManifest, Err: errors.New("leveldb: manifest file missing")}
return nil, corrupted
}
return openDB, recoverDB
}
func getMockCorruptOpenerAndRecoverer() (dbOpener, dbRecoverer) {
return getMockOpenerAndRecoverer(false)
}
func getMockRecoverableOpenerAndRecoverer() (dbOpener, dbRecoverer) {
return getMockOpenerAndRecoverer(true)
}