Example: How to use the separated 'btesting' library.

Signed-off-by: Piotr Tabor <ptab@google.com>
pull/360/head
Piotr Tabor 2022-12-19 16:52:00 +01:00
parent 46cdbf5b6c
commit f3e164d8dd
1 changed files with 25 additions and 23 deletions

View File

@ -6,6 +6,7 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"go.etcd.io/bbolt/internal/btesting"
"hash/fnv" "hash/fnv"
"log" "log"
"math/rand" "math/rand"
@ -218,40 +219,29 @@ func TestOpen_ErrChecksum(t *testing.T) {
// https://github.com/boltdb/bolt/issues/291 // https://github.com/boltdb/bolt/issues/291
func TestOpen_Size(t *testing.T) { func TestOpen_Size(t *testing.T) {
// Open a data file. // Open a data file.
db := MustOpenDB() db := btesting.MustCreateDB(t)
path := db.Path()
defer db.MustClose()
pagesize := db.Info().PageSize pagesize := db.Info().PageSize
// Insert until we get above the minimum 4MB size. // Insert until we get above the minimum 4MB size.
if err := db.Update(func(tx *bolt.Tx) error { err := db.Fill([]byte("data"), 1, 10000,
b, _ := tx.CreateBucketIfNotExists([]byte("data")) func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) },
for i := 0; i < 10000; i++ { func(tx int, k int) []byte { return make([]byte, 1000) },
if err := b.Put([]byte(fmt.Sprintf("%04d", i)), make([]byte, 1000)); err != nil { )
t.Fatal(err) if err != nil {
}
}
return nil
}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
// Close database and grab the size. path := db.Path()
if err := db.DB.Close(); err != nil { db.MustClose()
t.Fatal(err)
}
sz := fileSize(path) sz := fileSize(path)
if sz == 0 { if sz == 0 {
t.Fatalf("unexpected new file size: %d", sz) t.Fatalf("unexpected new file size: %d", sz)
} }
// Reopen database, update, and check size again. db.MustReopen()
db0, err := bolt.Open(path, 0666, nil) if err := db.Update(func(tx *bolt.Tx) error {
if err != nil {
t.Fatal(err)
}
if err := db0.Update(func(tx *bolt.Tx) error {
if err := tx.Bucket([]byte("data")).Put([]byte{0}, []byte{0}); err != nil { if err := tx.Bucket([]byte("data")).Put([]byte{0}, []byte{0}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -259,7 +249,7 @@ func TestOpen_Size(t *testing.T) {
}); err != nil { }); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := db0.Close(); err != nil { if err := db.Close(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
newSz := fileSize(path) newSz := fileSize(path)
@ -1627,11 +1617,13 @@ type DB struct {
} }
// MustOpenDB returns a new, open DB at a temporary location. // MustOpenDB returns a new, open DB at a temporary location.
// Deprecated: Please use btesting.MustCreateDB(...).
func MustOpenDB() *DB { func MustOpenDB() *DB {
return MustOpenWithOption(nil) return MustOpenWithOption(nil)
} }
// MustOpenDBWithOption returns a new, open DB at a temporary location with given options. // MustOpenDBWithOption returns a new, open DB at a temporary location with given options.
// Deprecated: Please use btesting.MustCreateWithOption(...).
func MustOpenWithOption(o *bolt.Options) *DB { func MustOpenWithOption(o *bolt.Options) *DB {
f := tempfile() f := tempfile()
if o == nil { if o == nil {
@ -1655,6 +1647,16 @@ func MustOpenWithOption(o *bolt.Options) *DB {
} }
} }
// Closes the DB without removing the file.
// Allows for Reopen().
func (db *DB) CloseTemporarily() error {
if err := db.DB.Close(); err != nil {
return err
}
db.DB = nil
return nil
}
// Close closes the database and deletes the underlying file. // Close closes the database and deletes the underlying file.
func (db *DB) Close() error { func (db *DB) Close() error {
// Log statistics. // Log statistics.