-
Notifications
You must be signed in to change notification settings - Fork 646
/
manydbs_test.go
73 lines (60 loc) · 1.25 KB
/
manydbs_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
package bbolt
import (
"crypto/rand"
"fmt"
"os"
"path/filepath"
"testing"
)
func createDb(t *testing.T) (*DB, func()) {
// First, create a temporary directory to be used for the duration of
// this test.
tempDirName, err := os.MkdirTemp("", "bboltmemtest")
if err != nil {
t.Fatalf("error creating temp dir: %v", err)
}
path := filepath.Join(tempDirName, "testdb.db")
bdb, err := Open(path, 0600, nil)
if err != nil {
t.Fatalf("error creating bbolt db: %v", err)
}
cleanup := func() {
bdb.Close()
os.RemoveAll(tempDirName)
}
return bdb, cleanup
}
func createAndPutKeys(t *testing.T) {
t.Parallel()
db, cleanup := createDb(t)
defer cleanup()
bucketName := []byte("bucket")
for i := 0; i < 100; i++ {
err := db.Update(func(tx *Tx) error {
nodes, err := tx.CreateBucketIfNotExists(bucketName)
if err != nil {
return err
}
var key [16]byte
_, rerr := rand.Read(key[:])
if rerr != nil {
return rerr
}
if err := nodes.Put(key[:], nil); err != nil {
return err
}
return nil
})
if err != nil {
t.Fatal(err)
}
}
}
func TestManyDBs(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
for i := 0; i < 100; i++ {
t.Run(fmt.Sprintf("%d", i), createAndPutKeys)
}
}