Skip to content

Commit

Permalink
Test many DBs used concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
jrick committed May 23, 2020
1 parent 9034717 commit 044f3bd
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions manydbs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package bbolt

import (
"fmt"
"io/ioutil"
"math/rand"
"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 := ioutil.TempDir("", "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
rand.Read(key[:])
if err := nodes.Put(key[:], nil); err != nil {
return err
}

return nil
})
if err != nil {
t.Fatal(err)
}
}
}

func TestManyDBs(t *testing.T) {
for i := 0; i < 100; i++ {
t.Run(fmt.Sprintf("%d", i), createAndPutKeys)
}
}

0 comments on commit 044f3bd

Please sign in to comment.