Add mock OS.

pull/34/head
Ben Johnson 2014-01-12 14:59:03 -07:00
parent ee24437bfc
commit 746c287b26
5 changed files with 64 additions and 13 deletions

25
db.go
View File

@ -1,7 +1,7 @@
package bolt
import (
"os"
. "os"
"sync"
"syscall"
"unsafe"
@ -26,8 +26,9 @@ type DB struct {
sync.Mutex
opened bool
file *os.File
metafile *os.File
os OS
file *File
metafile *File
data []byte
buf []byte
meta0 *meta
@ -55,18 +56,22 @@ type DB struct {
}
func NewDB() *DB {
return &DB{}
return &DB{os: &sysos{}}
}
func (db *DB) Path() string {
return db.path
}
func (db *DB) Open(path string, mode os.FileMode) error {
func (db *DB) Open(path string, mode FileMode) error {
var err error
db.Lock()
defer db.Unlock()
if db.os == nil {
db.os = &sysos{}
}
// Exit if the database is currently open.
if db.opened {
return DatabaseAlreadyOpenedError
@ -74,11 +79,11 @@ func (db *DB) Open(path string, mode os.FileMode) error {
// Open data file and separate sync handler for metadata writes.
db.path = path
if db.file, err = os.OpenFile(db.path, os.O_RDWR|os.O_CREATE, mode); err != nil {
if db.file, err = db.os.OpenFile(db.path, O_RDWR|O_CREATE, mode); err != nil {
db.close()
return err
}
if db.metafile, err = os.OpenFile(db.path, os.O_RDWR|os.O_SYNC, mode); err != nil {
if db.metafile, err = db.os.OpenFile(db.path, O_RDWR|O_SYNC, mode); err != nil {
db.close()
return err
}
@ -140,7 +145,7 @@ func (db *DB) mmap() error {
// Determine the map size based on the file size.
var size int
if info, err := os.Stat(db.file.Name()); err != nil {
if info, err := db.os.Stat(db.file.Name()); err != nil {
return err
} else if info.Size() < int64(db.pageSize*2) {
return &Error{"file size too small", nil}
@ -169,7 +174,7 @@ func (db *DB) mmap() error {
// init creates a new database file and initializes its meta pages.
func (db *DB) init() error {
// Set the page size to the OS page size unless that is larger than max page size.
db.pageSize = os.Getpagesize()
db.pageSize = db.os.Getpagesize()
if db.pageSize > maxPageSize {
db.pageSize = maxPageSize
}
@ -765,7 +770,7 @@ func (db *DB) SetFlags(flag int, onoff bool) error {
return nil
}
func (db *DB) Stat() *Stat {
func (db *DB) Stat() *stat {
/*
int toggle;

22
os.go
View File

@ -1,5 +1,25 @@
package bolt
import (
_ "os"
"os"
)
type OS interface {
OpenFile(name string, flag int, perm os.FileMode) (file *os.File, err error)
Stat(name string) (fi os.FileInfo, err error)
Getpagesize() int
}
type sysos struct{}
func (o *sysos) OpenFile(name string, flag int, perm os.FileMode) (file *os.File, err error) {
return os.OpenFile(name, flag, perm)
}
func (o *sysos) Stat(name string) (fi os.FileInfo, err error) {
return os.Stat(name)
}
func (o *sysos) Getpagesize() int {
return os.Getpagesize()
}

26
os_test.go Normal file
View File

@ -0,0 +1,26 @@
package bolt
import (
"os"
"github.com/stretchr/testify/mock"
)
type mockos struct {
mock.Mock
}
func (m *mockos) OpenFile(name string, flag int, perm os.FileMode) (file *os.File, err error) {
args := m.Called(name, flag, perm)
return args.Get(0).(*os.File), args.Error(1)
}
func (m *mockos) Stat(name string) (fi os.FileInfo, err error) {
args := m.Called(name)
return args.Get(0).(os.FileInfo), args.Error(1)
}
func (m *mockos) Getpagesize() int {
args := m.Called()
return args.Int(0)
}

View File

@ -1,6 +1,6 @@
package bolt
type Stat struct {
type stat struct {
PageSize int
Depth int
BranchPageCount int

View File

@ -1467,7 +1467,7 @@ func (t *transaction) Bucket(name string, flags int) (*Bucket, error) {
return nil, nil
}
func (t *transaction) Stat(b Bucket) *Stat {
func (t *transaction) Stat(b Bucket) *stat {
/*
if (txn == NULL || arg == NULL || dbi >= txn->mt_numdbs)
return EINVAL;