mirror of https://github.com/etcd-io/bbolt.git
Mock syscall.
parent
28c1e86a27
commit
47224c4387
8
db.go
8
db.go
|
@ -27,6 +27,7 @@ type DB struct {
|
|||
opened bool
|
||||
|
||||
os _os
|
||||
syscall _syscall
|
||||
file file
|
||||
metafile file
|
||||
data []byte
|
||||
|
@ -71,6 +72,9 @@ func (db *DB) Open(path string, mode os.FileMode) error {
|
|||
if db.os == nil {
|
||||
db.os = &sysos{}
|
||||
}
|
||||
if db.syscall == nil {
|
||||
db.syscall = &syssyscall{}
|
||||
}
|
||||
|
||||
// Exit if the database is currently open.
|
||||
if db.opened {
|
||||
|
@ -145,7 +149,7 @@ func (db *DB) mmap() error {
|
|||
|
||||
// Determine the map size based on the file size.
|
||||
var size int
|
||||
if info, err := db.os.Stat(db.file.Name()); err != nil {
|
||||
if info, err := db.os.Stat(db.path); err != nil {
|
||||
return err
|
||||
} else if info.Size() < int64(db.pageSize*2) {
|
||||
return &Error{"file size too small", nil}
|
||||
|
@ -154,7 +158,7 @@ func (db *DB) mmap() error {
|
|||
}
|
||||
|
||||
// Memory-map the data file as a byte slice.
|
||||
if db.data, err = syscall.Mmap(int(db.file.Fd()), 0, size, syscall.PROT_READ, syscall.MAP_SHARED); err != nil {
|
||||
if db.data, err = db.syscall.Mmap(int(db.file.Fd()), 0, size, syscall.PROT_READ, syscall.MAP_SHARED); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
14
db_test.go
14
db_test.go
|
@ -47,6 +47,20 @@ func TestDBOpenMetaFileError(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
// Ensure that the database limits the upper bound of the page size.
|
||||
/*
|
||||
func TestDBLimitPageSize(t *testing.T) {
|
||||
withMockDB(func(db *DB, mockos *mockos, path string) {
|
||||
buf := make([]byte, 4096)
|
||||
mockos.On("OpenFile", path, os.O_RDWR|os.O_CREATE, os.FileMode(0666)).Return(&mockfile{}, nil)
|
||||
mockos.On("OpenFile", path, os.O_RDWR|os.O_SYNC, os.FileMode(0666)).Return(&mockfile{}, nil)
|
||||
mockos.On("OpenFile", path, os.O_RDWR|os.O_SYNC, os.FileMode(0666)).Return(&mockfile{}, nil)
|
||||
err := db.Open(path, 0666)
|
||||
assert.Equal(t, err, exp)
|
||||
})
|
||||
}
|
||||
*/
|
||||
|
||||
// withDB executes a function with a database reference.
|
||||
func withDB(fn func(*DB, string)) {
|
||||
f, _ := ioutil.TempFile("", "bolt-")
|
||||
|
|
1
file.go
1
file.go
|
@ -2,7 +2,6 @@ package bolt
|
|||
|
||||
type file interface {
|
||||
Fd() uintptr
|
||||
Name() string
|
||||
ReadAt(b []byte, off int64) (n int, err error)
|
||||
WriteAt(b []byte, off int64) (n int, err error)
|
||||
}
|
||||
|
|
|
@ -6,18 +6,13 @@ import (
|
|||
|
||||
type mockfile struct {
|
||||
mock.Mock
|
||||
fd uintptr
|
||||
name string
|
||||
fd uintptr
|
||||
}
|
||||
|
||||
func (m *mockfile) Fd() uintptr {
|
||||
return m.fd
|
||||
}
|
||||
|
||||
func (m *mockfile) Name() string {
|
||||
return m.name
|
||||
}
|
||||
|
||||
func (m *mockfile) ReadAt(b []byte, off int64) (n int, err error) {
|
||||
args := m.Called(b, off)
|
||||
return args.Int(0), args.Error(1)
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package bolt
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type _syscall interface {
|
||||
Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error)
|
||||
}
|
||||
|
||||
type syssyscall struct{}
|
||||
|
||||
func (o *syssyscall) Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
|
||||
// err = (EACCES, EBADF, EINVAL, ENODEV, ENOMEM, ENXIO, EOVERFLOW)
|
||||
return syscall.Mmap(fd, offset, length, prot, flags)
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package bolt
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type mocksyscall struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *mocksyscall) Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
|
||||
args := m.Called(fd, offset, length, prot, flags)
|
||||
return args.Get(0).([]byte), args.Error(1)
|
||||
}
|
Loading…
Reference in New Issue