2014-01-12 05:51:01 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
2014-03-23 21:40:08 +00:00
|
|
|
"io"
|
2014-01-12 05:51:01 +00:00
|
|
|
"io/ioutil"
|
2014-03-04 20:02:17 +00:00
|
|
|
"math/rand"
|
2014-01-12 05:51:01 +00:00
|
|
|
"os"
|
2014-02-21 16:49:15 +00:00
|
|
|
"strconv"
|
2014-03-04 20:02:17 +00:00
|
|
|
"strings"
|
2014-01-12 05:51:01 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2014-02-26 23:32:05 +00:00
|
|
|
// Ensure that a database can be opened without error.
|
|
|
|
func TestOpen(t *testing.T) {
|
|
|
|
f, _ := ioutil.TempFile("", "bolt-")
|
|
|
|
path := f.Name()
|
|
|
|
f.Close()
|
|
|
|
os.Remove(path)
|
|
|
|
defer os.RemoveAll(path)
|
|
|
|
|
|
|
|
db, err := Open(path, 0666)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, db)
|
|
|
|
db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that opening a database with a bad path returns an error.
|
|
|
|
func TestOpenBadPath(t *testing.T) {
|
|
|
|
db, err := Open("/../bad-path", 0666)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, db)
|
|
|
|
}
|
|
|
|
|
2014-01-12 22:30:09 +00:00
|
|
|
// Ensure that a database can be opened without error.
|
2014-01-12 05:51:01 +00:00
|
|
|
func TestDBOpen(t *testing.T) {
|
|
|
|
withDB(func(db *DB, path string) {
|
|
|
|
err := db.Open(path, 0666)
|
|
|
|
assert.NoError(t, err)
|
2014-01-12 22:30:09 +00:00
|
|
|
assert.Equal(t, db.Path(), path)
|
2014-01-12 05:51:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-01-12 22:30:09 +00:00
|
|
|
// Ensure that the database returns an error if already open.
|
|
|
|
func TestDBReopen(t *testing.T) {
|
|
|
|
withDB(func(db *DB, path string) {
|
|
|
|
db.Open(path, 0666)
|
|
|
|
err := db.Open(path, 0666)
|
2014-02-16 19:18:44 +00:00
|
|
|
assert.Equal(t, err, ErrDatabaseOpen)
|
2014-01-12 22:30:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-03-23 21:40:08 +00:00
|
|
|
// Ensure that the database returns an error if the file handle cannot be open.
|
|
|
|
func TestDBOpenFileError(t *testing.T) {
|
|
|
|
withDBFile(func(db *DB, path string) {
|
|
|
|
exp := &os.PathError{
|
|
|
|
Op: "open",
|
|
|
|
Path: path + "/youre-not-my-real-parent",
|
|
|
|
Err: syscall.ENOTDIR,
|
|
|
|
}
|
|
|
|
err := db.Open(path+"/youre-not-my-real-parent", 0666)
|
|
|
|
assert.Equal(t, err, exp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that write errors to the meta file handler during initialization are returned.
|
|
|
|
func TestDBMetaInitWriteError(t *testing.T) {
|
|
|
|
withDB(func(db *DB, path string) {
|
|
|
|
// Mock the file system.
|
|
|
|
db.ops.metaWriteAt = func(p []byte, offset int64) (n int, err error) { return 0, io.ErrShortWrite }
|
|
|
|
|
|
|
|
// Open the database.
|
|
|
|
err := db.Open(path, 0666)
|
|
|
|
assert.Equal(t, err, io.ErrShortWrite)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a database that is too small returns an error.
|
|
|
|
func TestDBFileTooSmall(t *testing.T) {
|
|
|
|
withDBFile(func(db *DB, path string) {
|
|
|
|
// corrupt the database
|
|
|
|
err := os.Truncate(path, int64(os.Getpagesize()))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = db.Open(path, 0666)
|
|
|
|
assert.Equal(t, err, &Error{"file size too small", nil})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that corrupt meta0 page errors get returned.
|
|
|
|
func TestDBCorruptMeta0(t *testing.T) {
|
|
|
|
withDB(func(db *DB, path string) {
|
|
|
|
var m meta
|
|
|
|
m.magic = magic
|
|
|
|
m.version = version
|
|
|
|
m.pageSize = 0x8000
|
|
|
|
|
|
|
|
// Create a file with bad magic.
|
|
|
|
b := make([]byte, 0x10000)
|
|
|
|
p0, p1 := (*page)(unsafe.Pointer(&b[0x0000])), (*page)(unsafe.Pointer(&b[0x8000]))
|
|
|
|
p0.meta().magic = 0
|
|
|
|
p0.meta().version = version
|
|
|
|
p1.meta().magic = magic
|
|
|
|
p1.meta().version = version
|
|
|
|
err := ioutil.WriteFile(path, b, 0666)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Open the database.
|
|
|
|
err = db.Open(path, 0666)
|
|
|
|
assert.Equal(t, err, &Error{"meta error", ErrInvalid})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-01-13 17:35:04 +00:00
|
|
|
// Ensure that a database cannot open a transaction when it's not open.
|
2014-03-09 00:01:49 +00:00
|
|
|
func TestDBTxErrDatabaseNotOpen(t *testing.T) {
|
2014-01-13 17:35:04 +00:00
|
|
|
withDB(func(db *DB, path string) {
|
2014-03-09 03:25:37 +00:00
|
|
|
tx, err := db.Tx()
|
|
|
|
assert.Nil(t, tx)
|
2014-02-16 19:18:44 +00:00
|
|
|
assert.Equal(t, err, ErrDatabaseNotOpen)
|
2014-01-13 17:35:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-03-09 03:25:37 +00:00
|
|
|
// Ensure that a read-write transaction can be retrieved.
|
|
|
|
func TestDBRWTx(t *testing.T) {
|
|
|
|
withOpenDB(func(db *DB, path string) {
|
|
|
|
tx, err := db.RWTx()
|
|
|
|
assert.NotNil(t, tx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, tx.DB(), db)
|
|
|
|
assert.Equal(t, tx.Writable(), true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that opening a transaction while the DB is closed returns an error.
|
|
|
|
func TestDBRWTxOpenWithClosedDB(t *testing.T) {
|
|
|
|
withDB(func(db *DB, path string) {
|
|
|
|
tx, err := db.RWTx()
|
|
|
|
assert.Equal(t, err, ErrDatabaseNotOpen)
|
|
|
|
assert.Nil(t, tx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-02-15 21:54:45 +00:00
|
|
|
// Ensure a database can provide a transactional block.
|
2014-03-09 00:01:49 +00:00
|
|
|
func TestDBTxBlock(t *testing.T) {
|
2014-02-15 21:54:45 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-09 03:25:37 +00:00
|
|
|
err := db.Do(func(tx *Tx) error {
|
|
|
|
tx.CreateBucket("widgets")
|
|
|
|
b := tx.Bucket("widgets")
|
2014-02-23 06:08:30 +00:00
|
|
|
b.Put([]byte("foo"), []byte("bar"))
|
|
|
|
b.Put([]byte("baz"), []byte("bat"))
|
|
|
|
b.Delete([]byte("foo"))
|
2014-02-15 21:54:45 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2014-03-21 15:46:03 +00:00
|
|
|
err = db.With(func(tx *Tx) error {
|
|
|
|
assert.Nil(t, tx.Bucket("widgets").Get([]byte("foo")))
|
|
|
|
assert.Equal(t, []byte("bat"), tx.Bucket("widgets").Get([]byte("baz")))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2014-02-15 21:54:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-02-16 06:38:03 +00:00
|
|
|
// Ensure a closed database returns an error while running a transaction block
|
2014-03-09 00:01:49 +00:00
|
|
|
func TestDBTxBlockWhileClosed(t *testing.T) {
|
2014-02-16 06:38:03 +00:00
|
|
|
withDB(func(db *DB, path string) {
|
2014-03-09 03:25:37 +00:00
|
|
|
err := db.Do(func(tx *Tx) error {
|
|
|
|
tx.CreateBucket("widgets")
|
2014-02-16 06:38:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-02-16 19:18:44 +00:00
|
|
|
assert.Equal(t, err, ErrDatabaseNotOpen)
|
2014-02-16 06:38:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-03-23 16:34:53 +00:00
|
|
|
// Ensure a panic occurs while trying to commit a managed transaction.
|
|
|
|
func TestDBTxBlockWithManualCommitAndRollback(t *testing.T) {
|
|
|
|
withOpenDB(func(db *DB, path string) {
|
|
|
|
db.Do(func(tx *Tx) error {
|
|
|
|
tx.CreateBucket("widgets")
|
|
|
|
assert.Panics(t, func() { tx.Commit() })
|
|
|
|
assert.Panics(t, func() { tx.Rollback() })
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
db.With(func(tx *Tx) error {
|
|
|
|
assert.Panics(t, func() { tx.Commit() })
|
|
|
|
assert.Panics(t, func() { tx.Rollback() })
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-02-09 22:52:19 +00:00
|
|
|
// Ensure that the database can be copied to a file path.
|
|
|
|
func TestDBCopyFile(t *testing.T) {
|
2014-02-16 06:38:03 +00:00
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-21 15:46:03 +00:00
|
|
|
db.Do(func(tx *Tx) error {
|
|
|
|
tx.CreateBucket("widgets")
|
|
|
|
tx.Bucket("widgets").Put([]byte("foo"), []byte("bar"))
|
|
|
|
tx.Bucket("widgets").Put([]byte("baz"), []byte("bat"))
|
|
|
|
return nil
|
|
|
|
})
|
2014-02-16 06:38:03 +00:00
|
|
|
assert.NoError(t, os.RemoveAll("/tmp/bolt.copyfile.db"))
|
|
|
|
assert.NoError(t, db.CopyFile("/tmp/bolt.copyfile.db", 0666))
|
2014-02-09 22:52:19 +00:00
|
|
|
|
2014-02-16 06:38:03 +00:00
|
|
|
var db2 DB
|
|
|
|
assert.NoError(t, db2.Open("/tmp/bolt.copyfile.db", 0666))
|
|
|
|
defer db2.Close()
|
|
|
|
|
2014-03-21 15:46:03 +00:00
|
|
|
db2.With(func(tx *Tx) error {
|
|
|
|
assert.Equal(t, []byte("bar"), tx.Bucket("widgets").Get([]byte("foo")))
|
|
|
|
assert.Equal(t, []byte("bat"), tx.Bucket("widgets").Get([]byte("baz")))
|
|
|
|
return nil
|
|
|
|
})
|
2014-02-16 06:38:03 +00:00
|
|
|
})
|
2014-02-09 22:52:19 +00:00
|
|
|
}
|
|
|
|
|
2014-02-21 16:49:15 +00:00
|
|
|
// Ensure the database can return stats about itself.
|
|
|
|
func TestDBStat(t *testing.T) {
|
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-09 03:25:37 +00:00
|
|
|
db.Do(func(tx *Tx) error {
|
|
|
|
tx.CreateBucket("widgets")
|
|
|
|
b := tx.Bucket("widgets")
|
2014-02-21 16:49:15 +00:00
|
|
|
for i := 0; i < 10000; i++ {
|
2014-02-23 06:08:30 +00:00
|
|
|
b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i)))
|
2014-02-21 16:49:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// Delete some keys.
|
2014-03-21 15:46:03 +00:00
|
|
|
db.Do(func(tx *Tx) error {
|
|
|
|
return tx.Bucket("widgets").Delete([]byte("10"))
|
|
|
|
})
|
|
|
|
db.Do(func(tx *Tx) error {
|
|
|
|
return tx.Bucket("widgets").Delete([]byte("1000"))
|
|
|
|
})
|
2014-02-21 16:49:15 +00:00
|
|
|
|
|
|
|
// Open some readers.
|
2014-03-09 00:01:49 +00:00
|
|
|
t0, _ := db.Tx()
|
|
|
|
t1, _ := db.Tx()
|
|
|
|
t2, _ := db.Tx()
|
2014-03-09 03:25:37 +00:00
|
|
|
t2.Rollback()
|
2014-02-21 16:49:15 +00:00
|
|
|
|
|
|
|
// Obtain stats.
|
|
|
|
stat, err := db.Stat()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, stat.PageCount, 128)
|
|
|
|
assert.Equal(t, stat.FreePageCount, 2)
|
|
|
|
assert.Equal(t, stat.PageSize, 4096)
|
|
|
|
assert.Equal(t, stat.MmapSize, 4194304)
|
2014-03-09 00:01:49 +00:00
|
|
|
assert.Equal(t, stat.TxCount, 2)
|
2014-02-21 16:49:15 +00:00
|
|
|
|
|
|
|
// Close readers.
|
2014-03-09 03:25:37 +00:00
|
|
|
t0.Rollback()
|
|
|
|
t1.Rollback()
|
2014-02-21 16:49:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the getting stats on a closed database returns an error.
|
|
|
|
func TestDBStatWhileClosed(t *testing.T) {
|
|
|
|
withDB(func(db *DB, path string) {
|
|
|
|
stat, err := db.Stat()
|
|
|
|
assert.Equal(t, err, ErrDatabaseNotOpen)
|
|
|
|
assert.Nil(t, stat)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-02-09 22:52:19 +00:00
|
|
|
// Ensure that an error is returned when a database write fails.
|
|
|
|
func TestDBWriteFail(t *testing.T) {
|
|
|
|
t.Skip("pending") // TODO(benbjohnson)
|
|
|
|
}
|
|
|
|
|
2014-02-11 19:16:12 +00:00
|
|
|
// Ensure that the mmap grows appropriately.
|
|
|
|
func TestDBMmapSize(t *testing.T) {
|
|
|
|
db := &DB{pageSize: 4096}
|
|
|
|
assert.Equal(t, db.mmapSize(0), minMmapSize)
|
|
|
|
assert.Equal(t, db.mmapSize(16384), minMmapSize)
|
|
|
|
assert.Equal(t, db.mmapSize(minMmapSize-1), minMmapSize)
|
|
|
|
assert.Equal(t, db.mmapSize(minMmapSize), minMmapSize*2)
|
|
|
|
assert.Equal(t, db.mmapSize(10000000), 20000768)
|
|
|
|
assert.Equal(t, db.mmapSize((1<<30)-1), 2147483648)
|
|
|
|
assert.Equal(t, db.mmapSize(1<<30), 1<<31)
|
|
|
|
}
|
|
|
|
|
2014-02-28 22:13:00 +00:00
|
|
|
// Ensure that a database can return a string representation of itself.
|
|
|
|
func TestDBString(t *testing.T) {
|
|
|
|
db := &DB{path: "/tmp/foo"}
|
|
|
|
assert.Equal(t, db.String(), `DB<"/tmp/foo">`)
|
|
|
|
assert.Equal(t, db.GoString(), `bolt.DB{path:"/tmp/foo"}`)
|
|
|
|
}
|
|
|
|
|
2014-03-04 20:02:17 +00:00
|
|
|
// Benchmark the performance of single put transactions in random order.
|
|
|
|
func BenchmarkDBPutSequential(b *testing.B) {
|
|
|
|
value := []byte(strings.Repeat("0", 64))
|
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-21 15:46:03 +00:00
|
|
|
db.Do(func(tx *Tx) error {
|
|
|
|
return tx.CreateBucket("widgets")
|
|
|
|
})
|
2014-03-04 20:02:17 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2014-03-21 15:46:03 +00:00
|
|
|
db.Do(func(tx *Tx) error {
|
|
|
|
return tx.Bucket("widgets").Put([]byte(strconv.Itoa(i)), value)
|
|
|
|
})
|
2014-03-04 20:02:17 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Benchmark the performance of single put transactions in random order.
|
|
|
|
func BenchmarkDBPutRandom(b *testing.B) {
|
|
|
|
indexes := rand.Perm(b.N)
|
|
|
|
value := []byte(strings.Repeat("0", 64))
|
|
|
|
withOpenDB(func(db *DB, path string) {
|
2014-03-21 15:46:03 +00:00
|
|
|
db.Do(func(tx *Tx) error {
|
|
|
|
return tx.CreateBucket("widgets")
|
|
|
|
})
|
2014-03-04 20:02:17 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2014-03-21 15:46:03 +00:00
|
|
|
db.Do(func(tx *Tx) error {
|
|
|
|
return tx.Bucket("widgets").Put([]byte(strconv.Itoa(indexes[i])), value)
|
|
|
|
})
|
2014-03-04 20:02:17 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-01-12 22:30:09 +00:00
|
|
|
// withDB executes a function with a database reference.
|
2014-01-12 05:51:01 +00:00
|
|
|
func withDB(fn func(*DB, string)) {
|
|
|
|
f, _ := ioutil.TempFile("", "bolt-")
|
|
|
|
path := f.Name()
|
|
|
|
f.Close()
|
|
|
|
os.Remove(path)
|
|
|
|
defer os.RemoveAll(path)
|
|
|
|
|
2014-02-14 15:32:42 +00:00
|
|
|
var db DB
|
|
|
|
fn(&db, path)
|
2014-01-12 05:51:01 +00:00
|
|
|
}
|
2014-01-12 22:30:09 +00:00
|
|
|
|
2014-01-13 17:35:04 +00:00
|
|
|
// withOpenDB executes a function with an already opened database.
|
|
|
|
func withOpenDB(fn func(*DB, string)) {
|
|
|
|
withDB(func(db *DB, path string) {
|
|
|
|
if err := db.Open(path, 0666); err != nil {
|
|
|
|
panic("cannot open db: " + err.Error())
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
fn(db, path)
|
|
|
|
})
|
|
|
|
}
|
2014-02-16 04:50:34 +00:00
|
|
|
|
|
|
|
func trunc(b []byte, length int) []byte {
|
|
|
|
if length < len(b) {
|
|
|
|
return b[:length]
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|