aboutsummaryrefslogtreecommitdiff
path: root/db.go
diff options
context:
space:
mode:
authorTommi Virtanen <tv@eagain.net>2014-03-23 14:40:08 -0700
committerBen Johnson <benbjohnson@yahoo.com>2014-03-24 07:47:33 -0600
commite9b2cab0fa6fd0536cf472967cd82f418218c81c (patch)
treeb87399eb867a2a4bfd7fe84c54d25108ebe625f3 /db.go
parentResolve remaining errcheck warnings. (diff)
downloaddedo-e9b2cab0fa6fd0536cf472967cd82f418218c81c.tar.gz
dedo-e9b2cab0fa6fd0536cf472967cd82f418218c81c.tar.xz
Re-add tests for write failures
Commit d2173f5f0ecbf4ed93c768e975435b04df3186ec removed the complete os & syscall mocking layer as overly complex. This commit adds back the simplest possible thing: hooks to control the database file writes. Missing tests: TestDBOpenMetaFileError, TestDBMmapStatError. These are harder to test without more extensive mocking. Conflicts: db_test.go
Diffstat (limited to 'db.go')
-rw-r--r--db.go15
1 files changed, 14 insertions, 1 deletions
diff --git a/db.go b/db.go
index 51c24d3..5ec75b0 100644
--- a/db.go
+++ b/db.go
@@ -34,6 +34,11 @@ type DB struct {
rwlock sync.Mutex // Allows only one writer at a time.
metalock sync.Mutex // Protects meta page access.
mmaplock sync.RWMutex // Protects mmap access during remapping.
+
+ ops struct {
+ writeAt func(b []byte, off int64) (n int, err error)
+ metaWriteAt func(b []byte, off int64) (n int, err error)
+ }
}
// Path returns the path to currently open database file.
@@ -74,6 +79,14 @@ func (db *DB) Open(path string, mode os.FileMode) error {
return err
}
+ // default values for test hooks
+ if db.ops.writeAt == nil {
+ db.ops.writeAt = db.file.WriteAt
+ }
+ if db.ops.metaWriteAt == nil {
+ db.ops.metaWriteAt = db.metafile.WriteAt
+ }
+
// Initialize the database if it doesn't exist.
if info, err := db.file.Stat(); err != nil {
return &Error{"stat error", err}
@@ -226,7 +239,7 @@ func (db *DB) init() error {
p.count = 0
// Write the buffer to our data file.
- if _, err := db.metafile.WriteAt(buf, 0); err != nil {
+ if _, err := db.ops.metaWriteAt(buf, 0); err != nil {
return err
}