-
Notifications
You must be signed in to change notification settings - Fork 22
/
rocksdb.go
45 lines (35 loc) · 984 Bytes
/
rocksdb.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
package testutil
import (
"strconv"
"sync"
"testing"
"github.com/iotaledger/hive.go/kvstore"
"github.com/iotaledger/hive.go/kvstore/rocksdb"
)
// variables for keeping track of how many databases have been created by the given test.
var databaseCounter = make(map[string]int)
var databaseCounterMutex sync.Mutex
// RocksDB creates a temporary RocksDBKVStore that automatically gets cleaned up when the test finishes.
func RocksDB(t *testing.T) (kvstore.KVStore, error) {
t.Helper()
dir := t.TempDir()
db, err := rocksdb.CreateDB(dir)
if err != nil {
return nil, err
}
t.Cleanup(func() {
err := db.Close()
if err != nil {
t.Errorf("Closing database: %v", err)
}
})
databaseCounterMutex.Lock()
databaseCounter[t.Name()]++
counter := databaseCounter[t.Name()]
databaseCounterMutex.Unlock()
storeWithRealm, err := rocksdb.New(db).WithRealm([]byte(t.Name() + strconv.Itoa(counter)))
if err != nil {
return nil, err
}
return storeWithRealm, nil
}